From d0e893849a70eb69dade148054b29011dfca7d0e 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 | 21 ++++++------- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 7d85c1868cf2..fba0fbe182b5 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; /** @@ -1631,7 +1632,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'); @@ -1645,25 +1646,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(); } /** @@ -2374,7 +2371,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 4e193c7928a7..f4a4e7462a13 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -353,7 +353,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; @@ -1562,7 +1562,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 5f6b4fc0e3b7..fd0e1620d038 100644 --- a/CRM/Member/Form/MembershipView.php +++ b/CRM/Member/Form/MembershipView.php @@ -399,7 +399,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 14d5337ca37b..2433005403ac 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 f912a9a1815e..85eef823345d 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1583,15 +1583,12 @@ public function testCreatePendingWithMultipleTerms() { /** * Test Membership Payment owned by other contact, membership view should show all contribution records in listing. * is other contact. - * - * @throws \CRM_Core_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 @@ -1608,7 +1605,7 @@ public function testMembershipViewContributionOwnerDifferent() { 'financial_type_id' => 2, ]); - // create Membership + // Create Membership. $membershipId = $this->contactMembershipCreate([ 'contact_id' => $contactId1, 'membership_type_id' => $membershipTypeAnnualFixed['id'], @@ -1616,24 +1613,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(); @@ -1643,7 +1640,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); @@ -1651,7 +1648,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); } }