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