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