Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.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_PriceFieldValueTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035
TO
16class api_v3_PriceFieldValueTest extends CiviUnitTestCase {
17 protected $_apiversion = 3;
18 protected $_params;
19 protected $id = 0;
20 protected $priceSetID = 0;
21 protected $_entity = 'price_field_value';
b7c9bc4c 22
6a488035
TO
23 public $DBResetRequired = TRUE;
24
78373706
EM
25 /**
26 * @var int
27 */
28 protected $priceFieldID;
29
30 /**
31 * Setup function.
32 */
6a488035
TO
33 public function setUp() {
34 parent::setUp();
78373706 35 // Put stuff here that should happen before all tests in this unit.
9099cab3 36 $priceSetParams = [
6a488035 37 'name' => 'default_goat_priceset',
78373706 38 'title' => 'Goat accommodation',
6a488035
TO
39 'is_active' => 1,
40 'help_pre' => "Where does your goat sleep",
41 'help_post' => "thank you for your time",
42 'extends' => 2,
43 'financial_type_id' => 1,
44 'is_quick_config' => 1,
45 'is_reserved' => 1,
9099cab3 46 ];
6a488035 47
78373706 48 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams);
6a488035
TO
49 $this->priceSetID = $price_set['id'];
50
9099cab3 51 $priceFieldParams = [
6a488035
TO
52 'price_set_id' => $this->priceSetID,
53 'name' => 'grassvariety',
54 'label' => 'Grass Variety',
55 'html_type' => 'Text',
56 'is_enter_qty' => 1,
57 'is_active' => 1,
9099cab3 58 ];
78373706 59 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams);
6a488035 60 $this->priceFieldID = $priceField['id'];
9099cab3 61 $this->_params = [
6a488035 62 'price_field_id' => $this->priceFieldID,
78373706 63 'name' => 'rye grass',
6a488035 64 'label' => 'juicy and healthy',
21dfd5f5 65 'amount' => 1,
43dbd988 66 'financial_type_id' => 1,
9099cab3 67 ];
b6708aeb 68
6a488035 69 $membershipOrgId = $this->organizationCreate(NULL);
9099cab3
CW
70 $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]);
71 $priceSetParams1 = [
6a488035
TO
72 'name' => 'priceset',
73 'title' => 'Priceset with Multiple Terms',
74 'is_active' => 1,
75 'extends' => 3,
76 'financial_type_id' => 2,
77 'is_quick_config' => 1,
78 'is_reserved' => 1,
9099cab3 79 ];
78373706 80 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
6a488035 81 $this->priceSetID1 = $price_set1['id'];
9099cab3 82 $priceFieldParams1 = [
6a488035
TO
83 'price_set_id' => $this->priceSetID1,
84 'name' => 'memtype',
85 'label' => 'memtype',
86 'html_type' => 'Radio',
87 'is_enter_qty' => 1,
88 'is_active' => 1,
9099cab3 89 ];
78373706 90 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
6a488035
TO
91 $this->priceFieldID1 = $priceField1['id'];
92 }
93
78373706
EM
94 /**
95 * Tear down function.
96 *
97 * @throws \Exception
98 */
00be9182 99 public function tearDown() {
9099cab3 100 $tablesToTruncate = [
92915c55
TO
101 'civicrm_contact',
102 'civicrm_contribution',
9099cab3 103 ];
6a488035 104 $this->quickCleanup($tablesToTruncate);
9099cab3
CW
105 $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
106 $this->callAPISuccess('PriceField', 'delete', [
92915c55 107 'id' => $this->priceFieldID1,
9099cab3
CW
108 ]);
109 $this->callAPISuccess('PriceSet', 'delete', [
6a488035 110 'id' => $this->priceSetID1,
9099cab3
CW
111 ]);
112 $this->callAPISuccess('PriceField', 'delete', [
92915c55 113 'id' => $this->priceFieldID,
9099cab3
CW
114 ]);
115 $delete = $this->callAPISuccess('PriceSet', 'delete', [
6a488035 116 'id' => $this->priceSetID,
9099cab3 117 ]);
6a488035
TO
118
119 $this->assertAPISuccess($delete);
120 }
121
122 public function testCreatePriceFieldValue() {
ca985406 123 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
a15773db 124 $this->assertAPISuccess($result);
ba4a1892
TM
125 $this->assertEquals(1, $result['count']);
126 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
127 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
128 }
129
130 public function testGetBasicPriceFieldValue() {
ca985406 131 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
132 $this->id = $createResult['id'];
133 $this->assertAPISuccess($createResult);
9099cab3 134 $getParams = [
6a488035 135 'name' => 'contribution_amount',
9099cab3 136 ];
ca985406 137 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 138 $this->assertEquals(1, $getResult['count']);
9099cab3 139 $this->callAPISuccess('price_field_value', 'delete', ['id' => $createResult['id']]);
6a488035
TO
140 }
141
142 public function testDeletePriceFieldValue() {
9099cab3 143 $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
ca985406 144 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 145 $deleteParams = ['id' => $createResult['id']];
ca985406 146 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
147
9099cab3 148 $endCount = $this->callAPISuccess($this->_entity, 'getcount', []);
a15773db 149 $this->assertEquals($startCount, $endCount);
6a488035
TO
150 }
151
152 public function testGetFieldsPriceFieldValue() {
9099cab3 153 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
6a488035
TO
154 $this->assertEquals(1, $result['values']['max_value']['type']);
155 }
156
157 public function testCreatePriceFieldValuewithMultipleTerms() {
9099cab3 158 $params = [
6a488035 159 'price_field_id' => $this->priceFieldID1,
6c6e6187 160 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
161 'name' => 'memType1',
162 'label' => 'memType1',
163 'amount' => 90,
164 'membership_num_terms' => 2,
165 'is_active' => 1,
166 'financial_type_id' => 2,
9099cab3 167 ];
ca985406 168 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 169 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
ba4a1892 170 $this->assertEquals(1, $result['count']);
9099cab3 171 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
6a488035 172 }
92915c55 173
6a488035 174 public function testGetPriceFieldValuewithMultipleTerms() {
9099cab3 175 $params1 = [
6a488035 176 'price_field_id' => $this->priceFieldID1,
6c6e6187 177 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
178 'name' => 'memType1',
179 'label' => 'memType1',
180 'amount' => 90,
181 'membership_num_terms' => 2,
182 'is_active' => 1,
183 'financial_type_id' => 2,
9099cab3
CW
184 ];
185 $params2 = [
6a488035 186 'price_field_id' => $this->priceFieldID1,
6c6e6187 187 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
188 'name' => 'memType2',
189 'label' => 'memType2',
190 'amount' => 120,
191 'membership_num_terms' => 3,
192 'is_active' => 1,
193 'financial_type_id' => 2,
9099cab3 194 ];
ca985406 195 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
196 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
9099cab3 197 $result = $this->callAPISuccess($this->_entity, 'get', ['price_field_id' => $this->priceFieldID1]);
ba4a1892 198 $this->assertEquals(2, $result['count']);
9099cab3
CW
199 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result1['id']]);
200 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result2['id']]);
6a488035 201 }
96025800 202
1019b2fe 203 public function testCreatePriceFieldValueWithDisabledFinancialType() {
9099cab3 204 $financialTypeParams = [
1019b2fe
SL
205 'is_active' => 0,
206 'name' => 'Disabled Donations',
9099cab3 207 ];
1019b2fe 208 $financialType = $this->callAPISuccess('financial_type', 'create', $financialTypeParams);
9099cab3 209 $params = [
1019b2fe
SL
210 'price_field_id' => $this->priceFieldID,
211 'name' => 'DonType1',
212 'label' => 'DonType1',
213 'amount' => 90,
214 'is_active' => 1,
215 'financial_type_id' => $financialType['id'],
9099cab3 216 ];
1019b2fe
SL
217 $this->callAPIFailure($this->_entity, 'create', $params);
218 }
219
6a488035 220}