From 73c0e107070aba24531fc19bb91086003c8bf956 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Wed, 27 Jan 2016 02:01:47 +0530 Subject: [PATCH] CRM-16188, added order api test for get action ---------------------------------------- * CRM-16188: Create an order API https://issues.civicrm.org/jira/browse/CRM-16188 --- tests/phpunit/CiviTest/CiviUnitTestCase.php | 76 ++++++---- tests/phpunit/api/v3/OrderTest.php | 153 ++++++++++++++++++++ 2 files changed, 198 insertions(+), 31 deletions(-) create mode 100644 tests/phpunit/api/v3/OrderTest.php diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 5c880ccf26..958a2acb1e 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3458,43 +3458,13 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) 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)); + $priceFields = $this->createPriceSet('event', $this->_eventId); $participantParams = array( 'financial_type_id' => 4, 'event_id' => $this->_eventId, @@ -3538,4 +3508,48 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) return array($lineItems, $contribution); } + /** + * Create price set + * + * @param string $component + * @param int $componentId + * + * @return array + */ + protected function createPriceSet($component = 'contribution_page', $componentId = NULL) { + $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); + if ($componentId) { + CRM_Price_BAO_PriceSet::addTo('civicrm_' . $component, $componentId, $priceSetId); + } + return $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id)); + } + } diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php new file mode 100644 index 0000000000..dac7361282 --- /dev/null +++ b/tests/phpunit/api/v3/OrderTest.php @@ -0,0 +1,153 @@ +_apiversion = 3; + $this->_individualId = $this->individualCreate(); + } + + /** + * Clean up after each test. + */ + public function tearDown() { + $this->quickCleanUpFinancialEntities(); + $this->quickCleanup(array('civicrm_uf_match')); + } + + /** + * Test Get Payment api. + */ + public function testGetOrder() { + $contribution = $this->addOrder(FALSE, 100); + + $params = array( + 'contribution_id' => $contribution['id'], + ); + + $order = $this->callAPISuccess('Order', 'get', $params); + + $this->assertEquals(1, $order['count']); + $expectedResult = array($contribution['id'] => array( + 'total_amount' => 100, + 'contribution_id' => $contribution['id'], + 'contribution_status' => 'Completed', + 'net_amount' => 100, + )); + $lineItems[] = array( + 'entity_table' => 'civicrm_contribution', + 'entity_id' => $contribution['id'], + 'contribution_id' => $contribution['id'], + 'unit_price' => 100, + 'line_total' => 100, + 'financial_type_id' => 1, + ); + $this->checkPaymentResult($order, $expectedResult, $lineItems); + $this->callAPISuccess('Contribution', 'Delete', array( + 'id' => $contribution['id'], + )); + } + + /** + * Function to assert db values + */ + public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) { + foreach ($expectedResult[$results['id']] as $key => $value) { + $this->assertEquals($results['values'][$results['id']][$key], $value); + } + + if ($lineItems) { + foreach ($lineItems as $key => $items) { + foreach ($items as $k => $item) { + $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item); + } + } + } + } + + /** + * add order + * + * @param bool $isPriceSet + * @param float $amount + * @param array $extraParams + * + * @return array + */ + public function addOrder($isPriceSet, $amount = 300, $extraParams = array()) { + $p = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '2010-01-20', + 'total_amount' => $amount, + 'financial_type_id' => $this->_financialTypeId, + 'contribution_status_id' => 1, + ); + + if ($isPriceSet) { + $priceFields = $this->createPriceSet(); + 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'], + ); + } + $p['line_item'] = $lineItems; + } + $p = array_merge($extraParams, $p); + return $this->callAPISuccess('Contribution', 'create', $p); + } + +} -- 2.25.1