From 5266bd48c0dab5433e3091001ea0ffe47c0888dd Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 1 Nov 2019 13:35:40 +1300 Subject: [PATCH] [NFC] test cleanup I failed to replicate what I was trying to replicate but adding the test cleanup for now --- .../Financial/BAO/FinancialAccountTest.php | 2 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 108 ++++++++++-------- tests/phpunit/api/v3/OrderTest.php | 2 +- tests/phpunit/api/v3/PaymentTest.php | 40 +++++-- 4 files changed, 93 insertions(+), 59 deletions(-) diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index a3c0fd2b3e..2e7e7c3bb9 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -262,7 +262,7 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase { */ public function testValidateFinancialAccount() { // Create a record with financial item having financial account as Event Fee. - $this->createParticipantWithContribution(); + $this->createPartiallyPaidParticipantOrder(); $financialAccounts = CRM_Contribute_PseudoConstant::financialAccount(); $financialAccountId = array_search('Event Fee', $financialAccounts); $message = CRM_Financial_BAO_FinancialAccount::validateFinancialAccount($financialAccountId); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index e9404e0688..d83e6ce442 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -2629,55 +2629,9 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * * @throws \CRM_Core_Exception */ - protected function createParticipantWithContribution() { - // creating price set, price field - $this->_contactId = $this->individualCreate(); - $event = $this->eventCreate(); - $this->_eventId = $event['id']; - $eventParams = [ - 'id' => $this->_eventId, - 'financial_type_id' => 4, - 'is_monetary' => 1, - ]; - $this->callAPISuccess('event', 'create', $eventParams); - $priceFields = $this->createPriceSet('event', $this->_eventId); - $participantParams = [ - 'financial_type_id' => 4, - 'event_id' => $this->_eventId, - 'role_id' => 1, - 'status_id' => 14, - 'fee_currency' => 'USD', - 'contact_id' => $this->_contactId, - ]; - $participant = $this->callAPISuccess('Participant', 'create', $participantParams); - $orderParams = [ - 'total_amount' => 300, - 'currency' => 'USD', - 'contact_id' => $this->_contactId, - 'financial_type_id' => 4, - 'contribution_status_id' => 'Pending', - 'contribution_mode' => 'participant', - 'participant_id' => $participant['id'], - 'api.Payment.create' => ['total_amount' => 150], - ]; - foreach ($priceFields['values'] as $key => $priceField) { - $orderParams['line_items'][] = [ - 'line_item' => [ - [ - 'price_field_id' => $priceField['price_field_id'], - 'price_field_value_id' => $priceField['id'], - 'label' => $priceField['label'], - 'field_title' => $priceField['label'], - 'qty' => 1, - 'unit_price' => $priceField['amount'], - 'line_total' => $priceField['amount'], - 'financial_type_id' => $priceField['financial_type_id'], - 'entity_table' => 'civicrm_participant', - ], - ], - 'params' => $participant, - ]; - } + protected function createPartiallyPaidParticipantOrder() { + $orderParams = $this->getParticipantOrderParams(); + $orderParams['api.Payment.create'] = ['total_amount' => 150]; return $this->callAPISuccess('Order', 'create', $orderParams); } @@ -3350,4 +3304,60 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) CRM_Core_DAO::executeQuery("UPDATE civicrm_option_value SET label = REPLACE(name, ' Label**', '') WHERE label LIKE '% Label**'"); } + /** + * Get parameters to set up a multi-line participant order. + * + * @return array + * @throws \CRM_Core_Exception + */ + protected function getParticipantOrderParams(): array { + $this->_contactId = $this->individualCreate(); + $event = $this->eventCreate(); + $this->_eventId = $event['id']; + $eventParams = [ + 'id' => $this->_eventId, + 'financial_type_id' => 4, + 'is_monetary' => 1, + ]; + $this->callAPISuccess('event', 'create', $eventParams); + $priceFields = $this->createPriceSet('event', $this->_eventId); + $participantParams = [ + 'financial_type_id' => 4, + 'event_id' => $this->_eventId, + 'role_id' => 1, + 'status_id' => 14, + 'fee_currency' => 'USD', + 'contact_id' => $this->_contactId, + ]; + $participant = $this->callAPISuccess('Participant', 'create', $participantParams); + $orderParams = [ + 'total_amount' => 300, + 'currency' => 'USD', + 'contact_id' => $this->_contactId, + 'financial_type_id' => 4, + 'contribution_status_id' => 'Pending', + 'contribution_mode' => 'participant', + 'participant_id' => $participant['id'], + ]; + foreach ($priceFields['values'] as $key => $priceField) { + $orderParams['line_items'][] = [ + 'line_item' => [ + [ + 'price_field_id' => $priceField['price_field_id'], + 'price_field_value_id' => $priceField['id'], + 'label' => $priceField['label'], + 'field_title' => $priceField['label'], + 'qty' => 1, + 'unit_price' => $priceField['amount'], + 'line_total' => $priceField['amount'], + 'financial_type_id' => $priceField['financial_type_id'], + 'entity_table' => 'civicrm_participant', + ], + ], + 'params' => $participant, + ]; + } + return $orderParams; + } + } diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index 1abf849ecd..1ee4f3d6e0 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -102,7 +102,7 @@ class api_v3_OrderTest extends CiviUnitTestCase { */ public function testGetOrderParticipant() { $this->addOrder(FALSE, 100); - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $params = [ 'contribution_id' => $contribution['id'], diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index 472f6a0b56..66de216a2e 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -158,7 +158,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { */ public function testPaymentEmailReceipt() { $mut = new CiviMailUtils($this); - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $event = $this->callAPISuccess('Event', 'get', []); $this->addLocationToEvent($event['id']); $params = [ @@ -206,7 +206,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { public function testPaymentEmailReceiptFullyPaid() { $mut = new CiviMailUtils($this); CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviContribute', 'edit contributions', 'access CiviCRM']; - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $params = [ 'contribution_id' => $contribution['id'], @@ -242,7 +242,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $this->setCurrencySeparators($thousandSeparator); $decimalSeparator = ($thousandSeparator === ',' ? '.' : ','); $mut = new CiviMailUtils($this); - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $this->callAPISuccess('payment', 'create', [ 'contribution_id' => $contribution['id'], 'total_amount' => 50, @@ -284,13 +284,37 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $mut->clearMessages(); } + /** + * Test adding a payment to a pending multi-line order. + * + * @throws \CRM_Core_Exception + */ + public function testCreatePaymentPendingOrderNoLineItems() { + $order = $this->createPendingParticipantOrder(); + $this->callAPISuccess('Payment', 'create', [ + 'order_id' => $order['id'], + 'total_amount' => 50, + ]); + } + + /** + * Add participant with contribution + * + * @return array + * + * @throws \CRM_Core_Exception + */ + protected function createPendingParticipantOrder() { + return $this->callAPISuccess('Order', 'create', $this->getParticipantOrderParams()); + } + /** * Test create payment api with no line item in params * * @throws \CRM_Core_Exception */ public function testCreatePaymentNoLineItems() { - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); //Create partial payment $params = [ @@ -387,7 +411,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { * Test create payment api with line item in params */ public function testCreatePaymentLineItems() { - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]); //Create partial payment by passing line item array is params @@ -482,7 +506,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { */ public function testCancelPayment() { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute']; - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $params = [ 'contribution_id' => $contribution['id'], @@ -518,7 +542,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { */ public function testDeletePayment() { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute']; - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); $params = [ 'contribution_id' => $contribution['id'], @@ -572,7 +596,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase { */ public function testUpdatePayment() { CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions']; - $contribution = $this->createParticipantWithContribution(); + $contribution = $this->createPartiallyPaidParticipantOrder(); //Create partial payment by passing line item array is params $params = [ -- 2.25.1