From 6dbee28b471580735ccbb989ba5cab89ae80c30d Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 30 Dec 2019 17:06:06 +1300 Subject: [PATCH] Permit modified_id as a parameter for membership create api Adding a membership create api parameter makes this accessible via the api and also allows us to deprecate & remove the array. Note this PR does nothing at the api level to make it available - it just takes advantage of apiv3 passing params through to the BAO. The support is added in the BAO. However, I think we should add formal apiv4 support for modified_id as a parameter for Membership.create - only issue is that of course it's not a 'real' param so we need to discuss / understand how that needs to look. In general I think there are a few BAO that store modified_id --- CRM/Member/BAO/Membership.php | 9 ++++++--- .../CRM/Member/BAO/MembershipLogTest.php | 20 ++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 3fff6f955f..dfa6f1716e 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -105,12 +105,15 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { 'max_related' => $membership->max_related, ]; - $session = CRM_Core_Session::singleton(); + if (!empty($params['modified_id'])) { + $membershipLog['modified_id'] = $params['modified_id']; + } // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id - if ($session->get('userID')) { - $membershipLog['modified_id'] = $session->get('userID'); + elseif (CRM_Core_Session::singleton()->get('userID')) { + $membershipLog['modified_id'] = CRM_Core_Session::singleton()->get('userID'); } elseif (!empty($ids['userId'])) { + // @todo deprecated this - transform in create as a transitional measure. $membershipLog['modified_id'] = $ids['userId']; } else { diff --git a/tests/phpunit/CRM/Member/BAO/MembershipLogTest.php b/tests/phpunit/CRM/Member/BAO/MembershipLogTest.php index 0142c50345..9322190ad9 100644 --- a/tests/phpunit/CRM/Member/BAO/MembershipLogTest.php +++ b/tests/phpunit/CRM/Member/BAO/MembershipLogTest.php @@ -103,7 +103,6 @@ class CRM_Member_BAO_MembershipLogTest extends CiviUnitTestCase { */ public function tearDown() { $this->relationshipTypeDelete($this->relationshipTypeID); - $this->membershipStatusDelete($this->membershipStatusID); $this->quickCleanUpFinancialEntities(); $this->restoreMembershipTypes(); $this->contactDelete($this->organizationContactID); @@ -143,15 +142,29 @@ class CRM_Member_BAO_MembershipLogTest extends CiviUnitTestCase { $this->contactDelete($contactID); } + /** + * Test that the value for modified_id can be set. + * + * @throws \CRM_Core_Exception + */ + public function testCreateMembershipWithPassedInModifiedID() { + $modifier = $this->individualCreate(); + $membershipID = $this->setupMembership($modifier)[1]; + $this->assertEquals($modifier, $this->callAPISuccessGetValue('MembershipLog', ['membership_id' => $membershipID, 'return' => 'modified_id'])); + } + /** * Set up membership. * + * @param int|null $modifiedID + * * @return array * * @throws \CRM_Core_Exception */ - private function setupMembership(): array { + private function setupMembership($modifiedID = NULL): array { $contactID = $this->individualCreate(); + $modifiedID = $modifiedID ?? $contactID; $params = [ 'contact_id' => $contactID, @@ -162,10 +175,11 @@ class CRM_Member_BAO_MembershipLogTest extends CiviUnitTestCase { 'source' => 'Payment', 'is_override' => 1, 'status_id' => $this->membershipStatusID, + 'modified_id' => $modifiedID, ]; $membershipID = $this->callAPISuccess('Membership', 'create', $params)['id']; - $this->assertEquals($contactID, CRM_Core_DAO::singleValueQuery( + $this->assertEquals($modifiedID, CRM_Core_DAO::singleValueQuery( 'SELECT modified_id FROM civicrm_membership_log WHERE membership_id = %1', [1 => [$membershipID, 'Integer']] )); -- 2.25.1