Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 | 2 | /* |
7d61e75f TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 | 11 | |
e9479dcf EM |
12 | /** |
13 | * Class api_v3_MembershipTypeTest | |
acb109b7 | 14 | * @group headless |
e9479dcf | 15 | */ |
6a488035 TO |
16 | class api_v3_MembershipTypeTest extends CiviUnitTestCase { |
17 | protected $_contactID; | |
18 | protected $_contributionTypeID; | |
19 | protected $_apiversion; | |
20 | protected $_entity = 'MembershipType'; | |
b7c9bc4c | 21 | |
80d714d2 | 22 | /** |
23 | * Set up for tests. | |
24 | */ | |
00be9182 | 25 | public function setUp() { |
6a488035 | 26 | parent::setUp(); |
31c45547 | 27 | $this->useTransaction(TRUE); |
6a488035 | 28 | $this->_apiversion = 3; |
baccd59e | 29 | $this->_contactID = $this->organizationCreate(); |
6a488035 TO |
30 | } |
31 | ||
80d714d2 | 32 | /** |
33 | * Get the membership without providing an ID. | |
34 | * | |
35 | * This should return an empty array but not an error. | |
36 | */ | |
00be9182 | 37 | public function testGetWithoutId() { |
9099cab3 | 38 | $params = [ |
6a488035 TO |
39 | 'name' => '60+ Membership', |
40 | 'description' => 'people above 60 are given health instructions', | |
3d6a42e8 | 41 | 'financial_type_id' => 1, |
6a488035 TO |
42 | 'minimum_fee' => '200', |
43 | 'duration_unit' => 'month', | |
44 | 'duration_interval' => '10', | |
45 | 'visibility' => 'public', | |
9099cab3 | 46 | ]; |
6a488035 | 47 | |
80d714d2 | 48 | $membershipType = $this->callAPISuccess('membership_type', 'get', $params); |
49 | $this->assertEquals($membershipType['count'], 0); | |
6a488035 TO |
50 | } |
51 | ||
80d714d2 | 52 | /** |
53 | * Test get works. | |
54 | */ | |
00be9182 | 55 | public function testGet() { |
9099cab3 | 56 | $id = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]); |
6a488035 | 57 | |
9099cab3 | 58 | $params = [ |
6a488035 | 59 | 'id' => $id, |
9099cab3 | 60 | ]; |
80d714d2 | 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); | |
8484a5f0 | 64 | $this->assertEquals($membershipType['values'][$id]['financial_type_id'], $this->getFinancialTypeId('Member Dues')); |
80d714d2 | 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'); | |
6a488035 TO |
68 | $this->membershipTypeDelete($params); |
69 | } | |
70 | ||
5f1108c8 | 71 | /** |
72 | * Test create with missing mandatory field. | |
73 | */ | |
00be9182 | 74 | public function testCreateWithoutMemberOfContactId() { |
9099cab3 | 75 | $params = [ |
6a488035 TO |
76 | 'name' => '60+ Membership', |
77 | 'description' => 'people above 60 are given health instructions', | |
3d6a42e8 | 78 | 'financial_type_id' => 1, |
6a488035 TO |
79 | 'domain_id' => '1', |
80 | 'minimum_fee' => '200', | |
81 | 'duration_unit' => 'month', | |
82 | 'duration_interval' => '10', | |
83 | 'period_type' => 'rolling', | |
84 | 'visibility' => 'public', | |
9099cab3 | 85 | ]; |
6a488035 | 86 | |
80d714d2 | 87 | $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id'); |
6a488035 TO |
88 | } |
89 | ||
5f1108c8 | 90 | /** |
91 | * Test successful create. | |
92 | */ | |
00be9182 | 93 | public function testCreate() { |
9099cab3 | 94 | $params = [ |
6a488035 TO |
95 | 'name' => '40+ Membership', |
96 | 'description' => 'people above 40 are given health instructions', | |
97 | 'member_of_contact_id' => $this->_contactID, | |
3d6a42e8 | 98 | 'financial_type_id' => 1, |
6a488035 TO |
99 | 'domain_id' => '1', |
100 | 'minimum_fee' => '200', | |
101 | 'duration_unit' => 'month', | |
102 | 'duration_interval' => '10', | |
103 | 'period_type' => 'rolling', | |
104 | 'visibility' => 'public', | |
9099cab3 | 105 | ]; |
6a488035 | 106 | |
edc9b94e EM |
107 | $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__); |
108 | $this->assertNotNull($membershipType['values']); | |
9099cab3 | 109 | $this->membershipTypeDelete(['id' => $membershipType['id']]); |
6a488035 TO |
110 | } |
111 | ||
edc9b94e | 112 | /** |
fb38ab8d | 113 | * Domain ID can be intuited.. |
a91041c5 | 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. |
edc9b94e | 115 | */ |
fb38ab8d | 116 | public function testCreateWithoutDomainId() { |
9099cab3 | 117 | $params = [ |
6a488035 TO |
118 | 'name' => '60+ Membership', |
119 | 'description' => 'people above 60 are given health instructions', | |
120 | 'member_of_contact_id' => $this->_contactID, | |
3d6a42e8 | 121 | 'financial_type_id' => 1, |
6a488035 TO |
122 | 'minimum_fee' => '1200', |
123 | 'duration_unit' => 'month', | |
124 | 'duration_interval' => '10', | |
125 | 'period_type' => 'rolling', | |
92915c55 | 126 | 'visibility' => 'public', |
9099cab3 | 127 | ]; |
6a488035 | 128 | |
fb38ab8d | 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 | ||
6a488035 TO |
141 | } |
142 | ||
5b1b8db2 PH |
143 | /** |
144 | * CRM-20010 Tests period_type is required for MemberType create | |
145 | */ | |
146 | public function testMemberTypePeriodiTypeRequired() { | |
9099cab3 | 147 | $this->callAPIFailure('MembershipType', 'create', [ |
5b1b8db2 PH |
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, | |
9099cab3 | 155 | ]); |
5b1b8db2 PH |
156 | } |
157 | ||
5f1108c8 | 158 | /** |
159 | * Test update. | |
160 | */ | |
00be9182 | 161 | public function testUpdate() { |
9099cab3 CW |
162 | $id = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2]); |
163 | $newMemberOrgParams = [ | |
6a488035 TO |
164 | 'organization_name' => 'New membership organisation', |
165 | 'contact_type' => 'Organization', | |
92915c55 | 166 | 'visibility' => 1, |
9099cab3 | 167 | ]; |
6a488035 | 168 | |
9099cab3 | 169 | $params = [ |
6a488035 TO |
170 | 'id' => $id, |
171 | 'name' => 'Updated General', | |
5f1108c8 | 172 | 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams), |
6a488035 TO |
173 | 'duration_unit' => 'month', |
174 | 'duration_interval' => '10', | |
175 | 'period_type' => 'fixed', | |
92915c55 | 176 | 'domain_id' => 1, |
9099cab3 | 177 | ]; |
3d6a42e8 | 178 | |
d63167fc | 179 | $this->callAPISuccess('membership_type', 'update', $params); |
6a488035 TO |
180 | |
181 | $this->getAndCheck($params, $id, $this->_entity); | |
182 | } | |
183 | ||
5f1108c8 | 184 | /** |
185 | * Test successful delete. | |
186 | */ | |
00be9182 | 187 | public function testDelete() { |
9099cab3 CW |
188 | $membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->organizationCreate()]); |
189 | $params = [ | |
6a488035 | 190 | 'id' => $membershipTypeID, |
9099cab3 | 191 | ]; |
6a488035 | 192 | |
edc9b94e | 193 | $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 194 | } |
96025800 | 195 | |
5f1108c8 | 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 | */ | |
baccd59e | 201 | public function testDeleteRelationshipTypesUsedByMembershipType() { |
9099cab3 | 202 | $rel1 = $this->relationshipTypeCreate([ |
baccd59e CW |
203 | 'name_a_b' => 'abcde', |
204 | 'name_b_a' => 'abcde', | |
9099cab3 CW |
205 | ]); |
206 | $rel2 = $this->relationshipTypeCreate([ | |
baccd59e CW |
207 | 'name_a_b' => 'fghij', |
208 | 'name_b_a' => 'fghij', | |
9099cab3 CW |
209 | ]); |
210 | $rel3 = $this->relationshipTypeCreate([ | |
baccd59e CW |
211 | 'name_a_b' => 'lkmno', |
212 | 'name_b_a' => 'lkmno', | |
9099cab3 CW |
213 | ]); |
214 | $id = $this->membershipTypeCreate([ | |
baccd59e | 215 | 'member_of_contact_id' => $this->_contactID, |
9099cab3 CW |
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]); | |
baccd59e CW |
232 | $this->assertTrue(empty($newValues['relationship_type_id'])); |
233 | $this->assertTrue(empty($newValues['relationship_direction'])); | |
234 | } | |
235 | ||
35dfd73b | 236 | /** |
237 | * Test that membership type getlist returns an array of enabled membership types. | |
238 | */ | |
239 | public function testMembershipTypeGetList() { | |
240 | $this->membershipTypeCreate(); | |
9099cab3 CW |
241 | $this->membershipTypeCreate(['name' => 'cheap-skates']); |
242 | $this->membershipTypeCreate(['name' => 'disabled cheap-skates', 'is_active' => 0]); | |
243 | $result = $this->callAPISuccess('MembershipType', 'getlist', []); | |
35dfd73b | 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 | ||
026a0d69 JP |
249 | /** |
250 | * Test priceField values are correctly created for membership type | |
251 | * selected in contribution pages. | |
252 | */ | |
253 | public function testEnableMembershipTypeOnContributionPage() { | |
9099cab3 CW |
254 | $memType = []; |
255 | $memType[1] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'minimum_fee' => 100]); | |
256 | $priceSet = $this->callAPISuccess('price_set', 'create', [ | |
5afcf706 JP |
257 | 'title' => "test priceset", |
258 | 'name' => "test_priceset", | |
259 | 'extends' => "CiviMember", | |
260 | 'is_quick_config' => 1, | |
261 | 'financial_type_id' => "Member Dues", | |
9099cab3 | 262 | ]); |
5afcf706 | 263 | $priceSet = $priceSet['id']; |
9099cab3 | 264 | $field = $this->callAPISuccess('price_field', 'create', [ |
026a0d69 JP |
265 | 'price_set_id' => $priceSet, |
266 | 'name' => 'membership_amount', | |
267 | 'label' => 'Membership Amount', | |
268 | 'html_type' => 'Radio', | |
9099cab3 CW |
269 | ]); |
270 | $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [ | |
026a0d69 JP |
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'], | |
9099cab3 | 278 | ]); |
026a0d69 | 279 | |
9099cab3 CW |
280 | $memType[2] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID, 'minimum_fee' => 200]); |
281 | $fieldParams = [ | |
026a0d69 JP |
282 | 'id' => $field['id'], |
283 | 'label' => 'Membership Amount', | |
284 | 'html_type' => 'Radio', | |
9099cab3 | 285 | ]; |
026a0d69 JP |
286 | foreach ($memType as $rowCount => $type) { |
287 | $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($type); | |
9099cab3 | 288 | $fieldParams['option_id'] = [1 => $priceFieldValue['id']]; |
026a0d69 JP |
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 | ||
5afcf706 | 299 | //Update membership type name and visibility |
9099cab3 | 300 | $updateParams = [ |
5afcf706 JP |
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', | |
9099cab3 | 307 | ]; |
5afcf706 | 308 | $this->callAPISuccess('membership_type', 'create', $updateParams); |
9099cab3 | 309 | $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', [ |
5afcf706 JP |
310 | 'sequential' => 1, |
311 | 'membership_type_id' => $memType[1], | |
9099cab3 | 312 | ]); |
5afcf706 JP |
313 | //Verify if membership type updates are copied to pricefield value. |
314 | foreach ($priceFieldValue['values'] as $key => $value) { | |
9099cab3 | 315 | $setId = $this->callAPISuccessGetValue('PriceField', ['return' => "price_set_id", 'id' => $value['price_field_id']]); |
5afcf706 JP |
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 | ||
026a0d69 | 325 | foreach ($memType as $type) { |
9099cab3 | 326 | $this->callAPISuccess('membership_type', 'delete', ['id' => $type]); |
026a0d69 JP |
327 | } |
328 | ||
329 | } | |
330 | ||
6a488035 | 331 | } |