From da610957e921f3c3908bd1da2047ba84c5f936d7 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 19 Jun 2021 15:22:55 +1200 Subject: [PATCH] Fix transfer registration to transfer participant_payment row --- CRM/Event/Form/SelfSvcTransfer.php | 12 +++++++++--- .../phpunit/CRM/Event/Form/ParticipantTest.php | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CRM/Event/Form/SelfSvcTransfer.php b/CRM/Event/Form/SelfSvcTransfer.php index 1d5ac36d72..586a7b2262 100644 --- a/CRM/Event/Form/SelfSvcTransfer.php +++ b/CRM/Event/Form/SelfSvcTransfer.php @@ -513,14 +513,19 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form { ->addWhere('id', '=', $fromParticipantID) ->execute() ->first(); - + $participantPayments = civicrm_api3('ParticipantPayment', 'get', [ + 'return' => 'id', + 'participant_id' => $fromParticipantID, + ])['values']; unset($toParticipantValues['id']); $toParticipantValues['contact_id'] = $toContactID; $toParticipantValues['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Registered'); $toParticipantValues['register_date'] = date("Y-m-d"); //first create the new participant row -don't set registered_by yet or email won't be sent $participant = CRM_Event_BAO_Participant::create($toParticipantValues); - + foreach ($participantPayments as $payment) { + civicrm_api3('ParticipantPayment', 'create', ['id' => $payment['id'], 'participant_id' => $participant->id]); + } //send a confirmation email to the new participant $this->participantTransfer($participant); //now update registered_by_id @@ -531,7 +536,8 @@ class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form { $line_items = CRM_Price_BAO_LineItem::getLineItems($fromParticipantID); foreach ($line_items as $id => $item) { //Remove contribution id from older participant line item. - CRM_Core_DAO::singleValueQuery("UPDATE civicrm_line_item SET contribution_id = NULL WHERE id = %1", [1 => [$id, 'Integer']]); + CRM_Core_DAO::singleValueQuery('UPDATE civicrm_line_item SET contribution_id = NULL WHERE id = %1', [1 => [$id, 'Integer']]); + $item['entity_id'] = $participant->id; $item['id'] = NULL; $item['entity_table'] = "civicrm_participant"; diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index bac093c808..2d93dffbca 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -8,6 +8,7 @@ */ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { + use CRMTraits_Financial_OrderTrait; /** * Options on the from Email address array. * @@ -824,13 +825,13 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - public function testTransferParticipantRegistration() { + public function testTransferParticipantRegistration(): void { //Register a contact to a sample event. - $this->createParticipantRecordsFromTwoFieldPriceSet(); - $contribution = $this->callAPISuccessGetSingle('Contribution', []); + $this->createEventOrder(); + $contribution = $this->callAPISuccessGetSingle('Contribution', ['return' => 'id']); //Check line item count of the contribution id before transfer. $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']); - $this->assertEquals(count($lineItems), 2); + $this->assertCount(2, $lineItems); $participantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $contribution['id'], 'participant_id', 'contribution_id'); /* @var CRM_Event_Form_SelfSvcTransfer $form */ $form = $this->getFormObject('CRM_Event_Form_SelfSvcTransfer'); @@ -839,7 +840,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { //Assert participant is transferred to $toContactId. $participant = $this->callAPISuccess('Participant', 'getsingle', [ - 'return' => ["transferred_to_contact_id"], + 'return' => ['transferred_to_contact_id'], 'id' => $participantId, ]); $this->assertEquals($participant['transferred_to_contact_id'], $toContactId); @@ -852,7 +853,12 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { //Check line item count of the contribution id remains the same. $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']); - $this->assertEquals(count($lineItems), 2); + $this->assertCount(2, $lineItems); + // There should be 2 participant payments on the contribution & 0 others existing. + $this->callAPISuccessGetCount('ParticipantPayment', ['contribution_id' => $contribution['id']], 2); + $this->callAPISuccessGetCount('ParticipantPayment', [], 2); + $this->callAPISuccessGetCount('ParticipantPayment', ['participant_id' => $toParticipant['id']], 1); + $this->callAPISuccessGetCount('ParticipantPayment', ['participant_id' => $participantId], 0); } /** -- 2.25.1