From 80a680f908af72c9b0fa841ac4db10ef0b77901f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 22 Aug 2022 13:06:31 +1200 Subject: [PATCH] Move participant cancel tests to extension --- .../tests/phpunit/CancelTest.php | 51 ++++++++++-- .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 78 +------------------ 2 files changed, 47 insertions(+), 82 deletions(-) diff --git a/ext/contributioncancelactions/tests/phpunit/CancelTest.php b/ext/contributioncancelactions/tests/phpunit/CancelTest.php index c1ca5f4df3..573aacb3b4 100644 --- a/ext/contributioncancelactions/tests/phpunit/CancelTest.php +++ b/ext/contributioncancelactions/tests/phpunit/CancelTest.php @@ -241,16 +241,48 @@ class CancelTest extends TestCase implements HeadlessInterface, HookInterface, T } /** - * Test cancel order api + * Test cancel order api. + * + * @dataProvider getStatuses * @throws API_Exception */ - public function testCancelOrderWithParticipant(): void { + public function testCancelOrderWithParticipant($status): void { $this->createContact(); $orderID = $this->createEventOrder(); - $this->callAPISuccess('Order', 'cancel', ['contribution_id' => $orderID]); + $participantID = Participant::get()->addSelect('id')->execute()->first()['id']; + $additionalParticipantID = Participant::create()->setValues([ + 'event_id' => $this->getEventID(), + 'contact_id' => $this->individualCreate(), + 'registered_by_id' => $participantID, + 'status_id:name' => 'Pending from incomplete transaction', + ])->execute()->first()['id']; + if ($status === 'Cancelled') { + $this->callAPISuccess('Order', 'cancel', ['contribution_id' => $orderID]); + } + else { + Contribution::update()->setValues(['contribution_status_id:name' => $status])->addWhere('id', '=', $orderID)->execute(); + } $this->callAPISuccess('Order', 'get', ['contribution_id' => $orderID]); - $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => 'Cancelled']); - $this->callAPISuccessGetCount('Participant', ['status_id' => 'Cancelled'], 1); + $this->callAPISuccessGetSingle('Contribution', ['contribution_status_id' => $status]); + $this->callAPISuccessGetCount('Participant', ['status_id' => 'Cancelled'], 2); + + $cancelledActivatesCount = civicrm_api3('Activity', 'get', [ + 'sequential' => 1, + 'activity_type_id' => 'Event Registration', + 'subject' => ['LIKE' => '%Cancelled%'], + 'source_record_id' => ['IN' => [$participantID, $additionalParticipantID]], + ]); + + $this->assertEquals(2, $cancelledActivatesCount['count']); + } + + /** + * Get statuses that result in cancellation. + * + * @return \string[][] + */ + public function getStatuses():array { + return [['Cancelled'], ['Failed']]; } /** @@ -301,6 +333,15 @@ class CancelTest extends TestCase implements HeadlessInterface, HookInterface, T $this->assertEquals('Status changed from Pending to Cancelled', $activity->first()['subject']); } + /** + * Get the event ID. + * + * @return int + */ + protected function getEventID(): int { + return $this->ids['event'][0]; + } + /** * Create an event and an order for a participant in that event. * diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index a68f1c7002..a55e6d7ff3 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -256,88 +256,12 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { $this->assertStringContainsString('Contribution Information', $msg['html']); } - /** - * @throws \API_Exception - * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - * @throws \CiviCRM_API3_Exception - */ - public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities(): void { - // Test fails - reason not yet investigated. - $this->isValidateFinancialsOnPostAssert = FALSE; - $this->_setUpParticipantObjects('Pending from incomplete transaction'); - $additionalParticipantId = $this->participantCreate([ - 'event_id' => $this->_eventId, - 'registered_by_id' => $this->_participantId, - 'status_id' => 'Pending from incomplete transaction', - ]); - - Contribution::update(FALSE)->setValues([ - 'cancel_date' => 'now', - 'contribution_status_id:name' => 'Cancelled', - ])->addWhere('id', '=', $this->_contributionId)->execute(); - - $cancelledParticipantsCount = $this->callAPISuccess('Participant', 'get', [ - 'sequential' => 1, - 'id' => ['IN' => [$this->_participantId, $additionalParticipantId]], - 'status_id' => 'Cancelled', - ])['count']; - $this->assertEquals(2, $cancelledParticipantsCount); - - $cancelledActivatesCount = civicrm_api3('Activity', 'get', [ - 'sequential' => 1, - 'activity_type_id' => 'Event Registration', - 'subject' => ['LIKE' => '%Cancelled%'], - 'source_record_id' => ['IN' => [$this->_participantId, $additionalParticipantId]], - ]); - - $this->assertEquals(2, $cancelledActivatesCount['count']); - } - - /** - * Test that related pending participant records are cancelled. - * - * @throws \API_Exception - * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception - * @throws \Civi\API\Exception\UnauthorizedException - */ - public function testThatFailedEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities(): void { - $this->_setUpParticipantObjects('Pending from incomplete transaction'); - $additionalParticipantId = $this->participantCreate([ - 'event_id' => $this->_eventId, - 'registered_by_id' => $this->_participantId, - 'status_id' => 'Pending from incomplete transaction', - ]); - - Contribution::update(FALSE)->setValues([ - 'cancel_date' => 'now', - 'contribution_status_id:name' => 'Failed', - ])->addWhere('id', '=', $this->ids['contribution'])->execute(); - - $cancelledParticipantsCount = civicrm_api3('Participant', 'get', [ - 'sequential' => 1, - 'id' => ['IN' => [$this->_participantId, $additionalParticipantId]], - 'status_id' => 'Cancelled', - ])['count']; - $this->assertEquals(2, $cancelledParticipantsCount); - - $cancelledActivatesCount = civicrm_api3('Activity', 'get', [ - 'sequential' => 1, - 'activity_type_id' => 'Event Registration', - 'subject' => ['LIKE' => '%Cancelled%'], - 'source_record_id' => ['IN' => [$this->_participantId, $additionalParticipantId]], - ]); - - $this->assertEquals(2, $cancelledActivatesCount['count']); - } - /** * Prepare for contribution Test - involving only contribution objects * * @param bool $contributionPage */ - public function _setUpContributionObjects($contributionPage = FALSE) { + public function _setUpContributionObjects($contributionPage = FALSE): void { $contribution = new CRM_Contribute_BAO_Contribution(); $contribution->id = $this->_contributionId; -- 2.25.1