Skip to content

Commit

Permalink
Remove call to deprecated CRM_Contribute_PseudoConstant::contribution…
Browse files Browse the repository at this point in the history
…Status
  • Loading branch information
eileenmcnaughton committed Sep 15, 2022
1 parent 40391ad commit b6bcfdd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
31 changes: 14 additions & 17 deletions CRM/Member/BAO/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use Civi\API\Exception\UnauthorizedException;
use Civi\Api4\Membership;
use Civi\Api4\MembershipType;

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/MembershipView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Page/Tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Selector/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 8 additions & 10 deletions tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -1664,15 +1662,15 @@ 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);
$this->assertEquals($templateVar[0]['contact_id'], $contactId2);

$this->assertEquals($templateVar[1]['contribution_id'], $contribution2->id);
$this->assertEquals($templateVar[1]['contact_id'], $contactId2);
$this->assertEquals(count($templateVar), 2);
$this->assertCount(2, $templateVar);
}

}

0 comments on commit b6bcfdd

Please sign in to comment.