From 0f0b7485d3286ce6d7b8898b7af4cb5a2b2d5e06 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 22 Nov 2020 13:57:43 +1300 Subject: [PATCH] Add test demonstrating that an extraneous activity is being created This test checks that 1 & only 1 activity is created when a membership is cancelled because the related contribution is cancelled. It is currently failing as there is an extra 'status changed from cancelled to cancelled' activity created. The fix is simple but not included as https://github.com/civicrm/civicrm-core/pull/18997 needs to be merged first --- CRM/Contribute/BAO/Contribution.php | 14 ------ .../tests/phpunit/CancelTest.php | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 1fc7d212fa..036c951301 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -952,24 +952,10 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { if ($membership && $update) { $newStatus = array_search('Cancelled', $membershipStatuses); - // Create activity - $allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get'); - $activityParam = [ - 'subject' => "Status changed from {$allStatus[$membership->status_id]} to {$allStatus[$newStatus]}", - 'source_contact_id' => CRM_Core_Session::singleton()->get('userID'), - 'target_contact_id' => $membership->contact_id, - 'source_record_id' => $membership->id, - 'activity_type_id' => 'Change Membership Status', - 'status_id' => 'Completed', - 'priority_id' => 'Normal', - 'activity_date_time' => 'now', - ]; - $membership->status_id = $newStatus; $membership->is_override = TRUE; $membership->status_override_end_date = 'null'; $membership->save(); - civicrm_api3('activity', 'create', $activityParam); } } } diff --git a/ext/contributioncancelactions/tests/phpunit/CancelTest.php b/ext/contributioncancelactions/tests/phpunit/CancelTest.php index 946b3484d4..567da3f45c 100644 --- a/ext/contributioncancelactions/tests/phpunit/CancelTest.php +++ b/ext/contributioncancelactions/tests/phpunit/CancelTest.php @@ -1,5 +1,7 @@ assertEquals('', $afterPledge['pledge_total_paid']); } + /** + * Test cancelling a contribution with a membership on the contribution edit + * form. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testCancelFromContributionForm(): void { + $this->createContact(); + $this->createMembershipType(); + $this->createMembershipOrder(); + $this->createLoggedInUser(); + $formValues = [ + 'id' => $this->ids['Contribution'][0], + 'contact_id' => $this->ids['contact'][0], + 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Cancelled'), + ]; + $form = new CRM_Contribute_Form_Contribution(); + $_SERVER['REQUEST_METHOD'] = 'GET'; + $form->controller = new CRM_Core_Controller(); + $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller)); + $form->testSubmit($formValues, CRM_Core_Action::UPDATE); + + $contribution = Contribution::get() + ->addWhere('id', '=', $this->ids['Contribution'][0]) + ->addSelect('contribution_status_id:name') + ->execute()->first(); + $this->assertEquals('Cancelled', $contribution['contribution_status_id:name']); + $membership = $this->callAPISuccessGetSingle('Membership', []); + $this->assertEquals('Cancelled', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id'])); + $activity = Activity::get() + ->addSelect('subject', 'source_record_id', 'status_id') + ->addWhere('activity_type_id:name', '=', 'Change Membership Status') + ->execute(); + $this->assertCount(1, $activity); + $this->assertEquals('Status changed from Pending to Cancelled', $activity->first()['subject']); + } + /** * Create an event and an order for a participant in that event. * -- 2.25.1