3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class api_v3_LineItemTest
16 class api_v3_LineItemTest
extends CiviUnitTestCase
{
17 use CRM_Financial_Form_SalesTaxTrait
;
20 protected $_entity = 'line_item';
25 * @throws \CRM_Core_Exception
27 public function setUp() {
29 $this->useTransaction();
30 $contributionParams = [
31 'contact_id' => $this->individualCreate(),
32 'receive_date' => '20120511',
33 'total_amount' => 100.00,
34 'financial_type_id' => 'Donation',
35 'non_deductible_amount' => 10.00,
36 'fee_amount' => 51.00,
37 'net_amount' => 91.00,
39 'contribution_status_id' => 1,
41 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
43 'price_field_value_id' => 1,
44 'price_field_id' => 1,
45 'entity_table' => 'civicrm_contribution',
46 'entity_id' => $contribution['id'],
54 * Test tax is calculated correctly on the line item.
56 public function testCreateLineItemWithTax() {
57 $this->enableSalesTaxOnFinancialType('Donation');
58 $this->params
['financial_type_id'] = 'Donation';
59 $result = $this->callAPISuccess('LineItem', 'create', $this->params
);
60 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['id' => $result['id']]);
61 $this->assertEquals(5, $lineItem['tax_amount']);
62 $this->assertEquals(50, $lineItem['line_total']);
66 * Enable tax for the given financial type.
68 * @todo move to a trait, share.
72 public function enableSalesTaxOnFinancialType($type) {
73 $this->enableTaxAndInvoicing();
74 $this->relationForFinancialTypeWithFinancialAccount(CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', $type));
78 * Test basic create line item.
80 * @throws \CRM_Core_Exception
82 public function testCreateLineItem() {
83 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
)['values'];
84 $this->assertCount(1, $result);
85 $this->getAndCheck($this->params
, key($result), $this->_entity
);
89 * Test basic get line item.
91 public function testGetBasicLineItem() {
93 'entity_table' => 'civicrm_contribution',
95 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
96 $this->assertEquals(1, $getResult['count']);
100 * Test delete line item.
102 * @throws \CRM_Core_Exception
104 public function testDeleteLineItem() {
106 'entity_table' => 'civicrm_contribution',
108 $getResult = $this->callAPISuccess($this->_entity
, 'get', $getParams);
109 $deleteParams = ['id' => $getResult['id']];
110 $this->callAPIAndDocument($this->_entity
, 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
111 $checkDeleted = $this->callAPISuccess($this->_entity
, 'get');
112 $this->assertEquals(0, $checkDeleted['count']);
116 * Test getfields function.
118 * @throws \CRM_Core_Exception
120 public function testGetFieldsLineItem() {
121 $result = $this->callAPISuccess($this->_entity
, 'getfields', ['action' => 'create']);
122 $this->assertEquals(1, $result['values']['entity_id']['api.required']);