Unit test for #20342
[civicrm-core.git] / tests / phpunit / api / v3 / LineItemTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
7d61e75f
TO
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035 11
e9479dcf
EM
12/**
13 * Class api_v3_LineItemTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035 16class api_v3_LineItemTest extends CiviUnitTestCase {
0b17a870 17 use CRM_Financial_Form_SalesTaxTrait;
18
6a488035 19 protected $params;
6a488035 20 protected $_entity = 'line_item';
609a7918 21
f70aa654 22 /**
23 * Prepare for test.
24 *
25 * @throws \CRM_Core_Exception
0b577f4b 26 * @throws \CiviCRM_API3_Exception
f70aa654 27 */
7ef12efc 28 public function setUp(): void {
6a488035 29 parent::setUp();
f70aa654 30 $this->useTransaction();
9099cab3 31 $contributionParams = [
f70aa654 32 'contact_id' => $this->individualCreate(),
6a488035
TO
33 'receive_date' => '20120511',
34 'total_amount' => 100.00,
f70aa654 35 'financial_type_id' => 'Donation',
6a488035
TO
36 'non_deductible_amount' => 10.00,
37 'fee_amount' => 51.00,
38 'net_amount' => 91.00,
39 'source' => 'SSF',
40 'contribution_status_id' => 1,
9099cab3 41 ];
f70aa654 42 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
9099cab3 43 $this->params = [
6a488035
TO
44 'price_field_value_id' => 1,
45 'price_field_id' => 1,
46 'entity_table' => 'civicrm_contribution',
47 'entity_id' => $contribution['id'],
48 'qty' => 1,
49 'unit_price' => 50,
50 'line_total' => 50,
9099cab3 51 ];
6a488035
TO
52 }
53
0b17a870 54 /**
55 * Test tax is calculated correctly on the line item.
ba5e66f7 56 *
57 * @param int $version
58 *
59 * @dataProvider versionThreeAndFour
0b577f4b 60 * @throws \CRM_Core_Exception
0b17a870 61 */
0b577f4b 62 public function testCreateLineItemWithTax($version): void {
ba5e66f7 63 $this->_apiversion = $version;
0b17a870 64 $this->enableSalesTaxOnFinancialType('Donation');
65 $this->params['financial_type_id'] = 'Donation';
66 $result = $this->callAPISuccess('LineItem', 'create', $this->params);
67 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['id' => $result['id']]);
68 $this->assertEquals(5, $lineItem['tax_amount']);
69 $this->assertEquals(50, $lineItem['line_total']);
70 }
71
72 /**
73 * Enable tax for the given financial type.
74 *
0b577f4b
EM
75 * @param string $type
76 *
77 * @throws \CRM_Core_Exception
0b17a870 78 * @todo move to a trait, share.
79 *
ba5e66f7 80 * @dataProvider versionThreeAndFour
81 *
0b17a870 82 */
0b577f4b 83 public function enableSalesTaxOnFinancialType($type): void {
0b17a870 84 $this->enableTaxAndInvoicing();
28d44c71 85 $this->addTaxAccountToFinancialType(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', $type));
0b17a870 86 }
87
f70aa654 88 /**
89 * Test basic create line item.
90 *
ba5e66f7 91 * @param int $version
92 *
93 * @dataProvider versionThreeAndFour
94 *
f70aa654 95 * @throws \CRM_Core_Exception
96 */
0b577f4b 97 public function testCreateLineItem(int $version): void {
ba5e66f7 98 $this->_apiversion = $version;
f70aa654 99 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values'];
100 $this->assertCount(1, $result);
101 $this->getAndCheck($this->params, key($result), $this->_entity);
6a488035
TO
102 }
103
0b577f4b
EM
104 /**
105 * Test zero is valid for amount fields.
106 *
107 * https://github.com/civicrm/civicrm-core/pull/20342
108 *
109 * @param int $version
110 *
111 * @dataProvider versionThreeAndFour
112 *
113 * @throws \CRM_Core_Exception
114 */
115 public function testCreateLineItemZero(int $version): void {
116 $this->_apiversion = $version;
117 $this->callAPISuccess('LineItem', 'create', array_merge($this->params, ['unit_price' => 0, 'line_total' => 0]));
118 $this->callAPISuccess('LineItem', 'create', array_merge($this->params, ['unit_price' => 0.0, 'line_total' => 0.0]));
119 }
120
f70aa654 121 /**
122 * Test basic get line item.
ba5e66f7 123 *
124 * @param int $version
125 *
126 * @dataProvider versionThreeAndFour
f70aa654 127 */
0b577f4b 128 public function testGetBasicLineItem($version): void {
ba5e66f7 129 $this->_apiversion = $version;
9099cab3 130 $getParams = [
6a488035 131 'entity_table' => 'civicrm_contribution',
9099cab3 132 ];
609a7918 133 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 134 $this->assertEquals(1, $getResult['count']);
6a488035
TO
135 }
136
f70aa654 137 /**
138 * Test delete line item.
139 *
ba5e66f7 140 * @param int $version
141 *
142 * @dataProvider versionThreeAndFour
143 *
f70aa654 144 * @throws \CRM_Core_Exception
145 */
0b577f4b 146 public function testDeleteLineItem($version): void {
ba5e66f7 147 $this->_apiversion = $version;
9099cab3 148 $getParams = [
5896d037 149 'entity_table' => 'civicrm_contribution',
9099cab3 150 ];
609a7918 151 $getResult = $this->callAPISuccess($this->_entity, 'get', $getParams);
9099cab3 152 $deleteParams = ['id' => $getResult['id']];
f70aa654 153 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
154 $checkDeleted = $this->callAPISuccess($this->_entity, 'get');
ba4a1892 155 $this->assertEquals(0, $checkDeleted['count']);
6a488035
TO
156 }
157
f70aa654 158 /**
159 * Test getfields function.
160 *
161 * @throws \CRM_Core_Exception
162 */
0b577f4b 163 public function testGetFieldsLineItem(): void {
9099cab3 164 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
6a488035
TO
165 $this->assertEquals(1, $result['values']['entity_id']['api.required']);
166 }
167
6a488035 168}