| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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 | * Test APIv3 civicrm_membership functions |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | * @subpackage API_Member |
| 33 | */ |
| 34 | |
| 35 | |
| 36 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 37 | |
| 38 | class api_v3_MembershipTest extends CiviUnitTestCase { |
| 39 | protected $_apiversion; |
| 40 | protected $_contactID; |
| 41 | protected $_membershipTypeID; |
| 42 | protected $_membershipTypeID2; |
| 43 | protected $_membershipStatusID; |
| 44 | protected $__membershipID; |
| 45 | protected $_entity; |
| 46 | protected $_params; |
| 47 | |
| 48 | |
| 49 | public function setUp() { |
| 50 | // Connect to the database |
| 51 | parent::setUp(); |
| 52 | $this->_apiversion = 3; |
| 53 | $this->_contactID = $this->individualCreate(); |
| 54 | $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID)); |
| 55 | $this->_membershipTypeID2 = $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111')); |
| 56 | $this->_membershipStatusID = $this->membershipStatusCreate('test status'); |
| 57 | |
| 58 | CRM_Member_PseudoConstant::membershipType(NULL, TRUE); |
| 59 | CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); |
| 60 | CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name'); |
| 61 | |
| 62 | $this->_entity = 'Membership'; |
| 63 | $this->_params = array( |
| 64 | 'contact_id' => $this->_contactID, |
| 65 | 'membership_type_id' => $this->_membershipTypeID, |
| 66 | 'join_date' => '2009-01-21', |
| 67 | 'start_date' => '2009-01-21', |
| 68 | 'end_date' => '2009-12-21', |
| 69 | 'source' => 'Payment', |
| 70 | 'is_override' => 1, |
| 71 | 'status_id' => $this->_membershipStatusID, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | function tearDown() { |
| 76 | $this->quickCleanup(array( |
| 77 | 'civicrm_membership', |
| 78 | 'civicrm_membership_payment', |
| 79 | 'civicrm_membership_log', |
| 80 | ), |
| 81 | TRUE |
| 82 | ); |
| 83 | $this->membershipStatusDelete($this->_membershipStatusID); |
| 84 | $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2)); |
| 85 | $this->membershipTypeDelete(array('id' => $this->_membershipTypeID)); |
| 86 | $this->contactDelete($this->_contactID); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test civicrm_membership_delete() |
| 92 | */ |
| 93 | function testMembershipDelete() { |
| 94 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 95 | $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID); |
| 96 | $params = array( |
| 97 | 'id' => $membershipID |
| 98 | ); |
| 99 | $result = $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__); |
| 100 | $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID); |
| 101 | } |
| 102 | |
| 103 | function testMembershipDeleteEmpty() { |
| 104 | $params = array(); |
| 105 | $result = $this->callAPIFailure('membership', 'delete', $params); |
| 106 | } |
| 107 | |
| 108 | function testMembershipDeleteInvalidID() { |
| 109 | $params = array('id' => 'blah'); |
| 110 | $result = $this->callAPIFailure('membership', 'delete', $params); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Test civicrm_membership_delete() with invalid Membership Id |
| 115 | */ |
| 116 | function testMembershipDeleteWithInvalidMembershipId() { |
| 117 | $membershipId = 'membership'; |
| 118 | $result = $this->callAPIFailure('membership', 'delete', $membershipId); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * All other methods calls MembershipType and MembershipContact |
| 123 | * api, but putting simple test methods to control existence of |
| 124 | * these methods for backwards compatibility, also verifying basic |
| 125 | * behaviour is the same as new methods. |
| 126 | */ |
| 127 | function testContactMembershipsGet() { |
| 128 | $this->_membershipID = $this->contactMembershipCreate($this->_params); |
| 129 | $params = array(); |
| 130 | $result = $this->callAPISuccess('membership', 'get', $params); |
| 131 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 132 | 'id' => $this->_membershipID, |
| 133 | )); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Test civicrm_membership_get with params not array. |
| 138 | * Gets treated as contact_id, memberships expected. |
| 139 | */ |
| 140 | function testGetWithParamsContactId() { |
| 141 | $this->_membershipID = $this->contactMembershipCreate($this->_params); |
| 142 | $params = array( |
| 143 | 'contact_id' => $this->_contactID, |
| 144 | ); |
| 145 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 146 | |
| 147 | $result = $membership['values'][$this->_membershipID]; |
| 148 | $this->callAPISuccess('Membership', 'Delete', array( |
| 149 | 'id' => $this->_membershipID, |
| 150 | )); |
| 151 | $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__); |
| 152 | $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__); |
| 153 | $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__); |
| 154 | $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__); |
| 155 | $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__); |
| 156 | $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__); |
| 157 | $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__); |
| 158 | $this->assertEquals($result['is_override'], 1, "In line " . __LINE__); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Test civicrm_membership_get with params not array. |
| 163 | * Gets treated as contact_id, memberships expected. |
| 164 | */ |
| 165 | function testGetInSyntax() { |
| 166 | $this->_membershipID = $this->contactMembershipCreate($this->_params); |
| 167 | $this->_membershipID2 = $this->contactMembershipCreate($this->_params); |
| 168 | $this->_membershipID3 = $this->contactMembershipCreate($this->_params); |
| 169 | $params = array( |
| 170 | 'id' => array('IN' => array($this->_membershipID, $this->_membershipID3)), |
| 171 | ); |
| 172 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 173 | $this->assertEquals(2, $membership['count']); |
| 174 | $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values'])); |
| 175 | $params = array( |
| 176 | 'id' => array('NOT IN' => array($this->_membershipID, $this->_membershipID3)), |
| 177 | ); |
| 178 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 179 | $this->assertEquals(1, $membership['count']); |
| 180 | $this->assertEquals(array($this->_membershipID2), array_keys($membership['values'])); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Test civicrm_membership_get with params not array. |
| 186 | * Gets treated as contact_id, memberships expected. |
| 187 | */ |
| 188 | function testGetInSyntaxOnContactID() { |
| 189 | $this->_membershipID = $this->contactMembershipCreate($this->_params); |
| 190 | $contact2 = $this->individualCreate(); |
| 191 | $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine')); |
| 192 | $this->_membershipID2 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact2))); |
| 193 | $this->_membershipID3 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact3))); |
| 194 | $params = array( |
| 195 | 'contact_id' => array('IN' => array($this->_contactID, $contact3)), |
| 196 | ); |
| 197 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 198 | $this->assertEquals(2, $membership['count']); |
| 199 | $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values'])); |
| 200 | $params = array( |
| 201 | 'contact_id' => array('NOT IN' => array($this->_contactID, $contact3)), |
| 202 | ); |
| 203 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 204 | $this->assertEquals(1, $membership['count']); |
| 205 | $this->assertEquals(array($this->_membershipID2), array_keys($membership['values'])); |
| 206 | } |
| 207 | /** |
| 208 | * Test civicrm_membership_get with params not array. |
| 209 | * Gets treated as contact_id, memberships expected. |
| 210 | */ |
| 211 | |
| 212 | function testGetWithParamsMemberShipTypeId() { |
| 213 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 214 | $params = array( |
| 215 | 'membership_type_id' => $this->_membershipTypeID, |
| 216 | ); |
| 217 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 218 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 219 | 'id' => $membership['id'], |
| 220 | )); |
| 221 | $result = $membership['values'][$membership['id']]; |
| 222 | $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__); |
| 223 | $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__); |
| 224 | $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__); |
| 225 | $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__); |
| 226 | $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__); |
| 227 | $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__); |
| 228 | $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__); |
| 229 | $this->assertEquals($result['is_override'], 1, "In line " . __LINE__); |
| 230 | $this->assertEquals($result['id'], $membership['id']); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * check with complete array + custom field |
| 235 | * Note that the test is written on purpose without any |
| 236 | * variables specific to participant so it can be replicated into other entities |
| 237 | * and / or moved to the automated test suite |
| 238 | */ |
| 239 | function testGetWithParamsMemberShipIdAndCustom() { |
| 240 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 241 | |
| 242 | $params = $this->_params; |
| 243 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 244 | |
| 245 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
| 246 | |
| 247 | $getParams = array('membership_type_id' => $params['membership_type_id']); |
| 248 | $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__); |
| 249 | $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 250 | |
| 251 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 252 | 'id' => $result['id'], |
| 253 | )); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Test civicrm_membership_get with proper params. |
| 258 | * Memberships expected. |
| 259 | */ |
| 260 | function testGet() { |
| 261 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 262 | $params = array( |
| 263 | 'contact_id' => $this->_contactID, |
| 264 | ); |
| 265 | |
| 266 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 267 | $result = $membership['values'][$membershipID]; |
| 268 | $this->callAPISuccess('Membership', 'Delete', array( |
| 269 | 'id' => $membership['id'], |
| 270 | )); |
| 271 | $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__); |
| 272 | $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__); |
| 273 | $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__); |
| 274 | $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__); |
| 275 | |
| 276 | $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__); |
| 277 | $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__); |
| 278 | $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__); |
| 279 | $this->assertEquals($result['is_override'], 1, "In line " . __LINE__); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | * Test civicrm_membership_get with proper params. |
| 285 | * Memberships expected. |
| 286 | */ |
| 287 | function testGetWithId() { |
| 288 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 289 | $params = array( |
| 290 | 'contact_id' => $this->_contactID, |
| 291 | 'id' => $this->__membershipID, |
| 292 | 'return' => 'id', |
| 293 | ); |
| 294 | $result = $this->callAPISuccess('membership', 'get', $params); |
| 295 | $this->assertEquals($membershipID, $result['id']); |
| 296 | $params = array( |
| 297 | 'contact_id' => $this->_contactID, |
| 298 | 'membership_id' => $this->__membershipID, |
| 299 | 'return' => 'membership_id', |
| 300 | ); |
| 301 | $result = $this->callAPISuccess('membership', 'get', $params); |
| 302 | $this->assertEquals($membershipID, $result['id']); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Test civicrm_membership_get for only active. |
| 307 | * Memberships expected. |
| 308 | */ |
| 309 | function testGetOnlyActive() { |
| 310 | $description = "Demonstrates use of 'filter' active_only' param"; |
| 311 | $this->_membershipID = $this->contactMembershipCreate($this->_params); |
| 312 | $subfile = 'filterIsCurrent'; |
| 313 | $params = array( |
| 314 | 'contact_id' => $this->_contactID, |
| 315 | 'active_only' => 1, |
| 316 | ); |
| 317 | |
| 318 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 319 | $result = $membership['values'][$this->_membershipID]; |
| 320 | $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__); |
| 321 | $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__); |
| 322 | $params = array( |
| 323 | 'contact_id' => $this->_contactID, |
| 324 | 'filters' => array( |
| 325 | 'is_current' => 1, |
| 326 | ), |
| 327 | ); |
| 328 | |
| 329 | $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 330 | $result = $membership['values'][$this->_membershipID]; |
| 331 | $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__); |
| 332 | $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__); |
| 333 | |
| 334 | |
| 335 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 336 | 'id' => $this->_membershipID, |
| 337 | )); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Test civicrm_membership_get for non exist contact. |
| 342 | * empty Memberships. |
| 343 | */ |
| 344 | function testGetNoContactExists() { |
| 345 | $params = array( |
| 346 | 'contact_id' => 55555, |
| 347 | ); |
| 348 | |
| 349 | $membership = $this->callAPISuccess('membership', 'get', $params); |
| 350 | $this->assertEquals($membership['count'], 0, "In line " . __LINE__); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Test civicrm_membership_get with relationship. |
| 355 | * get Memberships. |
| 356 | */ |
| 357 | function testGetWithRelationship() { |
| 358 | $membershipOrgId = $this->organizationCreate(NULL); |
| 359 | $memberContactId = $this->individualCreate(); |
| 360 | |
| 361 | $relTypeParams = array( |
| 362 | 'name_a_b' => 'Relation 1', |
| 363 | 'name_b_a' => 'Relation 2', |
| 364 | 'description' => 'Testing relationship type', |
| 365 | 'contact_type_a' => 'Organization', |
| 366 | 'contact_type_b' => 'Individual', |
| 367 | 'is_reserved' => 1, |
| 368 | 'is_active' => 1, |
| 369 | ); |
| 370 | $relTypeID = $this->relationshipTypeCreate($relTypeParams); |
| 371 | |
| 372 | $params = array( |
| 373 | 'name' => 'test General', |
| 374 | 'duration_unit' => 'year', |
| 375 | 'duration_interval' => 1, |
| 376 | 'period_type' => 'rolling', |
| 377 | 'member_of_contact_id' => $membershipOrgId, |
| 378 | 'domain_id' => 1, |
| 379 | 'financial_type_id' => 1, |
| 380 | 'relationship_type_id' => $relTypeID, |
| 381 | 'relationship_direction' => 'b_a', |
| 382 | 'is_active' => 1, |
| 383 | ); |
| 384 | $memType = $this->callAPISuccess('membership_type', 'create', $params); |
| 385 | |
| 386 | $params = array( |
| 387 | 'contact_id' => $memberContactId, |
| 388 | 'membership_type_id' => $memType['id'], |
| 389 | 'join_date' => '2009-01-21', |
| 390 | 'start_date' => '2009-01-21', |
| 391 | 'end_date' => '2009-12-21', |
| 392 | 'source' => 'Payment', |
| 393 | 'is_override' => 1, |
| 394 | 'status_id' => $this->_membershipStatusID, |
| 395 | ); |
| 396 | $membershipID = $this->contactMembershipCreate($params); |
| 397 | |
| 398 | $params = array( |
| 399 | 'contact_id' => $memberContactId, |
| 400 | 'membership_type_id' => $memType['id'], |
| 401 | ); |
| 402 | |
| 403 | $result = $this->callAPISuccess('membership', 'get', $params); |
| 404 | |
| 405 | $membership = $result['values'][$membershipID]; |
| 406 | $this->assertEquals($this->_membershipStatusID, $membership['status_id']); |
| 407 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 408 | 'id' => $membership['id'], |
| 409 | )); |
| 410 | $this->membershipTypeDelete(array('id' => $memType['id'])); |
| 411 | $this->relationshipTypeDelete($relTypeID); |
| 412 | $this->contactDelete($membershipOrgId); |
| 413 | $this->contactDelete($memberContactId); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * We are checking for no enotices + only id & end_date returned |
| 418 | */ |
| 419 | function testMembershipGetWithReturn() { |
| 420 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 421 | $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date')); |
| 422 | foreach ($result['values'] as $membership) { |
| 423 | $this->assertEquals(array('id', 'end_date'), array_keys($membership)); |
| 424 | } |
| 425 | } |
| 426 | ///////////////// civicrm_membership_create methods |
| 427 | |
| 428 | /** |
| 429 | * Test civicrm_contact_memberships_create with empty params. |
| 430 | * Error expected. |
| 431 | */ |
| 432 | function testCreateWithEmptyParams() { |
| 433 | $params = array(); |
| 434 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * If is_overide is passed in status must also be passed in |
| 439 | */ |
| 440 | function testCreateOverrideNoStatus() { |
| 441 | $params = $this->_params; |
| 442 | unset($params['status_id']); |
| 443 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 444 | } |
| 445 | |
| 446 | function testMembershipCreateMissingRequired() { |
| 447 | $params = array( |
| 448 | 'membership_type_id' => '1', |
| 449 | 'join_date' => '2006-01-21', |
| 450 | 'start_date' => '2006-01-21', |
| 451 | 'end_date' => '2006-12-21', |
| 452 | 'source' => 'Payment', |
| 453 | 'status_id' => '2', |
| 454 | ); |
| 455 | |
| 456 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 457 | } |
| 458 | |
| 459 | function testMembershipCreate() { |
| 460 | $params = array( |
| 461 | 'contact_id' => $this->_contactID, |
| 462 | 'membership_type_id' => $this->_membershipTypeID, |
| 463 | 'join_date' => '2006-01-21', |
| 464 | 'start_date' => '2006-01-21', |
| 465 | 'end_date' => '2006-12-21', |
| 466 | 'source' => 'Payment', |
| 467 | 'is_override' => 1, |
| 468 | 'status_id' => $this->_membershipStatusID, |
| 469 | ); |
| 470 | |
| 471 | $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__); |
| 472 | $this->getAndCheck($params, $result['id'], $this->_entity); |
| 473 | $this->assertNotNull($result['id']); |
| 474 | $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__); |
| 475 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__); |
| 476 | } |
| 477 | /* |
| 478 | * Check for useful message if contact doesn't exist |
| 479 | */ |
| 480 | function testMembershipCreateWithInvalidContact() { |
| 481 | $params = array( |
| 482 | 'contact_id' => 999, |
| 483 | 'membership_type_id' => $this->_membershipTypeID, |
| 484 | 'join_date' => '2006-01-21', |
| 485 | 'start_date' => '2006-01-21', |
| 486 | 'end_date' => '2006-12-21', |
| 487 | 'source' => 'Payment', |
| 488 | 'is_override' => 1, |
| 489 | 'status_id' => $this->_membershipStatusID, |
| 490 | ); |
| 491 | |
| 492 | $result = $this->callAPIFailure('membership', 'create', $params, |
| 493 | 'contact_id is not valid : 999' |
| 494 | ); |
| 495 | } |
| 496 | function testMembershipCreateWithInvalidStatus() { |
| 497 | $params = $this->_params; |
| 498 | $params['status_id'] = 999; |
| 499 | $result = $this->callAPIFailure('membership', 'create', $params, |
| 500 | "'999' is not a valid option for field status_id" |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | function testMembershipCreateWithInvalidType() { |
| 505 | $params = $this->_params; |
| 506 | $params['membership_type_id'] = 999; |
| 507 | |
| 508 | $result = $this->callAPIFailure('membership', 'create', $params, |
| 509 | "'999' is not a valid option for field membership_type_id" |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * check with complete array + custom field |
| 515 | * Note that the test is written on purpose without any |
| 516 | * variables specific to participant so it can be replicated into other entities |
| 517 | * and / or moved to the automated test suite |
| 518 | */ |
| 519 | function testCreateWithCustom() { |
| 520 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 521 | |
| 522 | $params = $this->_params; |
| 523 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 524 | |
| 525 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
| 526 | $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID)); |
| 527 | $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Test civicrm_contact_memberships_create with membership id (edit |
| 532 | * membership). |
| 533 | * success expected. |
| 534 | */ |
| 535 | function testMembershipCreateWithId() { |
| 536 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 537 | $params = array( |
| 538 | 'id' => $membershipID, |
| 539 | 'contact_id' => $this->_contactID, |
| 540 | 'membership_type_id' => $this->_membershipTypeID, |
| 541 | 'join_date' => '2006-01-21', |
| 542 | 'start_date' => '2006-01-21', |
| 543 | 'end_date' => '2006-12-21', |
| 544 | 'source' => 'Payment', |
| 545 | 'is_override' => 1, |
| 546 | 'status_id' => $this->_membershipStatusID, |
| 547 | ); |
| 548 | |
| 549 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 550 | $this->callAPISuccess('Membership', 'Delete', array( |
| 551 | 'id' => $result['id'], |
| 552 | )); |
| 553 | $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Test civicrm_contact_memberships_create with membership id (edit |
| 558 | * membership). |
| 559 | * success expected. |
| 560 | */ |
| 561 | function testMembershipCreateUpdateWithIdNoContact() { |
| 562 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 563 | $params = array( |
| 564 | 'id' => $membershipID, |
| 565 | 'membership_type_id' => $this->_membershipTypeID, |
| 566 | 'contact_id' => $this->_contactID, |
| 567 | 'join_date' => '2006-01-21', |
| 568 | 'start_date' => '2006-01-21', |
| 569 | 'end_date' => '2006-12-21', |
| 570 | 'source' => 'Payment', |
| 571 | 'is_override' => 1, |
| 572 | 'status_id' => $this->_membershipStatusID, |
| 573 | ); |
| 574 | |
| 575 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 576 | $this->callAPISuccess('Membership', 'Delete', array( |
| 577 | 'id' => $result['id'], |
| 578 | )); |
| 579 | |
| 580 | $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Test civicrm_contact_memberships_create with membership id (edit |
| 585 | * membership). |
| 586 | * success expected. |
| 587 | */ |
| 588 | function testMembershipCreateUpdateWithIdNoDates() { |
| 589 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 590 | $params = array( |
| 591 | 'id' => $membershipID, |
| 592 | 'contact_id' => $this->_contactID, |
| 593 | 'membership_type_id' => $this->_membershipTypeID, |
| 594 | 'source' => 'Payment', |
| 595 | 'is_override' => 1, |
| 596 | 'status_id' => $this->_membershipStatusID, |
| 597 | ); |
| 598 | |
| 599 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 600 | $this->callAPISuccess('Membership', 'Delete', array( |
| 601 | 'id' => $result['id'], |
| 602 | )); |
| 603 | $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Test civicrm_contact_memberships_create with membership id (edit |
| 608 | * membership). |
| 609 | * success expected. |
| 610 | */ |
| 611 | function testMembershipCreateUpdateWithIdNoDatesNoType() { |
| 612 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 613 | $params = array( |
| 614 | 'id' => $membershipID, |
| 615 | 'source' => 'not much here', |
| 616 | 'contact_id' => $this->_contactID, |
| 617 | 'is_override' => 1, |
| 618 | 'status_id' => $this->_membershipStatusID, |
| 619 | ); |
| 620 | |
| 621 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 622 | $this->callAPISuccess('Membership', 'Delete', array( |
| 623 | 'id' => $result['id'], |
| 624 | )); |
| 625 | $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Test civicrm_contact_memberships_create with membership id (edit |
| 630 | * membership). |
| 631 | * success expected. |
| 632 | */ |
| 633 | function testMembershipCreateUpdateWithIDAndSource() { |
| 634 | $membershipID = $this->contactMembershipCreate($this->_params); |
| 635 | $params = array( |
| 636 | 'id' => $membershipID, |
| 637 | 'source' => 'changed', |
| 638 | 'contact_id' => $this->_contactID, |
| 639 | 'status_id' => $this->_membershipStatusID, 'membership_type_id' => $this->_membershipTypeID, |
| 640 | 'skipStatusCal' => 1, |
| 641 | ); |
| 642 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 643 | $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__); |
| 644 | $this->callAPISuccess('Membership', 'Delete', array( |
| 645 | 'id' => $result['id'], |
| 646 | )); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * change custom field using update |
| 651 | */ |
| 652 | function testUpdateWithCustom() { |
| 653 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 654 | |
| 655 | $params = $this->_params; |
| 656 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 657 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
| 658 | $result = $this->callAPISuccess($this->_entity, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom")); |
| 659 | $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID)); |
| 660 | |
| 661 | $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 662 | $delete = $this->callAPISuccess('Membership', 'Delete', array( |
| 663 | 'id' => $check['id'], |
| 664 | )); |
| 665 | |
| 666 | $this->customFieldDelete($ids['custom_field_id']); |
| 667 | $this->customGroupDelete($ids['custom_group_id']); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Test civicrm_contact_memberships_create Invalid membership data |
| 672 | * Error expected. |
| 673 | */ |
| 674 | function testMembershipCreateInvalidMemData() { |
| 675 | //membership_contact_id as string |
| 676 | $params = array( |
| 677 | 'membership_contact_id' => 'Invalid', |
| 678 | 'membership_type_id' => $this->_membershipTypeID, |
| 679 | 'join_date' => '2011-01-21', |
| 680 | 'start_date' => '2010-01-21', |
| 681 | 'end_date' => '2008-12-21', |
| 682 | 'source' => 'Payment', |
| 683 | 'is_override' => 1, |
| 684 | 'status_id' => $this->_membershipStatusID, ); |
| 685 | |
| 686 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 687 | |
| 688 | //membership_contact_id which is no in contact table |
| 689 | $params['membership_contact_id'] = 999; |
| 690 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 691 | |
| 692 | //invalid join date |
| 693 | unset($params['membership_contact_id']); |
| 694 | $params['join_date'] = "invalid"; |
| 695 | $result = $this->callAPIFailure('Membership', 'Create', $params); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Test civicrm_contact_memberships_create with membership_contact_id |
| 700 | * membership). |
| 701 | * Success expected. |
| 702 | */ |
| 703 | function testMembershipCreateWithMemContact() { |
| 704 | $params = array( |
| 705 | 'membership_contact_id' => $this->_contactID, |
| 706 | 'membership_type_id' => $this->_membershipTypeID, |
| 707 | 'join_date' => '2011-01-21', |
| 708 | 'start_date' => '2010-01-21', |
| 709 | 'end_date' => '2008-12-21', |
| 710 | 'source' => 'Payment', |
| 711 | 'is_override' => 1, |
| 712 | 'status_id' => $this->_membershipStatusID, |
| 713 | ); |
| 714 | |
| 715 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 716 | |
| 717 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 718 | 'id' => $result['id'], |
| 719 | )); |
| 720 | } |
| 721 | /** |
| 722 | * Test civicrm_contact_memberships_create with membership_contact_id |
| 723 | * membership). |
| 724 | * Success expected. |
| 725 | */ |
| 726 | function testMembershipCreateValidMembershipTypeString() { |
| 727 | $params = array( |
| 728 | 'membership_contact_id' => $this->_contactID, |
| 729 | 'membership_type_id' => 'General', |
| 730 | 'join_date' => '2011-01-21', |
| 731 | 'start_date' => '2010-01-21', |
| 732 | 'end_date' => '2008-12-21', |
| 733 | 'source' => 'Payment', |
| 734 | 'is_override' => 1, |
| 735 | 'status_id' => $this->_membershipStatusID, |
| 736 | ); |
| 737 | |
| 738 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 739 | $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']); |
| 740 | $result = $this->callAPISuccess('Membership', 'Delete', array( |
| 741 | 'id' => $result['id'], |
| 742 | )); |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Test civicrm_contact_memberships_create with membership_contact_id |
| 747 | * membership). |
| 748 | * Success expected. |
| 749 | */ |
| 750 | function testMembershipCreateInValidMembershipTypeString() { |
| 751 | $params = array( |
| 752 | 'membership_contact_id' => $this->_contactID, |
| 753 | 'membership_type_id' => 'invalid', |
| 754 | 'join_date' => '2011-01-21', |
| 755 | 'start_date' => '2010-01-21', |
| 756 | 'end_date' => '2008-12-21', |
| 757 | 'source' => 'Payment', |
| 758 | 'is_override' => 1, |
| 759 | 'status_id' => $this->_membershipStatusID, |
| 760 | ); |
| 761 | |
| 762 | $result = $this->callAPIFailure('membership', 'create', $params); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Test that if membership join date is not set it defaults to today |
| 767 | */ |
| 768 | function testEmptyJoinDate() { |
| 769 | unset($this->_params['join_date'], $this->_params['is_override']); |
| 770 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 771 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 772 | $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']); |
| 773 | $this->assertEquals('2009-01-21', $result['start_date']); |
| 774 | $this->assertEquals('2009-12-21', $result['end_date']); |
| 775 | } |
| 776 | /** |
| 777 | * Test that if membership start date is not set it defaults to correct end date |
| 778 | * - fixed |
| 779 | */ |
| 780 | function testEmptyStartDateFixed() { |
| 781 | unset($this->_params['start_date'], $this->_params['is_override']); |
| 782 | $this->_params['membership_type_id'] = $this->_membershipTypeID2; |
| 783 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 784 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 785 | $this->assertEquals('2009-01-21', $result['join_date']); |
| 786 | $this->assertEquals('2008-03-01', $result['start_date']); |
| 787 | $this->assertEquals('2009-12-21', $result['end_date']); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Test that if membership start date is not set it defaults to correct end date |
| 792 | * - fixed |
| 793 | */ |
| 794 | function testEmptyStartDateRolling() { |
| 795 | unset($this->_params['start_date'], $this->_params['is_override']); |
| 796 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 797 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 798 | $this->assertEquals('2009-01-21', $result['join_date']); |
| 799 | $this->assertEquals('2009-01-21', $result['start_date']); |
| 800 | $this->assertEquals('2009-12-21', $result['end_date']); |
| 801 | } |
| 802 | /** |
| 803 | * Test that if membership end date is not set it defaults to correct end date |
| 804 | * - rolling |
| 805 | */ |
| 806 | function testEmptyEndDateFixed() { |
| 807 | unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); |
| 808 | $this->_params['membership_type_id'] = $this->_membershipTypeID2; |
| 809 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 810 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 811 | $this->assertEquals('2009-01-21', $result['join_date']); |
| 812 | $this->assertEquals('2008-03-01', $result['start_date']); |
| 813 | $this->assertEquals('2010-02-28', $result['end_date']); |
| 814 | } |
| 815 | /** |
| 816 | * Test that if membership end date is not set it defaults to correct end date |
| 817 | * - rolling |
| 818 | */ |
| 819 | function testEmptyEndDateRolling() { |
| 820 | unset($this->_params['is_override'], $this->_params['end_date']); |
| 821 | $this->_params['membership_type_id'] = $this->_membershipTypeID; |
| 822 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 823 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 824 | $this->assertEquals('2009-01-21', $result['join_date']); |
| 825 | $this->assertEquals('2009-01-21', $result['start_date']); |
| 826 | $this->assertEquals('2010-01-20', $result['end_date']); |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /** |
| 831 | * Test that if datesdate are not set they not over-ridden if id is passed in |
| 832 | */ |
| 833 | function testMembershipDatesNotOverridden() { |
| 834 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 835 | unset($this->_params['end_date'], $this->_params['start_date']); |
| 836 | $this->_params['id'] = $result['id']; |
| 837 | $this->callAPISuccess($this->_entity, 'create', $this->_params); |
| 838 | $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); |
| 839 | $this->assertEquals('2009-01-21', $result['join_date']); |
| 840 | $this->assertEquals('2009-01-21', $result['start_date']); |
| 841 | $this->assertEquals('2009-12-21', $result['end_date']); |
| 842 | |
| 843 | } |
| 844 | } |
| 845 | |