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 $_contributionTypeID;
19 protected $_apiversion;
20 protected $_entity = 'MembershipType';
25 public function setUp() {
27 $this->useTransaction(TRUE);
28 $this->_apiversion
= 3;
29 $this->_contactID
= $this->organizationCreate();
33 * Get the membership without providing an ID.
35 * This should return an empty array but not an error.
37 public function testGetWithoutId() {
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',
48 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
49 $this->assertEquals($membershipType['count'], 0);
55 public function testGet() {
56 $id = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID
]);
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);
72 * Test create with missing mandatory field.
74 public function testCreateWithoutMemberOfContactId() {
76 'name' => '60+ Membership',
77 'description' => 'people above 60 are given health instructions',
78 'financial_type_id' => 1,
80 'minimum_fee' => '200',
81 'duration_unit' => 'month',
82 'duration_interval' => '10',
83 'period_type' => 'rolling',
84 'visibility' => 'public',
87 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
91 * Test successful create.
93 public function testCreate() {
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,
100 'minimum_fee' => '200',
101 'duration_unit' => 'month',
102 'duration_interval' => '10',
103 'period_type' => 'rolling',
104 'visibility' => 'public',
107 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__
, __FILE__
);
108 $this->assertNotNull($membershipType['values']);
109 $this->membershipTypeDelete(['id' => $membershipType['id']]);
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.
116 public function testCreateWithoutDomainId() {
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',
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);
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);
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);
144 * CRM-20010 Tests period_type is required for MemberType create
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,
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',
171 'name' => 'Updated General',
172 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
173 'duration_unit' => 'month',
174 'duration_interval' => '10',
175 'period_type' => 'fixed',
179 $this->callAPISuccess('membership_type', 'update', $params);
181 $this->getAndCheck($params, $id, $this->_entity
);
185 * Test successful delete.
187 public function testDelete() {
188 $membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->organizationCreate()]);
190 'id' => $membershipTypeID,
193 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__
, __FILE__
);
197 * Delete test that could do with a decent comment block.
199 * I can't skim this & understand it so if anyone does explain it here.
201 public function testDeleteRelationshipTypesUsedByMembershipType() {
202 $rel1 = $this->relationshipTypeCreate([
203 'name_a_b' => 'abcde',
204 'name_b_a' => 'abcde',
206 $rel2 = $this->relationshipTypeCreate([
207 'name_a_b' => 'fghij',
208 'name_b_a' => 'fghij',
210 $rel3 = $this->relationshipTypeCreate([
211 'name_a_b' => 'lkmno',
212 'name_b_a' => 'lkmno',
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'],
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']);
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']);
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']));
237 * Test that membership type getlist returns an array of enabled membership types.
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']);
250 * Test priceField values are correctly created for membership type
251 * selected in contribution pages.
253 public function testEnableMembershipTypeOnContributionPage() {
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",
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',
270 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
271 'name' => 'membership_amount',
272 'label' => 'Membership Amount',
274 'financial_type_id' => 'Donation',
275 'format.only_id' => TRUE,
276 'membership_type_id' => $memType[1],
277 'price_field_id' => $field['id'],
280 $memType[2] = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID
, 'minimum_fee' => 200]);
282 'id' => $field['id'],
283 'label' => 'Membership Amount',
284 'html_type' => 'Radio',
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;
296 $priceField = CRM_Price_BAO_PriceField
::create($fieldParams);
297 $this->assertEquals($priceField->id
, $fieldParams['id']);
299 //Update membership type name and visibility
302 'name' => 'General - Edited',
303 'visibility' => 'Admin',
304 'financial_type_id' => 1,
305 'minimum_fee' => 300,
306 'description' => 'Test edit description',
308 $this->callAPISuccess('membership_type', 'create', $updateParams);
309 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', [
311 'membership_type_id' => $memType[1],
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'])));
325 foreach ($memType as $type) {
326 $this->callAPISuccess('membership_type', 'delete', ['id' => $type]);