Merge pull request #1005 from GiantRobot/CRM-12804
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipStatusTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_MembershipStatusTest extends CiviUnitTestCase {
31
32 protected $_contactID;
33 protected $_contributionTypeID;
34 protected $_membershipTypeID;
35 protected $_membershipStatusID;
36 public $_eNoticeCompliant = TRUE;
37 protected $_apiversion;
38
39 function get_info() {
40 return array(
41 'name' => 'MembershipStatus Calc',
42 'description' => 'Test all MembershipStatus Calc API methods.',
43 'group' => 'CiviCRM API Tests',
44 );
45 }
46
47 function setUp() {
48 parent::setUp();
49 $this->_apiversion = 3;
50 $this->_contactID = $this->individualCreate();
51 $this->_membershipTypeID = $this->membershipTypeCreate($this->_contactID);
52 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
53
54 CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE);
55 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
56 }
57
58 function tearDown() {
59 $this->membershipStatusDelete($this->_membershipStatusID);
60 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
61 $this->contactDelete($this->_contactID);
62 }
63
64 ///////////////// civicrm_membership_status_get methods
65
66 /**
67 * Test civicrm_membership_status_get with wrong params type
68 */
69 function testGetWrongParamsType() {
70 $params = 'a string';
71 $result = $this->callAPIFailure('membership_status', 'get', $params);
72 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
73 }
74
75 /**
76 * Test civicrm_membership_status_get with empty params
77 */
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__);
84 }
85
86 /**
87 * Test civicrm_membership_status_get. Success expected.
88 */
89 function testGet() {
90 $params = array(
91 'name' => 'test status',
92 'version' => $this->_apiversion,
93 );
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__);
97 }
98
99 /**
100 * Test civicrm_membership_status_get. Success expected.
101 */
102 function testGetLimit() {
103 $params = array(
104 'version' => $this->_apiversion,
105 );
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__);
111 }
112
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__
118 );
119 }
120
121 ///////////////// civicrm_membership_status_create methods
122 function testCreateWithEmptyParams() {
123 $params = array();
124 $result = $this->callAPIFailure('membership_status', 'create', $params);
125 }
126
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);
132 }
133
134 function testCreateDuplicateName() {
135
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']);
141 }
142
143 function testCreateWithMissingRequired() {
144 $params = array('title' => 'Does not make sense');
145 $result = $this->callAPIFailure('membership_status', 'create', $params);
146 }
147
148 function testCreate() {
149 $params = array(
150 'name' => 'test membership status',
151 'version' => $this->_apiversion,
152 );
153 $result = civicrm_api('membership_status', 'create', $params);
154 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
155
156 $this->assertAPISuccess($result);
157 $this->assertNotNull($result['id']);
158 $this->membershipStatusDelete($result['id']);
159 }
160
161 function testUpdate() {
162 $params = array(
163 'name' => 'test membership status',
164 'version' => $this->_apiversion,
165 );
166 $result = civicrm_api('membership_status', 'create', $params);
167 $id = $result['id'];
168 $result = civicrm_api('membership_status', 'get', $params);
169 $this->assertEquals('test membership status', $result['values'][$id]['name']);
170 $newParams = array(
171 'id' => $id,
172 'name' => 'renamed',
173 'version' => $this->_apiversion,
174 );
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']);
179 }
180
181
182 ///////////////// civicrm_membership_status_update methods
183 //removed as none actually tested functionality - all just tested same stuff
184 //generic tests test.
185
186
187
188 ///////////////// civicrm_membership_status_calc methods
189 /*pending it being re-enabled
190
191
192 function testCalculateStatusWithNoMembershipID( )
193 {
194 $calcParams = array( 'title' => 'Does not make sense' );
195
196 $result = civicrm_api3_membership_status_calc( $calcParams );
197 $this->assertEquals( $result['is_error'], 1,"In line " . __LINE__ );
198 }
199
200 function testCalculateStatus( )
201 {
202
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");
209
210 $params = array(
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') );
217
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'] );
225
226 $this->membershipDelete( $membershipID );
227 }
228 */
229
230
231
232 ///////////////// civicrm_membership_status_delete methods
233 function testDeleteEmptyParams() {
234 $params = array();
235 $result = $this->callAPIFailure('membership_status', 'delete', $params);
236 }
237
238 function testDeleteWrongParamsType() {
239 $params = 'incorrect value';
240 $result = $this->callAPIFailure('membership_status', 'delete', $params);
241 }
242
243 function testDeleteWithMissingRequired() {
244 $params = array('title' => 'Does not make sense');
245 $result = $this->callAPIFailure('membership_status', 'delete', $params);
246 }
247
248 function testDelete() {
249 $membershipID = $this->membershipStatusCreate();
250 $params = array(
251 'id' => $membershipID,
252 'version' => $this->_apiversion,
253 );
254 $result = civicrm_api('membership_status', 'delete', $params);
255 $this->assertAPISuccess($result);
256 }
257 /*
258 * Test that trying to delete membership status while membership still exists creates error
259 */
260 function testDeleteWithMembershipError() {
261 $membershipStatusID = $this->membershipStatusCreate();
262 $this->_contactID = $this->individualCreate();
263 $this->_entity = 'membership';
264 $params = array(
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',
271 'is_override' => 1,
272 'status_id' => $membershipStatusID,
273 'version' => 3,
274 );
275
276 $result = civicrm_api('membership', 'create', $params);
277 $membershipID = $result['id'];
278
279 $params = array(
280 'id' => $membershipStatusID,
281 'version' => $this->_apiversion,
282 );
283 $result = $this->callAPIFailure('membership_status', 'delete', $params);
284
285 civicrm_api('Membership', 'Delete', array(
286 'id' => $membershipID,
287 'version' => $this->_apiversion,
288 ));
289
290 $result = civicrm_api('membership_status', 'delete', $params);
291 $this->assertAPISuccess($result, 'In line ' . __LINE__);
292 }
293 }
294