<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.7 |
+ | CiviCRM version 5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2017 |
+ | Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
* @package CiviCRM_APIv3
* @subpackage API_Job
*
- * @copyright CiviCRM LLC (c) 2004-2017
+ * @copyright CiviCRM LLC (c) 2004-2018
*/
/**
$this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params);
}
+ /**
+ * Clone job
+ */
+ public function testClone() {
+ $createResult = $this->callAPISuccess('job', 'create', $this->_params);
+ $params = array('id' => $createResult['id']);
+ $cloneResult = $this->callAPIAndDocument('job', 'clone', $params, __FUNCTION__, __FILE__);
+ $clonedJob = $cloneResult['values'][$cloneResult['id']];
+ $this->assertEquals($this->_params['name'] . ' - Copy', $clonedJob['name']);
+ $this->assertEquals($this->_params['description'], $clonedJob['description']);
+ $this->assertEquals($this->_params['parameters'], $clonedJob['parameters']);
+ $this->assertEquals($this->_params['is_active'], $clonedJob['is_active']);
+ $this->assertArrayNotHasKey('last_run', $clonedJob);
+ $this->assertArrayNotHasKey('scheduled_run_date', $clonedJob);
+ }
+
/**
* Check if required fields are not passed.
*/
$this->callAPISuccessGetCount('Membership', array('contact_id' => $contact2ID), 0);
$this->callAPISuccessGetCount('EntityTag', array('contact_id' => $contactID), 2);
$this->callAPISuccessGetCount('EntityTag', array('contact_id' => $contact2ID), 0);
- // 12 activities is one for each contribution (2), one for each membership (+2 = 4)
- // 3 for each of the added activities as there are 3 roles (+6 = 10
- // 2 for the (source & target) contact merged activity (+2 = 12)
- $this->callAPISuccessGetCount('ActivityContact', array('contact_id' => $contactID), 12);
+ // 14 activities is one for each contribution (2), two (source + target) for each membership (+(2x2) = 6)
+ // 3 for each of the added activities as there are 3 roles (+6 = 12
+ // 2 for the (source & target) contact merged activity (+2 = 14)
+ $this->callAPISuccessGetCount('ActivityContact', array('contact_id' => $contactID), 14);
// 2 for the connection to the deleted by merge activity (source & target)
$this->callAPISuccessGetCount('ActivityContact', array('contact_id' => $contact2ID), 2);
}
}
+ /**
+ * Test the batch merge copes with view only custom data field.
+ */
+ public function testBatchMergeCustomDataViewOnlyField() {
+ CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'edit my contact');
+ $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
+ $this->individualCreate($mouseParams);
+
+ $customGroup = $this->CustomGroupCreate();
+ $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'is_view' => 1));
+ $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah']));
+
+ $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0, 'mode' => 'safe'));
+ $this->assertEquals(1, count($result['values']['merged']));
+ $mouseParams['return'] = 'custom_' . $customField['id'];
+ $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
+ $this->assertEquals('blah', $mouse['custom_' . $customField['id']]);
+
+ $this->customFieldDelete($customGroup['id']);
+ $this->customGroupDelete($customGroup['id']);
+ }
+
/**
* Test the batch merge function actually works!
*
return $data;
}
+ /**
+ * Test processing membership for deceased contacts.
+ */
+ public function testProcessMembershipDeceased() {
+ $this->callAPISuccess('Job', 'process_membership', []);
+ $deadManWalkingID = $this->individualCreate();
+ $membershipID = $this->contactMembershipCreate(array('contact_id' => $deadManWalkingID));
+ $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
+ $this->callAPISuccess('Job', 'process_membership', []);
+ $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipID]);
+ $deceasedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
+ $this->assertEquals($deceasedStatusId, $membership['status_id']);
+ }
+
+ /**
+ * Test we get an error is deceased status is disabled.
+ */
+ public function testProcessMembershipNoDeceasedStatus() {
+ $deceasedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
+ $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 0, 'id' => $deceasedStatusId]);
+ CRM_Core_PseudoConstant::flush();
+
+ $deadManWalkingID = $this->individualCreate();
+ $this->contactMembershipCreate(array('contact_id' => $deadManWalkingID));
+ $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
+ $this->callAPIFailure('Job', 'process_membership', []);
+
+ $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 1, 'id' => $deceasedStatusId]);
+ }
+
+ /**
+ * Test processing membership: check that status is updated when it should be
+ * and left alone when it shouldn't.
+ */
+ public function testProcessMembershipUpdateStatus() {
+ $membershipTypeId = $this->membershipTypeCreate();
+
+ // Create admin-only membership status and get all statuses.
+ $result = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
+ $membershipStatusIdAdmin = $result['values'][0]['id'];
+ $memStatus = CRM_Member_PseudoConstant::membershipStatus();
+
+ // Default params, which we'll expand on below.
+ $params = [
+ 'membership_type_id' => $membershipTypeId,
+ 'skipStatusCal' => 1, // Don't calculate status.
+ 'source' => 'Test',
+ 'sequential' => 1,
+ ];
+
+ /*
+ * We create various memberships with wrong status, then check that the
+ * process_membership job sets the correct status for each.
+ * Also some memberships that should not be updated.
+ */
+
+ // Create membership with incorrect status but dates implying status New.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d');
+ $params['start_date'] = date('Y-m-d');
+ $params['end_date'] = date('Y-m-d', strtotime('now + 1 year'));
+ $params['status_id'] = 'Current'; // Intentionally incorrect status.
+ $resultNew = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Current', $memStatus), $resultNew['values'][0]['status_id']);
+
+ // Create membership with incorrect status but dates implying status Current.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
+ $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
+ $params['status_id'] = 'New'; // Intentionally incorrect status.
+ $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('New', $memStatus), $resultCurrent['values'][0]['status_id']);
+
+ // Create membership with incorrect status but dates implying status Grace.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 53 week'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 53 week'));
+ $params['end_date'] = date('Y-m-d', strtotime('now - 1 week'));
+ $params['status_id'] = 'Current'; // Intentionally incorrect status.
+ $resultGrace = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Current', $memStatus), $resultGrace['values'][0]['status_id']);
+
+ // Create membership with incorrect status but dates implying status Expired.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
+ $params['status_id'] = 'Grace'; // Intentionally incorrect status.
+ $resultExpired = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Grace', $memStatus), $resultExpired['values'][0]['status_id']);
+
+ // Create Pending membership with dates implying New: should not get updated.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d');
+ $params['start_date'] = date('Y-m-d');
+ $params['end_date'] = date('Y-m-d', strtotime('now + 1 year'));
+ $params['status_id'] = 'Pending';
+ $resultPending = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Pending', $memStatus), $resultPending['values'][0]['status_id']);
+
+ // Create Cancelled membership with dates implying Current: should not get updated.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
+ $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
+ $params['status_id'] = 'Cancelled';
+ $resultCancelled = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Cancelled', $memStatus), $resultCancelled['values'][0]['status_id']);
+
+ // Create status-overridden membership with dates implying Expired: should not get updated.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
+ $params['status_id'] = 'Current';
+ $params['is_override'] = 1;
+ $resultOverride = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals(array_search('Current', $memStatus), $resultOverride['values'][0]['status_id']);
+
+ // Create membership with admin-only status but dates implying Expired: should not get updated.
+ $params['contact_id'] = $this->individualCreate();
+ $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
+ $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
+ $params['status_id'] = $membershipStatusIdAdmin;
+ $params['is_override'] = 1;
+ $resultAdmin = $this->callAPISuccess('Membership', 'create', $params);
+ $this->assertEquals($membershipStatusIdAdmin, $resultAdmin['values'][0]['status_id']);
+
+ /*
+ * Create membership type with inheritence and check processing of secondary memberships.
+ */
+ $employerRelationshipId = $this->callAPISuccessGetValue('RelationshipType', [
+ 'return' => "id",
+ 'name_b_a' => "Employer Of",
+ ]);
+ // Create membership type: inherited through employment.
+ $membershipOrgId = $this->organizationCreate();
+ $params = [
+ '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' => $employerRelationshipId,
+ 'relationship_direction' => 'b_a',
+ 'is_active' => 1,
+ ];
+ $result = $this->callAPISuccess('membership_type', 'create', $params);
+ $membershipTypeId = $result['id'];
+
+ // Create employer and first employee
+ $employerId = $this->organizationCreate([], 1);
+ $memberContactId = $this->individualCreate(['employer_id' => $employerId], 0);
+
+ // Create inherited membership with incorrect status but dates implying status Expired.
+ $params = [
+ 'contact_id' => $employerId,
+ 'membership_type_id' => $membershipTypeId,
+ 'source' => 'Test suite',
+ 'join_date' => date('Y-m-d', strtotime('now - 16 month')),
+ 'start_date' => date('Y-m-d', strtotime('now - 16 month')),
+ 'end_date' => date('Y-m-d', strtotime('now - 4 month')),
+ 'status_id' => 'Grace', // Intentionally incorrect status.
+ 'skipStatusCal' => 1, // Don't calculate status.
+ ];
+ $organizationMembershipID = $this->contactMembershipCreate($params);
+
+ // Check that the employee inherited the membership and status.
+ $params = [
+ 'contact_id' => $memberContactId,
+ 'membership_type_id' => $membershipTypeId,
+ 'sequential' => 1,
+ ];
+ $resultInheritExpired = $this->callAPISuccess('membership', 'get', $params);
+ $this->assertEquals(1, $resultInheritExpired['count']);
+ $this->assertEquals($organizationMembershipID, $resultInheritExpired['values'][0]['owner_membership_id']);
+ $this->assertEquals(array_search('Grace', $memStatus), $resultInheritExpired['values'][0]['status_id']);
+
+ // Reset static $relatedContactIds array in createRelatedMemberships(),
+ // to avoid bug where inherited membership gets deleted.
+ $var = TRUE;
+ CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE);
+
+ // Check that after running process_membership job, statuses are correct.
+ $this->callAPISuccess('Job', 'process_membership', []);
+
+ // New - should get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultNew['values'][0]['id']]);
+ $this->assertEquals(array_search('New', $memStatus), $membership['status_id']);
+
+ // Current - should get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultCurrent['values'][0]['id']]);
+ $this->assertEquals(array_search('Current', $memStatus), $membership['status_id']);
+
+ // Grace - should get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultGrace['values'][0]['id']]);
+ $this->assertEquals(array_search('Grace', $memStatus), $membership['status_id']);
+
+ // Expired - should get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultExpired['values'][0]['id']]);
+ $this->assertEquals(array_search('Expired', $memStatus), $membership['status_id']);
+
+ // Pending - should not get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultPending['values'][0]['id']]);
+ $this->assertEquals(array_search('Pending', $memStatus), $membership['status_id']);
+
+ // Cancelled - should not get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultCancelled['values'][0]['id']]);
+ $this->assertEquals(array_search('Cancelled', $memStatus), $membership['status_id']);
+
+ // Override - should not get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultOverride['values'][0]['id']]);
+ $this->assertEquals(array_search('Current', $memStatus), $membership['status_id']);
+
+ // Admin - should not get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultAdmin['values'][0]['id']]);
+ $this->assertEquals($membershipStatusIdAdmin, $membership['status_id']);
+
+ // Inherit Expired - should get updated.
+ $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultInheritExpired['values'][0]['id']]);
+ $this->assertEquals(array_search('Expired', $memStatus), $membership['status_id']);
+ }
+
}