4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_MembershipStatusTest
extends CiviUnitTestCase
{
32 protected $_contactID;
33 protected $_contributionTypeID;
34 protected $_membershipTypeID;
35 protected $_membershipStatusID;
36 public $_eNoticeCompliant = TRUE;
37 protected $_apiversion;
41 'name' => 'MembershipStatus Calc',
42 'description' => 'Test all MembershipStatus Calc API methods.',
43 'group' => 'CiviCRM API Tests',
49 $this->_apiversion
= 3;
50 $this->_contactID
= $this->individualCreate();
51 $this->_membershipTypeID
= $this->membershipTypeCreate($this->_contactID
);
52 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
54 CRM_Member_PseudoConstant
::membershipType($this->_membershipTypeID
, TRUE);
55 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
59 $this->membershipStatusDelete($this->_membershipStatusID
);
60 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
61 $this->contactDelete($this->_contactID
);
64 ///////////////// civicrm_membership_status_get methods
67 * Test civicrm_membership_status_get with wrong params type
69 function testGetWrongParamsType() {
71 $result = $this->callAPIFailure('membership_status', 'get', $params);
72 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__
);
76 * Test civicrm_membership_status_get with empty params
78 function testGetEmptyParams() {
79 $params = array('version' => 3);
80 $result = civicrm_api('membership_status', 'get', $params);
81 // It should be 8 statuses, 7 default from mysql_data
82 // plus one test status added in setUp
83 $this->assertEquals(8, $result['count'], 'In line ' . __LINE__
);
87 * Test civicrm_membership_status_get. Success expected.
91 'name' => 'test status',
92 'version' => $this->_apiversion
,
94 $result = civicrm_api('membership_status', 'get', $params);
95 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
96 $this->assertEquals($result['values'][$this->_membershipStatusID
]['name'], "test status", "In line " . __LINE__
);
100 * Test civicrm_membership_status_get. Success expected.
102 function testGetLimit() {
104 'version' => $this->_apiversion
,
106 $result = civicrm_api('membership_status', 'getcount', $params);
107 $this->assertGreaterThan(1, $result, "Check more than one exists In line " . __LINE__
);
108 $params['option.limit'] = 1;
109 $result = civicrm_api('membership_status', 'getcount', $params);
110 $this->assertEquals(1, $result, "Check only 1 retrieved " . __LINE__
);
113 function testMembershipStatusesGet() {
114 $params = 'wrong type';
115 $result = civicrm_api('membership_status', 'get', $params);
116 $this->assertEquals(1, $result['is_error'],
117 "In line " . __LINE__
121 ///////////////// civicrm_membership_status_create methods
122 function testCreateWithEmptyParams() {
124 $result = $this->callAPIFailure('membership_status', 'create', $params);
127 function testCreateWithWrongParamsType() {
128 $params = 'a string';
129 $result = $this->callAPIFailure('membership_status', 'create', $params);
130 $params = array('version' =>3, 'id' => 'string');
131 $result = civicrm_api('membership_status', 'create', $params);
134 function testCreateDuplicateName() {
136 $params = array('version' =>3, 'name' => 'name');
137 $result = civicrm_api('membership_status', 'create', $params);
138 $this->assertAPISuccess($result);
139 $result = civicrm_api('membership_status', 'create', $params);
140 $this->assertEquals('A membership status with this name already exists.', $result['error_message']);
143 function testCreateWithMissingRequired() {
144 $params = array('title' => 'Does not make sense');
145 $result = $this->callAPIFailure('membership_status', 'create', $params);
148 function testCreate() {
150 'name' => 'test membership status',
151 'version' => $this->_apiversion
,
153 $result = civicrm_api('membership_status', 'create', $params);
154 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
156 $this->assertAPISuccess($result);
157 $this->assertNotNull($result['id']);
158 $this->membershipStatusDelete($result['id']);
161 function testUpdate() {
163 'name' => 'test membership status',
164 'version' => $this->_apiversion
,
166 $result = civicrm_api('membership_status', 'create', $params);
168 $result = civicrm_api('membership_status', 'get', $params);
169 $this->assertEquals('test membership status', $result['values'][$id]['name']);
173 'version' => $this->_apiversion
,
175 $result = civicrm_api('membership_status', 'create', $newParams);
176 $result = civicrm_api('membership_status', 'get', array('version' => 3, 'id' => $id));
177 $this->assertEquals('renamed', $result['values'][$id]['name']);
178 $this->membershipStatusDelete($result['id']);
182 ///////////////// civicrm_membership_status_update methods
183 //removed as none actually tested functionality - all just tested same stuff
184 //generic tests test.
188 ///////////////// civicrm_membership_status_calc methods
189 /*pending it being re-enabled
192 function testCalculateStatusWithNoMembershipID( )
194 $calcParams = array( 'title' => 'Does not make sense' );
196 $result = civicrm_api3_membership_status_calc( $calcParams );
197 $this->assertEquals( $result['is_error'], 1,"In line " . __LINE__ );
200 function testCalculateStatus( )
203 $join_date = new DateTime();
204 $start_date = new DateTime();
205 $end_date = new DateTime();
206 $join_date->modify("-5 months");
207 $start_date->modify("-5 months");
208 $end_date->modify("+7 months");
211 'contact_id' => $this->_contactID,
212 'membership_type_id' => $this->_membershipTypeID,
213 'membership_status_id' => $this->_membershipStatusID,
214 'join_date' => $join_date->format('Y-m-d'),
215 'start_date' => $start_date->format('Y-m-d'),
216 'end_date' => $end_date->format('Y-m-d') );
218 $membershipID = $this->contactMembershipCreate( $params );
219 $membershipStatusID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',$membershipID,'status_id');
220 $calcParams = array( 'membership_id' => $membershipID );
221 $result = _civicrm_api3_membership_status_calc( $calcParams );
222 $this->assertEquals( $result['is_error'], 0 );
223 $this->assertEquals( $membershipStatusID,$result['id'] );
224 $this->assertNotNull( $result['id'] );
226 $this->membershipDelete( $membershipID );
232 ///////////////// civicrm_membership_status_delete methods
233 function testDeleteEmptyParams() {
235 $result = $this->callAPIFailure('membership_status', 'delete', $params);
238 function testDeleteWrongParamsType() {
239 $params = 'incorrect value';
240 $result = $this->callAPIFailure('membership_status', 'delete', $params);
243 function testDeleteWithMissingRequired() {
244 $params = array('title' => 'Does not make sense');
245 $result = $this->callAPIFailure('membership_status', 'delete', $params);
248 function testDelete() {
249 $membershipID = $this->membershipStatusCreate();
251 'id' => $membershipID,
252 'version' => $this->_apiversion
,
254 $result = civicrm_api('membership_status', 'delete', $params);
255 $this->assertAPISuccess($result);
258 * Test that trying to delete membership status while membership still exists creates error
260 function testDeleteWithMembershipError() {
261 $membershipStatusID = $this->membershipStatusCreate();
262 $this->_contactID
= $this->individualCreate();
263 $this->_entity
= 'membership';
265 'contact_id' => $this->_contactID
,
266 'membership_type_id' => $this->_membershipTypeID
,
267 'join_date' => '2009-01-21',
268 'start_date' => '2009-01-21',
269 'end_date' => '2009-12-21',
270 'source' => 'Payment',
272 'status_id' => $membershipStatusID,
276 $result = civicrm_api('membership', 'create', $params);
277 $membershipID = $result['id'];
280 'id' => $membershipStatusID,
281 'version' => $this->_apiversion
,
283 $result = $this->callAPIFailure('membership_status', 'delete', $params);
285 civicrm_api('Membership', 'Delete', array(
286 'id' => $membershipID,
287 'version' => $this->_apiversion
,
290 $result = civicrm_api('membership_status', 'delete', $params);
291 $this->assertAPISuccess($result, 'In line ' . __LINE__
);