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