CRM-13014 support userLoginFinalize() for UF classes (fix civimobile)
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipStatusTest.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 require_once 'CiviTest/CiviUnitTestCase.php';
31 class api_v3_MembershipStatusTest extends CiviUnitTestCase {
32
33 protected $_contactID;
34 protected $_contributionTypeID;
35 protected $_membershipTypeID;
36 protected $_membershipStatusID;
37 public $_eNoticeCompliant = TRUE;
38 protected $_apiversion;
39
40 function get_info() {
41 return array(
42 'name' => 'MembershipStatus Calc',
43 'description' => 'Test all MembershipStatus Calc API methods.',
44 'group' => 'CiviCRM API Tests',
45 );
46 }
47
48 function setUp() {
49 parent::setUp();
50 $this->_apiversion = 3;
51 $this->_contactID = $this->individualCreate();
52 $this->_membershipTypeID = $this->membershipTypeCreate($this->_contactID);
53 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
54
55 CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE);
56 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
57 }
58
59 function tearDown() {
60 $this->membershipStatusDelete($this->_membershipStatusID);
61 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
62 $this->contactDelete($this->_contactID);
63 }
64
65 ///////////////// civicrm_membership_status_get methods
66
67 /**
68 * Test civicrm_membership_status_get with wrong params type
69 */
70 function testGetWrongParamsType() {
71 $params = 'a string';
72 $result = civicrm_api('membership_status', 'get', $params);
73
74 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
75 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
76 }
77
78 /**
79 * Test civicrm_membership_status_get with empty params
80 */
81 function testGetEmptyParams() {
82 $params = array('version' => 3);
83 $result = civicrm_api('membership_status', 'get', $params);
84 // It should be 8 statuses, 7 default from mysql_data
85 // plus one test status added in setUp
86 $this->assertEquals(8, $result['count'], 'In line ' . __LINE__);
87 }
88
89 /**
90 * Test civicrm_membership_status_get. Success expected.
91 */
92 function testGet() {
93 $params = array(
94 'name' => 'test status',
95 'version' => $this->_apiversion,
96 );
97 $result = civicrm_api('membership_status', 'get', $params);
98 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
99 $this->assertEquals($result['values'][$this->_membershipStatusID]['name'], "test status", "In line " . __LINE__);
100 }
101
102 /**
103 * Test civicrm_membership_status_get. Success expected.
104 */
105 function testGetLimit() {
106 $params = array(
107 'version' => $this->_apiversion,
108 );
109 $result = civicrm_api('membership_status', 'getcount', $params);
110 $this->assertGreaterThan(1, $result, "Check more than one exists In line " . __LINE__);
111 $params['option.limit'] = 1;
112 $result = civicrm_api('membership_status', 'getcount', $params);
113 $this->assertEquals(1, $result, "Check only 1 retrieved " . __LINE__);
114 }
115
116 function testMembershipStatusesGet() {
117 $params = 'wrong type';
118 $result = civicrm_api('membership_status', 'get', $params);
119 $this->assertEquals(1, $result['is_error'],
120 "In line " . __LINE__
121 );
122 }
123
124 ///////////////// civicrm_membership_status_create methods
125 function testCreateWithEmptyParams() {
126 $params = array();
127 $result = civicrm_api('membership_status', 'create', $params);
128 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
129 }
130
131 function testCreateWithWrongParamsType() {
132 $params = 'a string';
133 $result = civicrm_api('membership_status', 'create', $params);
134 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
135 $params = array('version' =>3, 'id' => 'string');
136 $result = civicrm_api('membership_status', 'create', $params);
137 }
138
139 function testCreateDuplicateName() {
140
141 $params = array('version' =>3, 'name' => 'name');
142 $result = civicrm_api('membership_status', 'create', $params);
143 $this->assertAPISuccess($result);
144 $result = civicrm_api('membership_status', 'create', $params);
145 $this->assertEquals('A membership status with this name already exists.', $result['error_message']);
146 }
147
148 function testCreateWithMissingRequired() {
149 $params = array('title' => 'Does not make sense');
150 $result = civicrm_api('membership_status', 'create', $params);
151 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
152 }
153
154 function testCreate() {
155 $params = array(
156 'name' => 'test membership status',
157 'version' => $this->_apiversion,
158 );
159 $result = civicrm_api('membership_status', 'create', $params);
160 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
161
162 $this->assertEquals($result['is_error'], 0);
163 $this->assertNotNull($result['id']);
164 $this->membershipStatusDelete($result['id']);
165 }
166
167 function testUpdate() {
168 $params = array(
169 'name' => 'test membership status',
170 'version' => $this->_apiversion,
171 );
172 $result = civicrm_api('membership_status', 'create', $params);
173 $id = $result['id'];
174 $result = civicrm_api('membership_status', 'get', $params);
175 $this->assertEquals('test membership status', $result['values'][$id]['name']);
176 $newParams = array(
177 'id' => $id,
178 'name' => 'renamed',
179 'version' => $this->_apiversion,
180 );
181 $result = civicrm_api('membership_status', 'create', $newParams);
182 $result = civicrm_api('membership_status', 'get', array('version' => 3, 'id' => $id));
183 $this->assertEquals('renamed', $result['values'][$id]['name']);
184 $this->membershipStatusDelete($result['id']);
185 }
186
187
188 ///////////////// civicrm_membership_status_update methods
189 //removed as none actually tested functionality - all just tested same stuff
190 //generic tests test.
191
192
193
194 ///////////////// civicrm_membership_status_calc methods
195 /*pending it being re-enabled
196
197
198 function testCalculateStatusWithNoMembershipID( )
199 {
200 $calcParams = array( 'title' => 'Does not make sense' );
201
202 $result = civicrm_api3_membership_status_calc( $calcParams );
203 $this->assertEquals( $result['is_error'], 1,"In line " . __LINE__ );
204 }
205
206 function testCalculateStatus( )
207 {
208
209 $join_date = new DateTime();
210 $start_date = new DateTime();
211 $end_date = new DateTime();
212 $join_date->modify("-5 months");
213 $start_date->modify("-5 months");
214 $end_date->modify("+7 months");
215
216 $params = array(
217 'contact_id' => $this->_contactID,
218 'membership_type_id' => $this->_membershipTypeID,
219 'membership_status_id' => $this->_membershipStatusID,
220 'join_date' => $join_date->format('Y-m-d'),
221 'start_date' => $start_date->format('Y-m-d'),
222 'end_date' => $end_date->format('Y-m-d') );
223
224 $membershipID = $this->contactMembershipCreate( $params );
225 $membershipStatusID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership',$membershipID,'status_id');
226 $calcParams = array( 'membership_id' => $membershipID );
227 $result = _civicrm_api3_membership_status_calc( $calcParams );
228 $this->assertEquals( $result['is_error'], 0 );
229 $this->assertEquals( $membershipStatusID,$result['id'] );
230 $this->assertNotNull( $result['id'] );
231
232 $this->membershipDelete( $membershipID );
233 }
234 */
235
236
237
238 ///////////////// civicrm_membership_status_delete methods
239 function testDeleteEmptyParams() {
240 $params = array();
241 $result = civicrm_api('membership_status', 'delete', $params);
242 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
243 }
244
245 function testDeleteWrongParamsType() {
246 $params = 'incorrect value';
247 $result = civicrm_api('membership_status', 'delete', $params);
248 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
249 }
250
251 function testDeleteWithMissingRequired() {
252 $params = array('title' => 'Does not make sense');
253 $result = civicrm_api('membership_status', 'delete', $params);
254 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__);
255 }
256
257 function testDelete() {
258 $membershipID = $this->membershipStatusCreate();
259 $params = array(
260 'id' => $membershipID,
261 'version' => $this->_apiversion,
262 );
263 $result = civicrm_api('membership_status', 'delete', $params);
264 $this->assertEquals($result['is_error'], 0);
265 }
266 /*
267 * Test that trying to delete membership status while membership still exists creates error
268 */
269 function testDeleteWithMembershipError() {
270 $membershipStatusID = $this->membershipStatusCreate();
271 $this->_contactID = $this->individualCreate();
272 $this->_entity = 'membership';
273 $params = array(
274 'contact_id' => $this->_contactID,
275 'membership_type_id' => $this->_membershipTypeID,
276 'join_date' => '2009-01-21',
277 'start_date' => '2009-01-21',
278 'end_date' => '2009-12-21',
279 'source' => 'Payment',
280 'is_override' => 1,
281 'status_id' => $membershipStatusID,
282 'version' => 3,
283 );
284
285 $result = civicrm_api('membership', 'create', $params);
286 $membershipID = $result['id'];
287
288 $params = array(
289 'id' => $membershipStatusID,
290 'version' => $this->_apiversion,
291 );
292 $result = civicrm_api('membership_status', 'delete', $params);
293 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
294
295 civicrm_api('Membership', 'Delete', array(
296 'id' => $membershipID,
297 'version' => $this->_apiversion,
298 ));
299
300 $result = civicrm_api('membership_status', 'delete', $params);
301 $this->assertEquals($result['is_error'], 0, 'In line ' . __LINE__);
302 }
303 }
304