From b6bcfdda2c86efc5c78e2cb114bded950af0806a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 16 Sep 2022 10:12:48 +1200 Subject: [PATCH] Remove call to deprecated CRM_Contribute_PseudoConstant::contributionStatus --- CRM/Member/BAO/Membership.php | 31 +++++++++---------- CRM/Member/Form/Membership.php | 4 +-- CRM/Member/Form/MembershipView.php | 2 +- CRM/Member/Page/Tab.php | 2 +- CRM/Member/Selector/Search.php | 2 +- .../CRM/Member/Form/MembershipTest.php | 18 +++++------ 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 40e6d0a5f071..82ae77c05d30 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -10,6 +10,7 @@ */ use Civi\API\Exception\UnauthorizedException; +use Civi\Api4\Membership; use Civi\Api4\MembershipType; /** @@ -1633,7 +1634,7 @@ public static function isCancelSubscriptionSupported($mid, $isNotCancelled = TRU $isCancelled = FALSE; if ($isNotCancelled) { - $isCancelled = self::isSubscriptionCancelled($mid); + $isCancelled = self::isSubscriptionCancelled((int) $mid); } $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($mid, 'membership', 'obj'); @@ -1647,25 +1648,21 @@ public static function isCancelSubscriptionSupported($mid, $isNotCancelled = TRU /** * Check whether subscription is already cancelled. * - * @param int $mid + * @param int $membershipID * Membership id. * - * @return string + * @return bool * contribution status + * + * @throws \CRM_Core_Exception */ - public static function isSubscriptionCancelled($mid) { - $sql = " - SELECT cr.contribution_status_id - FROM civicrm_contribution_recur cr -LEFT JOIN civicrm_membership mem ON ( cr.id = mem.contribution_recur_id ) - WHERE mem.id = %1 LIMIT 1"; - $params = [1 => [$mid, 'Integer']]; - $statusId = CRM_Core_DAO::singleValueQuery($sql, $params); - $status = CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name'); - if ($status == 'Cancelled') { - return TRUE; - } - return FALSE; + public static function isSubscriptionCancelled(int $membershipID): bool { + // Check permissions set to false 'in case' - ideally would check permissions are + // correct & remove. + return (bool) Membership::get(FALSE) + ->addWhere('id', '=', $membershipID) + ->addWhere('contribution_recur_id.contribution_status_id:name', '=', 'Cancelled') + ->selectRowCount()->execute()->count(); } /** @@ -2379,7 +2376,7 @@ public static function getAllContactMembership($contactID, $isTest = FALSE, $onl return $contactMembershipType; } - $membershipQuery = \Civi\Api4\Membership::get(FALSE) + $membershipQuery = Membership::get(FALSE) ->addWhere('contact_id', '=', $contactID) ->addWhere('status_id:name', '<>', 'Pending') ->addWhere('is_test', '=', $isTest) diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 60e1d4591223..ce76f3c0963a 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -354,7 +354,7 @@ public function setDefaultValues() { $subscriptionCancelled = FALSE; if (!empty($defaults['id'])) { - $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->_id); + $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled((int) $this->_id); } $alreadyAutoRenew = FALSE; @@ -1568,7 +1568,7 @@ protected function isUpdateToExistingRecurringMembership() { if ($this->_action & CRM_Core_Action::UPDATE && CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->getEntityId(), 'contribution_recur_id') - && !CRM_Member_BAO_Membership::isSubscriptionCancelled($this->getEntityId())) { + && !CRM_Member_BAO_Membership::isSubscriptionCancelled((int) $this->getEntityId())) { $isRecur = TRUE; } diff --git a/CRM/Member/Form/MembershipView.php b/CRM/Member/Form/MembershipView.php index a8af679e49da..2f1f7aa72112 100644 --- a/CRM/Member/Form/MembershipView.php +++ b/CRM/Member/Form/MembershipView.php @@ -400,7 +400,7 @@ public function preProcess() { $values['membership_type'] = CRM_Core_TestEntity::appendTestText($values['membership_type']); } - $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled($this->membershipID); + $subscriptionCancelled = CRM_Member_BAO_Membership::isSubscriptionCancelled((int) $this->membershipID); $values['auto_renew'] = ($autoRenew && !$subscriptionCancelled) ? 'Yes' : 'No'; //do check for campaigns diff --git a/CRM/Member/Page/Tab.php b/CRM/Member/Page/Tab.php index 5878ff8d1d5d..94845158c09b 100644 --- a/CRM/Member/Page/Tab.php +++ b/CRM/Member/Page/Tab.php @@ -167,7 +167,7 @@ public function browse() { // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error if (!empty($membership[$dao->id]['contribution_recur_id'])) { - if (CRM_Member_BAO_Membership::isSubscriptionCancelled($membership[$dao->id]['membership_id'])) { + if (CRM_Member_BAO_Membership::isSubscriptionCancelled((int) $membership[$dao->id]['membership_id'])) { $membership[$dao->id]['auto_renew'] = 2; } else { diff --git a/CRM/Member/Selector/Search.php b/CRM/Member/Selector/Search.php index f31c34d0f822..3505c836bbc9 100644 --- a/CRM/Member/Selector/Search.php +++ b/CRM/Member/Selector/Search.php @@ -423,7 +423,7 @@ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { // Display Auto-renew status on page (0=disabled, 1=enabled, 2=enabled, but error if (!empty($result->membership_recur_id)) { - if (CRM_Member_BAO_Membership::isSubscriptionCancelled($row['membership_id'])) { + if (CRM_Member_BAO_Membership::isSubscriptionCancelled((int) $row['membership_id'])) { $row['auto_renew'] = 2; } else { diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index cf247bb0274b..06d9db22cb6b 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1605,14 +1605,12 @@ public function testCreatePendingWithMultipleTerms() { * is other contact. * * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception - * @throws \Exception */ - public function testMembershipViewContributionOwnerDifferent() { + public function testMembershipViewContributionOwnerDifferent(): void { // Membership Owner $contactId1 = $this->individualCreate(); - // Contribution Onwer + // Contribution Owner $contactId2 = $this->individualCreate(); // create new membership type @@ -1637,24 +1635,24 @@ public function testMembershipViewContributionOwnerDifferent() { ]); // 1st Payment - $contriParams = [ + $contributionParams = [ 'membership_id' => $membershipId, 'total_amount' => 25, 'financial_type_id' => 2, 'contact_id' => $contactId2, 'receive_date' => '2020-08-08', ]; - $contribution1 = CRM_Member_BAO_Membership::recordMembershipContribution($contriParams); + $contribution1 = CRM_Member_BAO_Membership::recordMembershipContribution($contributionParams); // 2nd Payment - $contriParams = [ + $contributionParams = [ 'membership_id' => $membershipId, 'total_amount' => 25, 'financial_type_id' => 2, 'contact_id' => $contactId2, 'receive_date' => '2020-07-08', ]; - $contribution2 = CRM_Member_BAO_Membership::recordMembershipContribution($contriParams); + $contribution2 = CRM_Member_BAO_Membership::recordMembershipContribution($contributionParams); // View Membership record $membershipViewForm = new CRM_Member_Form_MembershipView(); @@ -1664,7 +1662,7 @@ public function testMembershipViewContributionOwnerDifferent() { $membershipViewForm->controller->setEmbedded(TRUE); $membershipViewForm->preProcess(); - // get contribution rows related to membership payments + // Get contribution rows related to membership payments. $templateVar = $membershipViewForm::getTemplate()->get_template_vars('rows'); $this->assertEquals($templateVar[0]['contribution_id'], $contribution1->id); @@ -1672,7 +1670,7 @@ public function testMembershipViewContributionOwnerDifferent() { $this->assertEquals($templateVar[1]['contribution_id'], $contribution2->id); $this->assertEquals($templateVar[1]['contact_id'], $contactId2); - $this->assertEquals(count($templateVar), 2); + $this->assertCount(2, $templateVar); } }