From db62fd2be1f166e616466176bd3ed857ebb08130 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 20 Jan 2016 16:07:08 +0530 Subject: [PATCH] CRM-16259, added api test for cancel action ---------------------------------------- * CRM-16259: Create Payment API https://issues.civicrm.org/jira/browse/CRM-16259 --- tests/phpunit/CiviTest/CiviUnitTestCase.php | 91 +++++++++++++++++++++ tests/phpunit/api/v3/PaymentTest.php | 30 +++++++ 2 files changed, 121 insertions(+) diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 374a2512de..5c880ccf26 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3447,4 +3447,95 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) )); } + /** + * Add participant with contribution + * + * @return array + */ + protected function createParticipantWithContribution() { + // creating price set, price field + require_once 'CiviTest/Event.php'; + require_once 'CiviTest/Contact.php'; + $this->_contactId = Contact::createIndividual(); + $this->_eventId = Event::create($this->_contactId); + $paramsSet['title'] = 'Price Set'; + $paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set'); + $paramsSet['is_active'] = TRUE; + $paramsSet['financial_type_id'] = 4; + $paramsSet['extends'] = 1; + $priceSet = $this->callAPISuccess('price_set', 'create', $paramsSet); + $priceSetId = $priceSet['id']; + //Checking for priceset added in the table. + $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title', + 'id', $paramsSet['title'], 'Check DB for created priceset' + ); + $paramsField = array( + 'label' => 'Price Field', + 'name' => CRM_Utils_String::titleToVar('Price Field'), + 'html_type' => 'CheckBox', + 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'), + 'option_value' => array('1' => 100, '2' => 200), + 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'), + 'option_weight' => array('1' => 1, '2' => 2), + 'option_amount' => array('1' => 100, '2' => 200), + 'is_display_amounts' => 1, + 'weight' => 1, + 'options_per_line' => 1, + 'is_active' => array('1' => 1, '2' => 1), + 'price_set_id' => $priceSet['id'], + 'is_enter_qty' => 1, + 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'), + ); + $priceField = CRM_Price_BAO_PriceField::create($paramsField); + $eventParams = array( + 'id' => $this->_eventId, + 'financial_type_id' => 4, + 'is_monetary' => 1, + ); + $this->callAPISuccess('event', 'create', $eventParams); + CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId); + $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id)); + $participantParams = array( + '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); + $contributionParams = array( + 'total_amount' => 150, + 'currency' => 'USD', + 'contact_id' => $this->_contactId, + 'financial_type_id' => 4, + 'contribution_status_id' => 1, + 'partial_payment_total' => 300.00, + 'partial_amount_pay' => 150, + 'contribution_mode' => 'participant', + 'participant_id' => $participant['id'], + ); + foreach ($priceFields['values'] as $key => $priceField) { + $lineItems[1][$key] = array( + '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'], + ); + } + $contributionParams['line_item'] = $lineItems; + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + $paymentParticipant = array( + 'participant_id' => $participant['id'], + 'contribution_id' => $contribution['id'], + ); + $ids = array(); + $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant); + return array($lineItems, $contribution); + } + } diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php index bb37907804..f56f8aaf08 100644 --- a/tests/phpunit/api/v3/PaymentTest.php +++ b/tests/phpunit/api/v3/PaymentTest.php @@ -315,4 +315,34 @@ class api_v3_PaymentTest extends CiviUnitTestCase { $this->_priceIds['price_field'] = $priceField['id']; } + /** + * Test cancel payment api + */ + public function testCancelPayment() { + list($lineItems, $contribution) = $this->createParticipantWithContribution(); + + $params = array( + 'contribution_id' => $contribution['id'], + ); + + $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__); + $this->assertEquals(1, $payment['count']); + + $cancelParams = array( + 'id' => $payment['id'], + ); + $this->callAPIAndDocument('payment', 'cancel', $cancelParams, __FUNCTION__, __FILE__); + + $payment = $this->callAPIAndDocument('payment', 'get', $params, __FUNCTION__, __FILE__); + $this->assertEquals(2, $payment['count']); + $amounts = array(-150.00, 150.00); + foreach($payment['values'] as $value) { + $this->assertEquals($value['total_amount'], array_pop($amounts), 'Mismatch total amount'); + } + + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + } -- 2.25.1