Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / tests / phpunit / CRM / Member / BAO / MembershipTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class CRM_Member_BAO_MembershipTypeTest
33 */
34 class CRM_Member_BAO_MembershipTypeTest extends CiviUnitTestCase {
35 /**
36 * @return array
37 */
38 function get_info() {
39 return array(
40 'name' => 'MembershipType BAOs',
41 'description' => 'Test all Member_BAO_MembershipType methods.',
42 'group' => 'CiviCRM BAO Tests',
43 );
44 }
45
46 function setUp() {
47 parent::setUp();
48
49 //create relationship
50 $params = array(
51 'name_a_b' => 'Relation 1',
52 'name_b_a' => 'Relation 2',
53 'contact_type_a' => 'Individual',
54 'contact_type_b' => 'Organization',
55 'is_reserved' => 1,
56 'is_active' => 1,
57 );
58 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
59 $this->_orgContactID = $this->organizationCreate();
60 $this->_indiviContactID = $this->individualCreate();
61 $this->_financialTypeId = 1;
62 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
63 }
64
65 /**
66 * Tears down the fixture, for example, closes a network connection.
67 * This method is called after a test is executed.
68 *
69 */
70 function tearDown() {
71 $this->relationshipTypeDelete($this->_relationshipTypeId);
72 $this->membershipStatusDelete($this->_membershipStatusID);
73 $this->contactDelete($this->_orgContactID);
74 $this->contactDelete($this->_indiviContactID);
75 }
76
77 /* check function add()
78 *
79 */
80 function testAdd() {
81 $ids = array();
82 $params = array(
83 'name' => 'test type',
84 'domain_id' => 1,
85 'description' => NULL,
86 'minimum_fee' => 10,
87 'duration_unit' => 'year',
88 'member_of_contact_id' => $this->_orgContactID,
89 'period_type' => 'fixed',
90 'duration_interval' => 1,
91 'financial_type_id' => $this->_financialTypeId,
92 'relationship_type_id' => $this->_relationshipTypeId,
93 'visibility' => 'Public',
94 );
95
96 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
97
98 $membership = $this->assertDBNotNull('CRM_Member_BAO_MembershipType', $this->_orgContactID,
99 'name', 'member_of_contact_id',
100 'Database check on updated membership record.'
101 );
102
103 $this->assertEquals($membership, 'test type', 'Verify membership type name.');
104 $this->membershipTypeDelete(array('id' => $membershipType->id));
105 }
106
107 /* check function retrive()
108 *
109 */
110 function testRetrieve() {
111 $ids = array();
112 $params = array(
113 'name' => 'General',
114 'description' => NULL,
115 'domain_id' => 1,
116 'minimum_fee' => 100,
117 'duration_unit' => 'year',
118 'period_type' => 'fixed',
119 'member_of_contact_id' => $this->_orgContactID,
120 'duration_interval' => 1,
121 'financial_type_id' => $this->_financialTypeId,
122 'relationship_type_id' => $this->_relationshipTypeId,
123 'visibility' => 'Public',
124 );
125 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
126
127 $params = array('name' => 'General');
128 $default = array();
129 $result = CRM_Member_BAO_MembershipType::retrieve($params, $default);
130 $this->assertEquals($result->name, 'General', 'Verify membership type name.');
131 $this->membershipTypeDelete(array('id' => $membershipType->id));
132 }
133
134 /* check function isActive()
135 *
136 */
137 function testSetIsActive() {
138 $ids = array();
139 $params = array(
140 'name' => 'General',
141 'description' => NULL,
142 'domain_id' => 1,
143 'minimum_fee' => 100,
144 'duration_unit' => 'year',
145 'period_type' => 'fixed',
146 'duration_interval' => 1,
147 'member_of_contact_id' => $this->_orgContactID,
148 'financial_type_id' => $this->_financialTypeId,
149 'relationship_type_id' => $this->_relationshipTypeId,
150 'visibility' => 'Public',
151 'is_active' => 1,
152 );
153 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
154
155 CRM_Member_BAO_MembershipType::setIsActive($membership->id, 0);
156
157 $isActive = $this->assertDBNotNull('CRM_Member_BAO_MembershipType', $membership->id,
158 'is_active', 'id',
159 'Database check on membership type status.'
160 );
161
162 $this->assertEquals($isActive, 0, 'Verify membership type status.');
163 $this->membershipTypeDelete(array('id' => $membership->id));
164 }
165
166 /* check function del()
167 *
168 */
169 function testdel() {
170 $ids = array();
171 $params = array(
172 'name' => 'General',
173 'description' => NULL,
174 'minimum_fee' => 100,
175 'domain_id' => 1,
176 'duration_unit' => 'year',
177 'period_type' => 'fixed',
178 'member_of_contact_id' => $this->_orgContactID,
179 'duration_interval' => 1,
180 'financial_type_id' => $this->_financialTypeId,
181 'relationship_type_id' => $this->_relationshipTypeId,
182 'visibility' => 'Public',
183 'is_active' => 1,
184 );
185 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
186
187 $result = CRM_Member_BAO_MembershipType::del($membership->id);
188
189 $this->assertEquals($result, TRUE, 'Verify membership deleted.');
190 }
191
192 /* check function convertDayFormat( )
193 *
194 */
195 function testConvertDayFormat() {
196 $ids = array();
197 $params = array(
198 'name' => 'General',
199 'description' => NULL,
200 'minimum_fee' => 100,
201 'domain_id' => 1,
202 'duration_unit' => 'year',
203 'period_type' => 'fixed',
204 'member_of_contact_id' => $this->_orgContactID,
205 'fixed_period_start_day' => 1213,
206 'fixed_period_rollover_day' => 1214,
207 'duration_interval' => 1,
208 'financial_type_id' => $this->_financialTypeId,
209 'relationship_type_id' => $this->_relationshipTypeId,
210 'visibility' => 'Public',
211 'is_active' => 1,
212 );
213 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
214 $membershipType[$membership->id] = $params;
215
216 CRM_Member_BAO_MembershipType::convertDayFormat($membershipType);
217
218 $this->assertEquals($membershipType[$membership->id]['fixed_period_rollover_day'], 'Dec 14', 'Verify memberFixed Period Rollover Day.');
219 $this->membershipTypeDelete(array('id' => $membership->id));
220 }
221
222 /* check function getMembershipTypes( )
223 *
224 */
225 function testGetMembershipTypes() {
226 $ids = array();
227 $params = array(
228 'name' => 'General',
229 'description' => NULL,
230 'minimum_fee' => 100,
231 'domain_id' => 1,
232 'duration_unit' => 'year',
233 'member_of_contact_id' => $this->_orgContactID,
234 'period_type' => 'fixed',
235 'duration_interval' => 1,
236 'financial_type_id' => $this->_financialTypeId,
237 'relationship_type_id' => $this->_relationshipTypeId,
238 'visibility' => 'Public',
239 'is_active' => 1,
240 );
241 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
242 $result = CRM_Member_BAO_MembershipType::getMembershipTypes();
243 $this->assertEquals($result[$membership->id], 'General', 'Verify membership types.');
244 $this->membershipTypeDelete(array('id' => $membership->id));
245 }
246
247 /* check function getMembershipTypeDetails( )
248 *
249 */
250 function testGetMembershipTypeDetails() {
251 $ids = array();
252 $params = array(
253 'name' => 'General',
254 'description' => NULL,
255 'minimum_fee' => 100,
256 'domain_id' => 1,
257 'duration_unit' => 'year',
258 'period_type' => 'fixed',
259 'member_of_contact_id' => $this->_orgContactID,
260 'duration_interval' => 1,
261 'financial_type_id' => $this->_financialTypeId,
262 'relationship_type_id' => $this->_relationshipTypeId,
263 'visibility' => 'Public',
264 'is_active' => 1,
265 );
266 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
267 $result = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membership->id);
268
269 $this->assertEquals($result['name'], 'General', 'Verify membership type details.');
270 $this->assertEquals($result['duration_unit'], 'year', 'Verify membership types details.');
271 $this->membershipTypeDelete(array('id' => $membership->id));
272 }
273
274 /* check function getDatesForMembershipType( )
275 *
276 */
277 function testGetDatesForMembershipType() {
278 $ids = array();
279 $params = array(
280 'name' => 'General',
281 'description' => NULL,
282 'minimum_fee' => 100,
283 'domain_id' => 1,
284 'duration_unit' => 'year',
285 'member_of_contact_id' => $this->_orgContactID,
286 'period_type' => 'rolling',
287 'duration_interval' => 1,
288 'financial_type_id' => $this->_financialTypeId,
289 'relationship_type_id' => $this->_relationshipTypeId,
290 'visibility' => 'Public',
291 'is_active' => 1,
292 );
293 $membership = CRM_Member_BAO_MembershipType::add($params, $ids);
294
295 $membershipDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->id);
296 $this->assertEquals($membershipDates['start_date'], date('Ymd'), 'Verify membership types details.');
297 $this->membershipTypeDelete(array('id' => $membership->id));
298 }
299
300 /* check function getRenewalDatesForMembershipType( )
301 *
302 */
303 function testGetRenewalDatesForMembershipType() {
304 $ids = array();
305 $params = array(
306 'name' => 'General',
307 'domain_id' => 1,
308 'description' => NULL,
309 'minimum_fee' => 100,
310 'duration_unit' => 'year',
311 'member_of_contact_id' => $this->_orgContactID,
312 'period_type' => 'rolling',
313 'duration_interval' => 1,
314 'financial_type_id' => $this->_financialTypeId,
315 'relationship_type_id' => $this->_relationshipTypeId,
316 'visibility' => 'Public',
317 'is_active' => 1,
318 );
319 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
320
321 $params = array(
322 'contact_id' => $this->_indiviContactID,
323 'membership_type_id' => $membershipType->id,
324 'join_date' => '20060121000000',
325 'start_date' => '20060121000000',
326 'end_date' => '20070120000000',
327 'source' => 'Payment',
328 'is_override' => 1,
329 'status_id' => $this->_membershipStatusID,
330 );
331 $ids = array();
332 $membership = CRM_Member_BAO_Membership::create($params, $ids);
333
334 $membershipRenewDates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id);
335
336 $this->assertEquals($membershipRenewDates['start_date'], '20060121', 'Verify membership renewal start date.');
337 $this->assertEquals($membershipRenewDates['end_date'], '20080120', 'Verify membership renewal end date.');
338
339 $this->membershipDelete($membership->id);
340 $this->membershipTypeDelete(array('id' => $membershipType->id));
341 }
342
343 /* check function getMembershipTypesByOrg( )
344 *
345 */
346 function testGetMembershipTypesByOrg() {
347 $ids = array();
348 $params = array(
349 'name' => 'General',
350 'description' => NULL,
351 'domain_id' => 1,
352 'minimum_fee' => 100,
353 'duration_unit' => 'year',
354 'member_of_contact_id' => $this->_orgContactID,
355 'period_type' => 'rolling',
356 'duration_interval' => 1,
357 'financial_type_id' => $this->_financialTypeId,
358 'relationship_type_id' => $this->_relationshipTypeId,
359 'visibility' => 'Public',
360 'is_active' => 1,
361 );
362 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
363
364 $result = CRM_Member_BAO_MembershipType::getMembershipTypesByOrg($this->_orgContactID);
365 $this->assertEquals(empty($result), FALSE, 'Verify membership types for organization.');
366
367 $result = CRM_Member_BAO_MembershipType::getMembershipTypesByOrg(501);
368 $this->assertEquals(empty($result), TRUE, 'Verify membership types for organization.');
369
370 $this->membershipTypeDelete(array('id' => $membershipType->id));
371 }
372 }
373