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,
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));
+ }
+
}
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+
+/**
+ * Test APIv3 civicrm_contribute_* functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contribution
+ */
+class api_v3_OrderTest extends CiviUnitTestCase {
+
+ /**
+ * Assume empty database with just civicrm_data.
+ */
+ protected $_individualId;
+ protected $_financialTypeId = 1;
+ protected $_apiversion;
+ public $debug = 0;
+
+ /**
+ * Setup function.
+ */
+ public function setUp() {
+ parent::setUp();
+
+ $this->_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);
+ }
+
+}