From c16b461902a6e010aa8d6347fe4dd7b2a076506d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 8 Aug 2017 15:53:42 -0700 Subject: [PATCH] CRM-21052 - Activity.create API - Abide by setting `civicaseActivityRevisions` --- api/v3/Activity.php | 5 ++-- tests/phpunit/api/v3/CaseTest.php | 43 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/api/v3/Activity.php b/api/v3/Activity.php index 1b675c265e..924bfe8cb4 100644 --- a/api/v3/Activity.php +++ b/api/v3/Activity.php @@ -43,6 +43,7 @@ * API result array */ function civicrm_api3_activity_create($params) { + $isNew = empty($params['id']); if (empty($params['id'])) { // an update does not require any mandatory parameters @@ -91,7 +92,7 @@ function civicrm_api3_activity_create($params) { } if (!empty($params['case_id'])) { $case_id = $params['case_id']; - if (!empty($params['id'])) { + if (!empty($params['id']) && Civi::settings()->get('civicaseActivityRevisions')) { $oldActivityParams = array('id' => $params['id']); if (!$oldActivityValues) { CRM_Activity_BAO_Activity::retrieve($oldActivityParams, $oldActivityValues); @@ -158,7 +159,7 @@ function civicrm_api3_activity_create($params) { $activityBAO = CRM_Activity_BAO_Activity::create($params); if (isset($activityBAO->id)) { - if ($case_id && !$createRevision) { + if ($case_id && $isNew && !$createRevision) { // If this is a brand new case activity, add to case(s) foreach ((array) $case_id as $singleCaseId) { $caseActivityParams = array('activity_id' => $activityBAO->id, 'case_id' => $singleCaseId); diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index 32363b9025..ec86456ba1 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -77,6 +77,11 @@ class api_v3_CaseTest extends CiviCaseTestCase { ); } + public function tearDown() { + unset($GLOBALS['civicrm_setting']['domain']['civicaseActivityRevisions']); + parent::tearDown(); + } + /** * Check with empty array. */ @@ -427,6 +432,44 @@ class api_v3_CaseTest extends CiviCaseTestCase { //TODO: check some more things } + /** + * If you disable `civicaseActivityRevisions`, then editing an activity + * will *not* create or change IDs. + */ + public function testCaseActivityUpdate_Untracked() { + $GLOBALS['civicrm_setting']['domain']['civicaseActivityRevisions'] = FALSE; + Civi::service('settings_manager')->useMandatory(); + $this->assertEquals(FALSE, Civi::settings()->get('civicaseActivityRevisions')); + + // Need to create the case and activity before we can update it + $this->testCaseActivityCreate(); + + $oldIDs = CRM_Utils_SQL_Select::from('civicrm_activity') + ->select('id, original_id, is_current_revision') + ->orderBy('id') + ->execute()->fetchAll(); + + $params = array( + 'activity_id' => $this->_caseActivityId, + 'case_id' => 1, + 'activity_type_id' => 14, + 'source_contact_id' => $this->_loggedInUser, + 'subject' => 'New subject', + ); + $result = $this->callAPISuccess('activity', 'create', $params); + $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']); + + // id should not change because we've opted out. + $this->assertEquals($this->_caseActivityId, $result['values'][$result['id']]['id']); + $this->assertEmpty($result['values'][$result['id']]['original_id']); + + $newIDs = CRM_Utils_SQL_Select::from('civicrm_activity') + ->select('id, original_id, is_current_revision') + ->orderBy('id') + ->execute()->fetchAll(); + $this->assertEquals($oldIDs, $newIDs); + } + public function testCaseActivityUpdateCustom() { // Create a case first $result = $this->callAPISuccess('case', 'create', $this->_params); -- 2.25.1