Merge pull request #18114 from eileenmcnaughton/phone
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.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_MembershipTypeTest
14 * @group headless
15 */
16 class api_v3_MembershipTypeTest extends CiviUnitTestCase {
17 protected $_contactID;
18 protected $_entity = 'MembershipType';
19
20 /**
21 * Set up for tests.
22 */
23 public function setUp() {
24 parent::setUp();
25 $this->useTransaction(TRUE);
26 $this->_contactID = $this->organizationCreate();
27 }
28
29 /**
30 * Get the membership without providing an ID.
31 *
32 * This should return an empty array but not an error.
33 *
34 * @dataProvider versionThreeAndFour
35 *
36 * @param bool $version
37 *
38 * @throws \CRM_Core_Exception
39 */
40 public function testGetWithoutId($version) {
41 $this->_apiversion = $version;
42 $params = [
43 'name' => '60+ Membership',
44 'description' => 'people above 60 are given health instructions',
45 'financial_type_id' => 1,
46 'minimum_fee' => '200',
47 'duration_unit' => 'month',
48 'duration_interval' => '10',
49 'visibility' => 'public',
50 ];
51
52 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
53 $this->assertEquals($membershipType['count'], 0);
54 }
55
56 /**
57 * Test get works.
58 *
59 * @dataProvider versionThreeAndFour
60 *
61 * @param bool $version
62 */
63 public function testGet($version) {
64 $this->_apiversion = $version;
65 $id = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]);
66 $params = ['id' => $id];
67 $membershipType = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
68 $this->assertEquals($membershipType['values'][$id]['name'], 'General');
69 $this->assertEquals($membershipType['values'][$id]['member_of_contact_id'], $this->_contactID);
70 $this->assertEquals($membershipType['values'][$id]['financial_type_id'], $this->getFinancialTypeId('Member Dues'));
71 $this->assertEquals($membershipType['values'][$id]['duration_unit'], 'year');
72 $this->assertEquals($membershipType['values'][$id]['duration_interval'], '1');
73 $this->assertEquals($membershipType['values'][$id]['period_type'], 'rolling');
74 $this->membershipTypeDelete($params);
75 }
76
77 /**
78 * Test create with missing mandatory field.
79 *
80 * @dataProvider versionThreeAndFour
81 *
82 * @param bool $version
83 */
84 public function testCreateWithoutMemberOfContactId($version) {
85 $this->_apiversion = $version;
86 $params = [
87 'name' => '60+ Membership',
88 'description' => 'people above 60 are given health instructions',
89 'financial_type_id' => 1,
90 'domain_id' => '1',
91 'minimum_fee' => '200',
92 'duration_unit' => 'month',
93 'duration_interval' => '10',
94 'period_type' => 'rolling',
95 'visibility' => 'public',
96 ];
97
98 $msg = $version === 4 ? 'Mandatory values missing from Api4 MembershipType::create: member_of_contact_id' : 'Mandatory key(s) missing from params array: member_of_contact_id';
99 $this->callAPIFailure('membership_type', 'create', $params, $msg);
100 }
101
102 /**
103 * Test successful create.
104 *
105 * @dataProvider versionThreeAndFour
106 *
107 * @param bool $version
108 */
109 public function testCreate($version) {
110 $this->_apiversion = $version;
111 $params = [
112 'name' => '40+ Membership',
113 'description' => 'people above 40 are given health instructions',
114 'member_of_contact_id' => $this->_contactID,
115 'financial_type_id' => 1,
116 'domain_id' => '1',
117 'minimum_fee' => '200',
118 'duration_unit' => 'month',
119 'duration_interval' => '10',
120 'period_type' => 'rolling',
121 'visibility' => 'public',
122 ];
123
124 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
125 $this->assertNotNull($membershipType['values']);
126 $this->membershipTypeDelete(['id' => $membershipType['id']]);
127 }
128
129 /**
130 * Domain ID can be intuited..
131 * DomainID is now optional on API, check that it gets set correctly and that the domain_id is not overwritten when not specified in create.
132 *
133 * @dataProvider versionThreeAndFour
134 *
135 * @param bool $version
136 *
137 * @throws \CRM_Core_Exception
138 */
139 public function testCreateWithoutDomainId($version) {
140 $this->_apiversion = $version;
141 $params = [
142 'name' => '60+ Membership',
143 'description' => 'people above 60 are given health instructions',
144 'member_of_contact_id' => $this->_contactID,
145 'financial_type_id' => 1,
146 'minimum_fee' => '1200',
147 'duration_unit' => 'month',
148 'duration_interval' => '10',
149 'period_type' => 'rolling',
150 'visibility' => 'public',
151 ];
152
153 $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
154 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
155 $this->assertEquals(CRM_Core_Config::domainID(), $domainID);
156
157 $this->callAPISuccess('membership_type', 'create', ['domain_id' => 2, 'id' => $membershipType['id']]);
158 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
159 $this->assertEquals(2, $domainID);
160
161 $this->callAPISuccess('membership_type', 'create', ['id' => $membershipType['id'], 'description' => 'Cool member']);
162 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
163 $this->assertEquals(2, $domainID);
164
165 }
166
167 /**
168 * CRM-20010 Tests period_type is required for MemberType create
169 *
170 * @dataProvider versionThreeAndFour
171 *
172 * @param bool $version
173 */
174 public function testMemberTypePeriodiTypeRequired($version) {
175 $this->_apiversion = $version;
176 $this->callAPIFailure('MembershipType', 'create', [
177 'domain_id' => 'Default Domain Name',
178 'member_of_contact_id' => 1,
179 'financial_type_id' => 'Member Dues',
180 'duration_unit' => 'month',
181 'duration_interval' => 1,
182 'name' => 'Standard Member',
183 'minimum_fee' => 100,
184 ]);
185 }
186
187 /**
188 * Test update.
189 *
190 * @dataProvider versionThreeAndFour
191 *
192 * @param bool $version
193 *
194 * @throws \CRM_Core_Exception
195 */
196 public function testUpdate($version) {
197 $this->_apiversion = $version;
198 $id = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2]);
199 $newMemberOrgParams = [
200 'organization_name' => 'New membership organisation',
201 'contact_type' => 'Organization',
202 'visibility' => 1,
203 ];
204
205 $params = [
206 'id' => $id,
207 'name' => 'Updated General',
208 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
209 'duration_unit' => 'month',
210 'duration_interval' => '10',
211 'period_type' => 'fixed',
212 'domain_id' => 1,
213 ];
214
215 $this->callAPISuccess('membership_type', 'update', $params);
216
217 $this->getAndCheck($params, $id, $this->_entity);
218 }
219
220 /**
221 * Test successful delete.
222 *
223 * @dataProvider versionThreeAndFour
224 *
225 * @param bool $version
226 */
227 public function testDelete($version) {
228 $this->_apiversion = $version;
229 $membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->organizationCreate()]);
230 $params = ['id' => $membershipTypeID];
231 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
232 }
233
234 /**
235 * Delete test that could do with a decent comment block.
236 *
237 * I can't skim this & understand it so if anyone does explain it here.
238 */
239 public function testDeleteRelationshipTypesUsedByMembershipType() {
240 $rel1 = $this->relationshipTypeCreate([
241 'name_a_b' => 'abcde',
242 'name_b_a' => 'abcde',
243 ]);
244 $rel2 = $this->relationshipTypeCreate([
245 'name_a_b' => 'fghij',
246 'name_b_a' => 'fghij',
247 ]);
248 $rel3 = $this->relationshipTypeCreate([
249 'name_a_b' => 'lkmno',
250 'name_b_a' => 'lkmno',
251 ]);
252 $id = $this->membershipTypeCreate([
253 'member_of_contact_id' => $this->_contactID,
254 'relationship_type_id' => [$rel1, $rel2, $rel3],
255 'relationship_direction' => ['a_b', 'a_b', 'b_a'],
256 ]);
257
258 $this->callAPISuccess('RelationshipType', 'delete', ['id' => $rel2]);
259 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', ['id' => $id]);
260 $this->assertEquals([$rel1, $rel3], $newValues['relationship_type_id']);
261 $this->assertEquals(['a_b', 'b_a'], $newValues['relationship_direction']);
262
263 $this->callAPISuccess('RelationshipType', 'delete', ['id' => $rel1]);
264 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', ['id' => $id]);
265 $this->assertEquals([$rel3], $newValues['relationship_type_id']);
266 $this->assertEquals(['b_a'], $newValues['relationship_direction']);
267
268 $this->callAPISuccess('RelationshipType', 'delete', ['id' => $rel3]);
269 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', ['id' => $id]);
270 $this->assertTrue(empty($newValues['relationship_type_id']));
271 $this->assertTrue(empty($newValues['relationship_direction']));
272 }
273
274 /**
275 * Test that membership type getlist returns an array of enabled membership types.
276 */
277 public function testMembershipTypeGetList() {
278 $this->membershipTypeCreate();
279 $this->membershipTypeCreate(['name' => 'cheap-skates']);
280 $this->membershipTypeCreate(['name' => 'disabled cheap-skates', 'is_active' => 0]);
281 $result = $this->callAPISuccess('MembershipType', 'getlist', []);
282 $this->assertEquals(2, $result['count']);
283 $this->assertEquals('cheap-skates', $result['values'][0]['label']);
284 $this->assertEquals('General', $result['values'][1]['label']);
285 }
286
287 /**
288 * Test priceField values are correctly created for membership type
289 * selected in contribution pages.
290 */
291 public function testEnableMembershipTypeOnContributionPage() {
292 $memType = [];
293 $memType[1] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'minimum_fee' => 100]);
294 $priceSet = $this->callAPISuccess('price_set', 'create', [
295 'title' => 'test priceset',
296 'name' => 'test_priceset',
297 'extends' => 'CiviMember',
298 'is_quick_config' => 1,
299 'financial_type_id' => 'Member Dues',
300 ]);
301 $priceSet = $priceSet['id'];
302 $field = $this->callAPISuccess('price_field', 'create', [
303 'price_set_id' => $priceSet,
304 'name' => 'membership_amount',
305 'label' => 'Membership Amount',
306 'html_type' => 'Radio',
307 ]);
308 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
309 'name' => 'membership_amount',
310 'label' => 'Membership Amount',
311 'amount' => 100,
312 'financial_type_id' => 'Donation',
313 'format.only_id' => TRUE,
314 'membership_type_id' => $memType[1],
315 'price_field_id' => $field['id'],
316 ]);
317
318 $memType[2] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'minimum_fee' => 200]);
319 $fieldParams = [
320 'id' => $field['id'],
321 'label' => 'Membership Amount',
322 'html_type' => 'Radio',
323 ];
324 foreach ($memType as $rowCount => $type) {
325 $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($type);
326 $fieldParams['option_id'] = [1 => $priceFieldValue['id']];
327 $fieldParams['option_label'][$rowCount] = $membetype['name'] ?? NULL;
328 $fieldParams['option_amount'][$rowCount] = $membetype['minimum_fee'] ?? 0;
329 $fieldParams['option_weight'][$rowCount] = $membetype['weight'] ?? NULL;
330 $fieldParams['option_description'][$rowCount] = $membetype['description'] ?? NULL;
331 $fieldParams['option_financial_type_id'][$rowCount] = $membetype['financial_type_id'] ?? NULL;
332 $fieldParams['membership_type_id'][$rowCount] = $type;
333 }
334 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
335 $this->assertEquals($priceField->id, $fieldParams['id']);
336
337 //Update membership type name and visibility
338 $updateParams = [
339 'id' => $memType[1],
340 'name' => 'General - Edited',
341 'visibility' => 'Admin',
342 'financial_type_id' => 1,
343 'minimum_fee' => 300,
344 'description' => 'Test edit description',
345 ];
346 $this->callAPISuccess('membership_type', 'create', $updateParams);
347 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', [
348 'sequential' => 1,
349 'membership_type_id' => $memType[1],
350 ]);
351 //Verify if membership type updates are copied to pricefield value.
352 foreach ($priceFieldValue['values'] as $key => $value) {
353 $setId = $this->callAPISuccessGetValue('PriceField', ['return' => 'price_set_id', 'id' => $value['price_field_id']]);
354 if ($setId == $priceSet) {
355 $this->assertEquals($value['label'], $updateParams['name']);
356 $this->assertEquals($value['description'], $updateParams['description']);
357 $this->assertEquals((int) $value['amount'], $updateParams['minimum_fee']);
358 $this->assertEquals($value['financial_type_id'], $updateParams['financial_type_id']);
359 $this->assertEquals($value['visibility_id'], CRM_Price_BAO_PriceField::getVisibilityOptionID(strtolower($updateParams['visibility'])));
360 }
361 }
362
363 foreach ($memType as $type) {
364 $this->callAPISuccess('membership_type', 'delete', ['id' => $type]);
365 }
366
367 }
368
369 }