From 5f5e7a766e4b54a52462542046baf7f960c49e6e Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Wed, 11 Jul 2018 15:39:00 +0100 Subject: [PATCH] dev/core#253: Cancelling or An Error during event registration payment should cancel all additional participates --- CRM/Core/Payment/BaseIPN.php | 18 +-- .../phpunit/CRM/Core/Payment/BaseIPNTest.php | 139 ++++++++---------- 2 files changed, 68 insertions(+), 89 deletions(-) diff --git a/CRM/Core/Payment/BaseIPN.php b/CRM/Core/Payment/BaseIPN.php index 96cdc87698..1829889ab6 100644 --- a/CRM/Core/Payment/BaseIPN.php +++ b/CRM/Core/Payment/BaseIPN.php @@ -273,12 +273,9 @@ class CRM_Core_Payment_BaseIPN { } if ($participant) { - $participantStatuses = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'status_id', array( - 'labelColumn' => 'name', - 'flip' => 1, - )); - $participant->status_id = $participantStatuses['Cancelled']; - $participant->save(); + $participantParams['id'] = $participant->id; + $participantParams['status_id'] = 'Cancelled'; + civicrm_api3('Participant', 'create', $participantParams); } } @@ -370,12 +367,9 @@ class CRM_Core_Payment_BaseIPN { } if ($participant) { - $participantStatuses = CRM_Core_PseudoConstant::get('CRM_Event_DAO_Participant', 'status_id', array( - 'labelColumn' => 'name', - 'flip' => 1, - )); - $participant->status_id = $participantStatuses['Cancelled']; - $participant->save(); + $participantParams['id'] = $participant->id; + $participantParams['status_id'] = 'Cancelled'; + civicrm_api3('Participant', 'create', $participantParams); } } $transaction->commit(); diff --git a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php index 821c75d0cf..51f3f7b191 100644 --- a/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/BaseIPNTest.php @@ -411,82 +411,63 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { $this->assertFalse(is_array($result)); } - /* @codingStandardsIgnoreStart - * Test calls main functions in sequence per 'main' - I had hoped to test the functions more - * fully but the calls to the POST happen in more than one function - * keeping this as good example of data to bring back to life later - - public function testMainFunctionActions() { - $ids = $objects = array( ); - $input['component'] = 'Contribute'; - $postedParams = array( - 'x_response_code' => 1, - 'x_response_reason_code' => 1, - 'x_response_reason_text' => 'This transaction has been approved.', - 'x_avs_code' => 'Y', - 'x_auth_code' => 140454, - 'x_trans_id' => 4353599599, - 'x_method' => 'CC', - 'x_card_type' => 'American Express', - 'x_account_number' => 'XXXX2701', - 'x_first_name' => 'Arthur', - 'x_last_name' => 'Jacobs', - 'x_company' => null, - 'x_address' => '866 2166th St SN', - 'x_city' => 'Edwardstown', - 'x_state' => 'WA', - 'x_zip' => 98026, - 'x_country' => 'US', - 'x_phone' => null, - 'x_fax' => null, - 'x_email' => null, - 'x_invoice_num' => 'a9fb56c24576lk4c9490f6', - 'x_description' => 'my desc', - 'x_type' => 'auth_capture', - 'x_cust_id' => 5191, - 'x_ship_to_first_name' => null, - 'x_ship_to_last_name' => null, - 'x_ship_to_company' => null, - 'x_ship_to_address' => null, - 'x_ship_to_city' => null, - 'x_ship_to_state' => null, - 'x_ship_to_zip' => null, - 'x_ship_to_country' => null, - 'x_amount' => 60.00, - 'x_tax' => 0.00, - 'x_duty' => 0.00, - 'x_freight' => 0.00, - 'x_tax_exempt' => FALSE, - 'x_po_num' => null, - 'x_MD5_Hash' => '069ECAD13C8E15AC205CDF92B8B58CC7', - 'x_cvv2_resp_code' => null, - 'x_cavv_response' => null, - 'x_test_request' => false, - 'description' => 'my description' - ); - $this->IPN->getInput( $input, $ids ); - $this->IPN->getIDs( $ids, $input ); - - CRM_Core_Error::debug_var( '$ids', $ids ); - CRM_Core_Error::debug_var( '$input', $input ); - - $paymentProcessorID = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_PaymentProcessorType', - 'AuthNet', 'id', 'name' ); - - if ( ! $this->IPN->validateData( $input, $ids, $objects, true, $paymentProcessorID ) ) { - return false; - } - - if ( $component == 'contribute' && $ids['contributionRecur'] ) { - // check if first contribution is completed, else complete first contribution - $first = true; - if ( $objects['contribution']->contribution_status_id == 1 ) { - $first = false; - } - return $this->IPN->recur( $input, $ids, $objects, $first ); - } - } - @codingStandardsIgnoreEnd */ + public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() { + $this->_setUpParticipantObjects('Pending from incomplete transaction'); + $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); + $additionalParticipantId = $this->participantCreate(array( + 'event_id' => $this->_eventId, + 'registered_by_id' => $this->_participantId, + 'status_id' => 'Pending from incomplete transaction', + )); + + $transaction = new CRM_Core_Transaction(); + $this->IPN->cancelled($this->objects, $transaction); + + $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']); + } + + public function testThatFailedEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() { + $this->_setUpParticipantObjects('Pending from incomplete transaction'); + $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); + $additionalParticipantId = $this->participantCreate(array( + 'event_id' => $this->_eventId, + 'registered_by_id' => $this->_participantId, + 'status_id' => 'Pending from incomplete transaction', + )); + + $transaction = new CRM_Core_Transaction(); + $this->IPN->failed($this->objects, $transaction); + + $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 @@ -606,14 +587,18 @@ class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { /** * Set up participant requirements for test. + * + * @param string $participantStatus + * The participant to create status */ - public function _setUpParticipantObjects() { + public function _setUpParticipantObjects($participantStatus = 'Attended') { $event = $this->eventCreate(array('is_email_confirm' => 1)); $this->_eventId = $event['id']; $this->_participantId = $this->participantCreate(array( 'event_id' => $this->_eventId, 'contact_id' => $this->_contactId, + 'status_id' => $participantStatus, )); $this->callAPISuccess('participant_payment', 'create', array( -- 2.25.1