Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.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_PriceFieldValueTest
14 * @group headless
15 */
16 class 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';
22
23 public $DBResetRequired = TRUE;
24
25 /**
26 * @var int
27 */
28 protected $priceFieldID;
29
30 /**
31 * Setup function.
32 */
33 public function setUp() {
34 parent::setUp();
35 // Put stuff here that should happen before all tests in this unit.
36 $priceSetParams = [
37 'name' => 'default_goat_priceset',
38 'title' => 'Goat accommodation',
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,
46 ];
47
48 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams);
49 $this->priceSetID = $price_set['id'];
50
51 $priceFieldParams = [
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,
58 ];
59 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams);
60 $this->priceFieldID = $priceField['id'];
61 $this->_params = [
62 'price_field_id' => $this->priceFieldID,
63 'name' => 'rye grass',
64 'label' => 'juicy and healthy',
65 'amount' => 1,
66 'financial_type_id' => 1,
67 ];
68
69 $membershipOrgId = $this->organizationCreate(NULL);
70 $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]);
71 $priceSetParams1 = [
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,
79 ];
80 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
81 $this->priceSetID1 = $price_set1['id'];
82 $priceFieldParams1 = [
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,
89 ];
90 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
91 $this->priceFieldID1 = $priceField1['id'];
92 }
93
94 /**
95 * Tear down function.
96 *
97 * @throws \Exception
98 */
99 public function tearDown() {
100 $tablesToTruncate = [
101 'civicrm_contact',
102 'civicrm_contribution',
103 ];
104 $this->quickCleanup($tablesToTruncate);
105 $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
106 $this->callAPISuccess('PriceField', 'delete', [
107 'id' => $this->priceFieldID1,
108 ]);
109 $this->callAPISuccess('PriceSet', 'delete', [
110 'id' => $this->priceSetID1,
111 ]);
112 $this->callAPISuccess('PriceField', 'delete', [
113 'id' => $this->priceFieldID,
114 ]);
115 $delete = $this->callAPISuccess('PriceSet', 'delete', [
116 'id' => $this->priceSetID,
117 ]);
118
119 $this->assertAPISuccess($delete);
120 }
121
122 public function testCreatePriceFieldValue() {
123 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
124 $this->assertAPISuccess($result);
125 $this->assertEquals(1, $result['count']);
126 $this->assertNotNull($result['values'][$result['id']]['id']);
127 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
128 }
129
130 public function testGetBasicPriceFieldValue() {
131 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
132 $this->id = $createResult['id'];
133 $this->assertAPISuccess($createResult);
134 $getParams = [
135 'name' => 'contribution_amount',
136 ];
137 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
138 $this->assertEquals(1, $getResult['count']);
139 $this->callAPISuccess('price_field_value', 'delete', ['id' => $createResult['id']]);
140 }
141
142 public function testDeletePriceFieldValue() {
143 $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
144 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
145 $deleteParams = ['id' => $createResult['id']];
146 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
147
148 $endCount = $this->callAPISuccess($this->_entity, 'getcount', []);
149 $this->assertEquals($startCount, $endCount);
150 }
151
152 public function testGetFieldsPriceFieldValue() {
153 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
154 $this->assertEquals(1, $result['values']['max_value']['type']);
155 }
156
157 public function testCreatePriceFieldValuewithMultipleTerms() {
158 $params = [
159 'price_field_id' => $this->priceFieldID1,
160 'membership_type_id' => $this->_membershipTypeID,
161 'name' => 'memType1',
162 'label' => 'memType1',
163 'amount' => 90,
164 'membership_num_terms' => 2,
165 'is_active' => 1,
166 'financial_type_id' => 2,
167 ];
168 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
169 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
170 $this->assertEquals(1, $result['count']);
171 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
172 }
173
174 public function testGetPriceFieldValuewithMultipleTerms() {
175 $params1 = [
176 'price_field_id' => $this->priceFieldID1,
177 'membership_type_id' => $this->_membershipTypeID,
178 'name' => 'memType1',
179 'label' => 'memType1',
180 'amount' => 90,
181 'membership_num_terms' => 2,
182 'is_active' => 1,
183 'financial_type_id' => 2,
184 ];
185 $params2 = [
186 'price_field_id' => $this->priceFieldID1,
187 'membership_type_id' => $this->_membershipTypeID,
188 'name' => 'memType2',
189 'label' => 'memType2',
190 'amount' => 120,
191 'membership_num_terms' => 3,
192 'is_active' => 1,
193 'financial_type_id' => 2,
194 ];
195 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
196 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
197 $result = $this->callAPISuccess($this->_entity, 'get', ['price_field_id' => $this->priceFieldID1]);
198 $this->assertEquals(2, $result['count']);
199 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result1['id']]);
200 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result2['id']]);
201 }
202
203 public function testCreatePriceFieldValueWithDisabledFinancialType() {
204 $financialTypeParams = [
205 'is_active' => 0,
206 'name' => 'Disabled Donations',
207 ];
208 $financialType = $this->callAPISuccess('financial_type', 'create', $financialTypeParams);
209 $params = [
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'],
216 ];
217 $this->callAPIFailure($this->_entity, 'create', $params);
218 }
219
220 }