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