Update Copywrite year to be 2019
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTypeTest.php
CommitLineData
6a488035 1<?php
6a488035 2/*
8d7a9d07 3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
8d7a9d07 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
8d7a9d07
CB
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_MembershipTypeTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class api_v3_MembershipTypeTest extends CiviUnitTestCase {
33 protected $_contactID;
34 protected $_contributionTypeID;
35 protected $_apiversion;
36 protected $_entity = 'MembershipType';
b7c9bc4c 37
80d714d2 38 /**
39 * Set up for tests.
40 */
00be9182 41 public function setUp() {
6a488035 42 parent::setUp();
31c45547 43 $this->useTransaction(TRUE);
6a488035 44 $this->_apiversion = 3;
baccd59e 45 $this->_contactID = $this->organizationCreate();
6a488035
TO
46 }
47
80d714d2 48 /**
49 * Get the membership without providing an ID.
50 *
51 * This should return an empty array but not an error.
52 */
00be9182 53 public function testGetWithoutId() {
6a488035
TO
54 $params = array(
55 'name' => '60+ Membership',
56 'description' => 'people above 60 are given health instructions',
3d6a42e8 57 'financial_type_id' => 1,
6a488035
TO
58 'minimum_fee' => '200',
59 'duration_unit' => 'month',
60 'duration_interval' => '10',
61 'visibility' => 'public',
6a488035
TO
62 );
63
80d714d2 64 $membershipType = $this->callAPISuccess('membership_type', 'get', $params);
65 $this->assertEquals($membershipType['count'], 0);
6a488035
TO
66 }
67
80d714d2 68 /**
69 * Test get works.
70 */
00be9182 71 public function testGet() {
75638074 72 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
6a488035
TO
73
74 $params = array(
75 'id' => $id,
6a488035 76 );
80d714d2 77 $membershipType = $this->callAPIAndDocument('membership_type', 'get', $params, __FUNCTION__, __FILE__);
78 $this->assertEquals($membershipType['values'][$id]['name'], 'General');
79 $this->assertEquals($membershipType['values'][$id]['member_of_contact_id'], $this->_contactID);
8484a5f0 80 $this->assertEquals($membershipType['values'][$id]['financial_type_id'], $this->getFinancialTypeId('Member Dues'));
80d714d2 81 $this->assertEquals($membershipType['values'][$id]['duration_unit'], 'year');
82 $this->assertEquals($membershipType['values'][$id]['duration_interval'], '1');
83 $this->assertEquals($membershipType['values'][$id]['period_type'], 'rolling');
6a488035
TO
84 $this->membershipTypeDelete($params);
85 }
86
5f1108c8 87 /**
88 * Test create with missing mandatory field.
89 */
00be9182 90 public function testCreateWithoutMemberOfContactId() {
6a488035
TO
91 $params = array(
92 'name' => '60+ Membership',
93 'description' => 'people above 60 are given health instructions',
3d6a42e8 94 'financial_type_id' => 1,
6a488035
TO
95 'domain_id' => '1',
96 'minimum_fee' => '200',
97 'duration_unit' => 'month',
98 'duration_interval' => '10',
99 'period_type' => 'rolling',
100 'visibility' => 'public',
6a488035
TO
101 );
102
80d714d2 103 $this->callAPIFailure('membership_type', 'create', $params, 'Mandatory key(s) missing from params array: member_of_contact_id');
6a488035
TO
104 }
105
5f1108c8 106 /**
107 * Test successful create.
108 */
00be9182 109 public function testCreate() {
6a488035
TO
110 $params = array(
111 'name' => '40+ Membership',
112 'description' => 'people above 40 are given health instructions',
113 'member_of_contact_id' => $this->_contactID,
3d6a42e8 114 'financial_type_id' => 1,
6a488035
TO
115 'domain_id' => '1',
116 'minimum_fee' => '200',
117 'duration_unit' => 'month',
118 'duration_interval' => '10',
119 'period_type' => 'rolling',
120 'visibility' => 'public',
6a488035
TO
121 );
122
edc9b94e
EM
123 $membershipType = $this->callAPIAndDocument('membership_type', 'create', $params, __FUNCTION__, __FILE__);
124 $this->assertNotNull($membershipType['values']);
125 $this->membershipTypeDelete(array('id' => $membershipType['id']));
6a488035
TO
126 }
127
edc9b94e 128 /**
fb38ab8d 129 * Domain ID can be intuited..
a91041c5 130 * 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 131 */
fb38ab8d 132 public function testCreateWithoutDomainId() {
6a488035
TO
133 $params = array(
134 'name' => '60+ Membership',
135 'description' => 'people above 60 are given health instructions',
136 'member_of_contact_id' => $this->_contactID,
3d6a42e8 137 'financial_type_id' => 1,
6a488035
TO
138 'minimum_fee' => '1200',
139 'duration_unit' => 'month',
140 'duration_interval' => '10',
141 'period_type' => 'rolling',
92915c55
TO
142 'visibility' => 'public',
143 );
6a488035 144
fb38ab8d 145 $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
146 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
147 $this->assertEquals(CRM_Core_Config::domainID(), $domainID);
148
149 $this->callAPISuccess('membership_type', 'create', ['domain_id' => 2, 'id' => $membershipType['id']]);
150 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
151 $this->assertEquals(2, $domainID);
152
153 $this->callAPISuccess('membership_type', 'create', ['id' => $membershipType['id'], 'description' => 'Cool member']);
154 $domainID = $this->callAPISuccessGetValue('MembershipType', ['return' => 'domain_id', 'id' => $membershipType['id']]);
155 $this->assertEquals(2, $domainID);
156
6a488035
TO
157 }
158
5b1b8db2
PH
159 /**
160 * CRM-20010 Tests period_type is required for MemberType create
161 */
162 public function testMemberTypePeriodiTypeRequired() {
163 $this->callAPIFailure('MembershipType', 'create', array(
164 'domain_id' => "Default Domain Name",
165 'member_of_contact_id' => 1,
166 'financial_type_id' => "Member Dues",
167 'duration_unit' => "month",
168 'duration_interval' => 1,
169 'name' => "Standard Member",
170 'minimum_fee' => 100,
171 ));
172 }
173
5f1108c8 174 /**
175 * Test update.
176 */
00be9182 177 public function testUpdate() {
75638074 178 $id = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'financial_type_id' => 2));
5f1108c8 179 $newMemberOrgParams = array(
6a488035
TO
180 'organization_name' => 'New membership organisation',
181 'contact_type' => 'Organization',
92915c55
TO
182 'visibility' => 1,
183 );
6a488035 184
6a488035
TO
185 $params = array(
186 'id' => $id,
187 'name' => 'Updated General',
5f1108c8 188 'member_of_contact_id' => $this->organizationCreate($newMemberOrgParams),
6a488035
TO
189 'duration_unit' => 'month',
190 'duration_interval' => '10',
191 'period_type' => 'fixed',
92915c55
TO
192 'domain_id' => 1,
193 );
3d6a42e8 194
d63167fc 195 $this->callAPISuccess('membership_type', 'update', $params);
6a488035
TO
196
197 $this->getAndCheck($params, $id, $this->_entity);
198 }
199
5f1108c8 200 /**
201 * Test successful delete.
202 */
00be9182 203 public function testDelete() {
5f1108c8 204 $membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->organizationCreate()));
3d6a42e8 205 $params = array(
6a488035 206 'id' => $membershipTypeID,
6a488035
TO
207 );
208
edc9b94e 209 $this->callAPIAndDocument('membership_type', 'delete', $params, __FUNCTION__, __FILE__);
6a488035 210 }
96025800 211
5f1108c8 212 /**
213 * Delete test that could do with a decent comment block.
214 *
215 * I can't skim this & understand it so if anyone does explain it here.
216 */
baccd59e
CW
217 public function testDeleteRelationshipTypesUsedByMembershipType() {
218 $rel1 = $this->relationshipTypeCreate(array(
219 'name_a_b' => 'abcde',
220 'name_b_a' => 'abcde',
221 ));
222 $rel2 = $this->relationshipTypeCreate(array(
223 'name_a_b' => 'fghij',
224 'name_b_a' => 'fghij',
225 ));
226 $rel3 = $this->relationshipTypeCreate(array(
227 'name_a_b' => 'lkmno',
228 'name_b_a' => 'lkmno',
229 ));
230 $id = $this->membershipTypeCreate(array(
231 'member_of_contact_id' => $this->_contactID,
232 'relationship_type_id' => array($rel1, $rel2, $rel3),
233 'relationship_direction' => array('a_b', 'a_b', 'b_a'),
234 ));
235
236 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel2));
237 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
238 $this->assertEquals(array($rel1, $rel3), $newValues['relationship_type_id']);
239 $this->assertEquals(array('a_b', 'b_a'), $newValues['relationship_direction']);
240
241 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel1));
242 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
243 $this->assertEquals(array($rel3), $newValues['relationship_type_id']);
244 $this->assertEquals(array('b_a'), $newValues['relationship_direction']);
245
246 $this->callAPISuccess('RelationshipType', 'delete', array('id' => $rel3));
247 $newValues = $this->callAPISuccess('MembershipType', 'getsingle', array('id' => $id));
248 $this->assertTrue(empty($newValues['relationship_type_id']));
249 $this->assertTrue(empty($newValues['relationship_direction']));
250 }
251
35dfd73b 252 /**
253 * Test that membership type getlist returns an array of enabled membership types.
254 */
255 public function testMembershipTypeGetList() {
256 $this->membershipTypeCreate();
257 $this->membershipTypeCreate(array('name' => 'cheap-skates'));
258 $this->membershipTypeCreate(array('name' => 'disabled cheap-skates', 'is_active' => 0));
259 $result = $this->callAPISuccess('MembershipType', 'getlist', array());
260 $this->assertEquals(2, $result['count']);
261 $this->assertEquals('cheap-skates', $result['values'][0]['label']);
262 $this->assertEquals('General', $result['values'][1]['label']);
263 }
264
026a0d69
JP
265 /**
266 * Test priceField values are correctly created for membership type
267 * selected in contribution pages.
268 */
269 public function testEnableMembershipTypeOnContributionPage() {
270 $memType = array();
271 $memType[1] = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'minimum_fee' => 100));
5afcf706
JP
272 $priceSet = $this->callAPISuccess('price_set', 'create', array(
273 'title' => "test priceset",
274 'name' => "test_priceset",
275 'extends' => "CiviMember",
276 'is_quick_config' => 1,
277 'financial_type_id' => "Member Dues",
026a0d69 278 ));
5afcf706 279 $priceSet = $priceSet['id'];
026a0d69
JP
280 $field = $this->callAPISuccess('price_field', 'create', array(
281 'price_set_id' => $priceSet,
282 'name' => 'membership_amount',
283 'label' => 'Membership Amount',
284 'html_type' => 'Radio',
285 ));
286 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
287 'name' => 'membership_amount',
288 'label' => 'Membership Amount',
289 'amount' => 100,
290 'financial_type_id' => 'Donation',
291 'format.only_id' => TRUE,
292 'membership_type_id' => $memType[1],
293 'price_field_id' => $field['id'],
294 ));
295
296 $memType[2] = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID, 'minimum_fee' => 200));
297 $fieldParams = array(
298 'id' => $field['id'],
299 'label' => 'Membership Amount',
300 'html_type' => 'Radio',
301 );
302 foreach ($memType as $rowCount => $type) {
303 $membetype = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($type);
304 $fieldParams['option_id'] = array(1 => $priceFieldValue['id']);
305 $fieldParams['option_label'][$rowCount] = CRM_Utils_Array::value('name', $membetype);
306 $fieldParams['option_amount'][$rowCount] = CRM_Utils_Array::value('minimum_fee', $membetype, 0);
307 $fieldParams['option_weight'][$rowCount] = CRM_Utils_Array::value('weight', $membetype);
308 $fieldParams['option_description'][$rowCount] = CRM_Utils_Array::value('description', $membetype);
309 $fieldParams['option_financial_type_id'][$rowCount] = CRM_Utils_Array::value('financial_type_id', $membetype);
310 $fieldParams['membership_type_id'][$rowCount] = $type;
311 }
312 $priceField = CRM_Price_BAO_PriceField::create($fieldParams);
313 $this->assertEquals($priceField->id, $fieldParams['id']);
314
5afcf706
JP
315 //Update membership type name and visibility
316 $updateParams = array(
317 'id' => $memType[1],
318 'name' => 'General - Edited',
319 'visibility' => 'Admin',
320 'financial_type_id' => 1,
321 'minimum_fee' => 300,
322 'description' => 'Test edit description',
323 );
324 $this->callAPISuccess('membership_type', 'create', $updateParams);
325 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', array(
326 'sequential' => 1,
327 'membership_type_id' => $memType[1],
328 ));
329 //Verify if membership type updates are copied to pricefield value.
330 foreach ($priceFieldValue['values'] as $key => $value) {
331 $setId = $this->callAPISuccessGetValue('PriceField', array('return' => "price_set_id", 'id' => $value['price_field_id']));
332 if ($setId == $priceSet) {
333 $this->assertEquals($value['label'], $updateParams['name']);
334 $this->assertEquals($value['description'], $updateParams['description']);
335 $this->assertEquals((int) $value['amount'], $updateParams['minimum_fee']);
336 $this->assertEquals($value['financial_type_id'], $updateParams['financial_type_id']);
337 $this->assertEquals($value['visibility_id'], CRM_Price_BAO_PriceField::getVisibilityOptionID(strtolower($updateParams['visibility'])));
338 }
339 }
340
026a0d69
JP
341 foreach ($memType as $type) {
342 $this->callAPISuccess('membership_type', 'delete', array('id' => $type));
343 }
344
345 }
346
6a488035 347}