3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class api_v3_MembershipTypeTest
16 class api_v3_MembershipTypeTest
extends CiviUnitTestCase
{
17 protected $_contactID;
18 protected $_entity = 'MembershipType';
23 public function setUp() {
25 $this->useTransaction(TRUE);
26 $this->_contactID
= $this->organizationCreate();
30 * Get the membership without providing an ID.
32 * This should return an empty array but not an error.
34 * @dataProvider versionThreeAndFour
36 * @param bool $version
38 * @throws \CRM_Core_Exception
40 public function testGetWithoutId($version) {
41 $this->_apiversion
= $version;
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',
52 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
53 $this->assertEquals($membershipType['count'], 0);
59 * @dataProvider versionThreeAndFour
61 * @param bool $version
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);
78 * Test create with missing mandatory field.
80 * @dataProvider versionThreeAndFour
82 * @param bool $version
84 public function testCreateWithoutMemberOfContactId($version) {
85 $this->_apiversion
= $version;
87 'name' => '60+ Membership',
88 'description' => 'people above 60 are given health instructions',
89 'financial_type_id' => 1,
91 'minimum_fee' => '200',
92 'duration_unit' => 'month',
93 'duration_interval' => '10',
94 'period_type' => 'rolling',
95 'visibility' => 'public',
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);
103 * Test successful create.
105 * @dataProvider versionThreeAndFour
107 * @param bool $version
109 public function testCreate($version) {
110 $this->_apiversion
= $version;
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,
117 'minimum_fee' => '200',
118 'duration_unit' => 'month',
119 'duration_interval' => '10',
120 'period_type' => 'rolling',
121 'visibility' => 'public',
124 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
125 $this->assertNotNull($membershipType['values']);
126 $this->membershipTypeDelete(['id' => $membershipType['id']]);
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.
133 * @dataProvider versionThreeAndFour
135 * @param bool $version
137 * @throws \CRM_Core_Exception
139 public function testCreateWithoutDomainId($version) {
140 $this->_apiversion
= $version;
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',
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);
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);
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);
168 * CRM-20010 Tests period_type is required for MemberType create
170 * @dataProvider versionThreeAndFour
172 * @param bool $version
174 public function testMemberTypePeriodTypeRequired($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,
190 * @dataProvider versionThreeAndFour
192 * @param bool $version
194 * @throws \CRM_Core_Exception
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',
207 'name' => 'Updated General',
208 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
209 'duration_unit' => 'month',
210 'duration_interval' => '10',
211 'period_type' => 'fixed',
215 $this->callAPISuccess('membership_type', 'update', $params);
217 $this->getAndCheck($params, $id, $this->_entity
);
221 * Test successful delete.
223 * @dataProvider versionThreeAndFour
225 * @param bool $version
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__
);
235 * Delete test that could do with a decent comment block.
237 * I can't skim this & understand it so if anyone does explain it here.
239 public function testDeleteRelationshipTypesUsedByMembershipType() {
240 $rel1 = $this->relationshipTypeCreate([
241 'name_a_b' => 'abcde',
242 'name_b_a' => 'abcde',
244 $rel2 = $this->relationshipTypeCreate([
245 'name_a_b' => 'fghij',
246 'name_b_a' => 'fghij',
248 $rel3 = $this->relationshipTypeCreate([
249 'name_a_b' => 'lkmno',
250 'name_b_a' => 'lkmno',
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'],
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']);
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']);
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']));
275 * Test that membership type getlist returns an array of enabled membership types.
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']);
288 * Test priceField values are correctly created for membership type
289 * selected in contribution pages.
291 public function testEnableMembershipTypeOnContributionPage() {
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',
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',
308 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
309 'name' => 'membership_amount',
310 'label' => 'Membership Amount',
312 'financial_type_id' => 'Donation',
313 'format.only_id' => TRUE,
314 'membership_type_id' => $memType[1],
315 'price_field_id' => $field['id'],
318 $memType[2] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID
, 'minimum_fee' => 200]);
320 'id' => $field['id'],
321 'label' => 'Membership Amount',
322 'html_type' => 'Radio',
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;
334 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
335 $this->assertEquals($priceField->id
, $fieldParams['id']);
337 //Update membership type name and visibility
340 'name' => 'General - Edited',
341 'visibility' => 'Admin',
342 'financial_type_id' => 1,
343 'minimum_fee' => 300,
344 'description' => 'Test edit description',
346 $this->callAPISuccess('membership_type', 'create', $updateParams);
347 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', [
349 'membership_type_id' => $memType[1],
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'])));
363 foreach ($memType as $type) {
364 $this->callAPISuccess('membership_type', 'delete', ['id' => $type]);