X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv3%2FMembershipTest.php;h=f4023963e1d58ba5ca8489a1d04f82d946fbbae7;hb=c9ec4c2ab72776ea126ecc1a73117f1b53f9cfba;hp=36e81afe3000e7c10c66ff1f40d73ca34bbb9932;hpb=98febc67fca1349bf4b086c8fa5d3152c4d98777;p=civicrm-core.git diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 36e81afe30..f4023963e1 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -1,7 +1,7 @@ _apiversion = 3; $this->_contactID = $this->individualCreate(); - $this->_membershipTypeID = $this->membershipTypeCreate($this->_contactID); + $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID)); + $this->_membershipTypeID2 = $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111')); $this->_membershipStatusID = $this->membershipStatusCreate('test status'); - require_once 'CRM/Member/PseudoConstant.php'; CRM_Member_PseudoConstant::membershipType(NULL, TRUE); CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name'); @@ -80,6 +81,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { TRUE ); $this->membershipStatusDelete($this->_membershipStatusID); + $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2)); $this->membershipTypeDelete(array('id' => $this->_membershipTypeID)); $this->contactDelete($this->_contactID); @@ -160,6 +162,53 @@ class api_v3_MembershipTest extends CiviUnitTestCase { * Test civicrm_membership_get with params not array. * Gets treated as contact_id, memberships expected. */ + function testGetInSyntax() { + $this->_membershipID = $this->contactMembershipCreate($this->_params); + $this->_membershipID2 = $this->contactMembershipCreate($this->_params); + $this->_membershipID3 = $this->contactMembershipCreate($this->_params); + $params = array( + 'id' => array('IN' => array($this->_membershipID, $this->_membershipID3)), + ); + $membership = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(2, $membership['count']); + $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values'])); + $params = array( + 'id' => array('NOT IN' => array($this->_membershipID, $this->_membershipID3)), + ); + $membership = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(1, $membership['count']); + $this->assertEquals(array($this->_membershipID2), array_keys($membership['values'])); + + } + + /** + * Test civicrm_membership_get with params not array. + * Gets treated as contact_id, memberships expected. + */ + function testGetInSyntaxOnContactID() { + $this->_membershipID = $this->contactMembershipCreate($this->_params); + $contact2 = $this->individualCreate(); + $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine')); + $this->_membershipID2 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact2))); + $this->_membershipID3 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact3))); + $params = array( + 'contact_id' => array('IN' => array($this->_contactID, $contact3)), + ); + $membership = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(2, $membership['count']); + $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values'])); + $params = array( + 'contact_id' => array('NOT IN' => array($this->_contactID, $contact3)), + ); + $membership = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(1, $membership['count']); + $this->assertEquals(array($this->_membershipID2), array_keys($membership['values'])); + } + /** + * Test civicrm_membership_get with params not array. + * Gets treated as contact_id, memberships expected. + */ + function testGetWithParamsMemberShipTypeId() { $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); $params = array( @@ -307,7 +356,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { */ function testGetWithRelationship() { $membershipOrgId = $this->organizationCreate(NULL); - $memberContactId = $this->individualCreate(NULL); + $memberContactId = $this->individualCreate(); $relTypeParams = array( 'name_a_b' => 'Relation 1', @@ -364,6 +413,16 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $this->contactDelete($memberContactId); } + /** + * We are checking for no enotices + only id & end_date returned + */ + function testMembershipGetWithReturn() { + $membershipID = $this->contactMembershipCreate($this->_params); + $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date')); + foreach ($result['values'] as $membership) { + $this->assertEquals(array('id', 'end_date'), array_keys($membership)); + } + } ///////////////// civicrm_membership_create methods /** @@ -659,7 +718,128 @@ class api_v3_MembershipTest extends CiviUnitTestCase { 'id' => $result['id'], )); } + /** + * Test civicrm_contact_memberships_create with membership_contact_id + * membership). + * Success expected. + */ + function testMembershipCreateValidMembershipTypeString() { + $params = array( + 'membership_contact_id' => $this->_contactID, + 'membership_type_id' => 'General', + 'join_date' => '2011-01-21', + 'start_date' => '2010-01-21', + 'end_date' => '2008-12-21', + 'source' => 'Payment', + 'is_override' => 1, + 'status_id' => $this->_membershipStatusID, + ); + + $result = $this->callAPISuccess('membership', 'create', $params); + $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']); + $result = $this->callAPISuccess('Membership', 'Delete', array( + 'id' => $result['id'], + )); + } + + /** + * Test civicrm_contact_memberships_create with membership_contact_id + * membership). + * Success expected. + */ + function testMembershipCreateInValidMembershipTypeString() { + $params = array( + 'membership_contact_id' => $this->_contactID, + 'membership_type_id' => 'invalid', + 'join_date' => '2011-01-21', + 'start_date' => '2010-01-21', + 'end_date' => '2008-12-21', + 'source' => 'Payment', + 'is_override' => 1, + 'status_id' => $this->_membershipStatusID, + ); + + $result = $this->callAPIFailure('membership', 'create', $params); + } + + /** + * Test that if membership join date is not set it defaults to today + */ + function testEmptyJoinDate() { + unset($this->_params['join_date'], $this->_params['is_override']); + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + /** + * Test that if membership start date is not set it defaults to correct end date + * - fixed + */ + function testEmptyStartDateFixed() { + unset($this->_params['start_date'], $this->_params['is_override']); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2008-03-01', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + + /** + * Test that if membership start date is not set it defaults to correct end date + * - fixed + */ + function testEmptyStartDateRolling() { + unset($this->_params['start_date'], $this->_params['is_override']); + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + /** + * Test that if membership end date is not set it defaults to correct end date + * - rolling + */ + function testEmptyEndDateFixed() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2008-03-01', $result['start_date']); + $this->assertEquals('2010-02-28', $result['end_date']); + } + /** + * Test that if membership end date is not set it defaults to correct end date + * - rolling + */ + function testEmptyEndDateRolling() { + unset($this->_params['is_override'], $this->_params['end_date']); + $this->_params['membership_type_id'] = $this->_membershipTypeID; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2010-01-20', $result['end_date']); + } + - ///////////////// civicrm_membership_delete methods + /** + * Test that if datesdate are not set they not over-ridden if id is passed in + */ + function testMembershipDatesNotOverridden() { + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + unset($this->_params['end_date'], $this->_params['start_date']); + $this->_params['id'] = $result['id']; + $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + + } }