Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / tests / phpunit / api / v3 / LineItemTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_LineItemTest
14 * @group headless
15 */
16 class api_v3_LineItemTest extends CiviUnitTestCase {
17 use CRM_Financial_Form_SalesTaxTrait;
18
19 protected $params;
20 protected $_entity = 'line_item';
21
22 /**
23 * Prepare for test.
24 *
25 * @throws \CRM_Core_Exception
26 */
27 public function setUp() {
28 parent::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,
38 'source' => 'SSF',
39 'contribution_status_id' => 1,
40 ];
41 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
42 $this->params = [
43 'price_field_value_id' => 1,
44 'price_field_id' => 1,
45 'entity_table' => 'civicrm_contribution',
46 'entity_id' => $contribution['id'],
47 'qty' => 1,
48 'unit_price' => 50,
49 'line_total' => 50,
50 ];
51 }
52
53 /**
54 * Test tax is calculated correctly on the line item.
55 */
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']);
63 }
64
65 /**
66 * Enable tax for the given financial type.
67 *
68 * @todo move to a trait, share.
69 *
70 * @param string $type
71 */
72 public function enableSalesTaxOnFinancialType($type) {
73 $this->enableTaxAndInvoicing();
74 $this->relationForFinancialTypeWithFinancialAccount(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', $type));
75 }
76
77 /**
78 * Test basic create line item.
79 *
80 * @throws \CRM_Core_Exception
81 */
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);
86 }
87
88 /**
89 * Test basic get line item.
90 */
91 public function testGetBasicLineItem() {
92 $getParams = [
93 'entity_table' => 'civicrm_contribution',
94 ];
95 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
96 $this->assertEquals(1, $getResult['count']);
97 }
98
99 /**
100 * Test delete line item.
101 *
102 * @throws \CRM_Core_Exception
103 */
104 public function testDeleteLineItem() {
105 $getParams = [
106 'entity_table' => 'civicrm_contribution',
107 ];
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']);
113 }
114
115 /**
116 * Test getfields function.
117 *
118 * @throws \CRM_Core_Exception
119 */
120 public function testGetFieldsLineItem() {
121 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
122 $this->assertEquals(1, $result['values']['entity_id']['api.required']);
123 }
124
125 }