X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv3%2FMembershipTest.php;h=aa5d0937a20e895b0f81722d0c9924c1fa245864;hb=7cbc050355fcbc0f6f279534ab34381745ca5483;hp=8ed9bc10c4163e99093afb30b35f5642fb1cc056;hpb=28c8af2418511a5dbc537c0b9e9b922196bec519;p=civicrm-core.git diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 8ed9bc10c4..aa5d0937a2 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -3,7 +3,7 @@ +--------------------------------------------------------------------+ | CiviCRM version 4.6 | +--------------------------------------------------------------------+ - | Copyright CiviCRM LLC (c) 2004-2014 | + | Copyright CiviCRM LLC (c) 2004-2015 | +--------------------------------------------------------------------+ | This file is a part of CiviCRM. | | | @@ -52,14 +52,16 @@ class api_v3_MembershipTest extends CiviUnitTestCase { public function setUp() { - // Connect to the database + // Connect to the database. parent::setUp(); $this->_apiversion = 3; $this->_contactID = $this->individualCreate(); $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID)); $this->_membershipTypeID2 = $this->membershipTypeCreate(array( 'period_type' => 'fixed', + // Ie. 1 March. 'fixed_period_start_day' => '301', + // Ie. 11 Nov. 'fixed_period_rollover_day' => '1111', )); $this->_membershipStatusID = $this->membershipStatusCreate('test status'); @@ -97,7 +99,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test civicrm_membership_delete() + * Test membership deletion. */ public function testMembershipDelete() { $membershipID = $this->contactMembershipCreate($this->_params); @@ -118,7 +120,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test civicrm_membership_delete() with invalid Membership Id + * Test civicrm_membership_delete() with invalid Membership Id. */ public function testMembershipDeleteWithInvalidMembershipId() { $membershipId = 'membership'; @@ -126,20 +128,17 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * All other methods calls MembershipType and MembershipContact - * api, but putting simple test methods to control existence of - * these methods for backwards compatibility, also verifying basic - * behaviour is the same as new methods. + * Test membership get. */ public function testContactMembershipsGet() { $this->_membershipID = $this->contactMembershipCreate($this->_params); - $params = array(); - $this->callAPISuccess('membership', 'get', $params); + $this->callAPISuccess('membership', 'get', array()); $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID)); } /** * Test civicrm_membership_get with params not array. + * * Gets treated as contact_id, memberships expected. */ public function testGetWithParamsContactId() { @@ -165,6 +164,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { /** * Test civicrm_membership_get with params not array. + * * Gets treated as contact_id, memberships expected. */ public function testGetInSyntax() { @@ -338,9 +338,8 @@ class api_v3_MembershipTest extends CiviUnitTestCase { * Memberships expected. */ public function testGetOnlyActive() { - $description = "Demonstrates use of 'filter' active_only' param"; + $description = "Demonstrates use of 'filter' active_only' param."; $this->_membershipID = $this->contactMembershipCreate($this->_params); - $subfile = 'filterIsCurrent'; $params = array( 'contact_id' => $this->_contactID, 'active_only' => 1, @@ -356,7 +355,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { ), ); - $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); + $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent'); $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID); $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID); @@ -554,6 +553,38 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $result = $this->callAPISuccess('membership', 'get', $params); $this->assertEquals(0, $result['count']); + // Set up params for enable/disable checks + $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1])); + $params = array( + 'contact_id' => $memberContactId[1], + 'membership_type_id' => $membershipTypeId, + ); + + // Deactivate relationship using create and assert membership is not inherited + $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0)); + $result = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(0, $result['count']); + + // Re-enable relationship using create and assert membership is inherited + $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1)); + $result = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(1, $result['count']); + + // Deactivate relationship using setvalue and assert membership is not inherited + $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0)); + $result = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(0, $result['count']); + + // Re-enable relationship using setvalue and assert membership is inherited + $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1)); + $result = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(1, $result['count']); + + // Delete relationship and assert membership is not inherited + $this->callAPISuccess('relationship', 'delete', array('id' => $relationship1['id'])); + $result = $this->callAPISuccess('membership', 'get', $params); + $this->assertEquals(0, $result['count']); + // Tear down - reverse of creation to be safe $this->contactDelete($memberContactId[2]); $this->contactDelete($memberContactId[1]); @@ -586,7 +617,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * If is_overide is passed in status must also be passed in + * If is_overide is passed in status must also be passed in. */ public function testCreateOverrideNoStatus() { $params = $this->_params; @@ -675,7 +706,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $params = $this->_params; $params['custom_' . $ids['custom_field_id']] = "custom string"; - $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData'); $check = $this->callAPISuccess($this->_entity, 'get', array( 'id' => $result['id'], 'contact_id' => $this->_contactID, @@ -804,14 +835,14 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Change custom field using update + * Change custom field using update. */ public function testUpdateWithCustom() { $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); $params = $this->_params; $params['custom_' . $ids['custom_field_id']] = "custom string"; - $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); + $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData'); $result = $this->callAPISuccess($this->_entity, 'create', array( 'id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom", @@ -843,6 +874,16 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $this->callAPISuccessGetCount('membership', array(), 2); } + /** + * Custom hook for update membership. + * + * @param string $op + * @param object $objectName + * @param int $id + * @param array $params + * + * @throws \Exception + */ public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) { if ($objectName == 'Membership' && $op == 'edit') { $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id'])); @@ -854,7 +895,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test civicrm_contact_memberships_create Invalid membership data + * Test civicrm_contact_memberships_create Invalid membership data. * Error expected. */ public function testMembershipCreateInvalidMemData() { @@ -951,7 +992,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test that if membership join date is not set it defaults to today + * Test that if membership join date is not set it defaults to today. */ public function testEmptyJoinDate() { unset($this->_params['join_date'], $this->_params['is_override']); @@ -963,7 +1004,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test that if membership start date is not set it defaults to correct end date + * Test that if membership start date is not set it defaults to correct end date. * - fixed */ public function testEmptyStartDateFixed() { @@ -980,6 +1021,279 @@ class api_v3_MembershipTest extends CiviUnitTestCase { * Test that if membership start date is not set it defaults to correct end date * - fixed */ + public function testEmptyStartEndDateFixedOneYear() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1)); + $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 start date is not set it defaults to correct end date for fixed multi year memberships. + */ + public function testEmptyStartEndDateFixedMultiYear() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 5)); + $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('2014-02-28', $result['end_date']); + } + + /** + * Test correct end and start dates are calculated for fixed multi year memberships. + * + * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15) + * + * In this set our start date is after the start day and before the rollover day so we don't get an extra year + * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec + * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019 + */ + public function testFixedMultiYearDateSetTwoEmptyStartEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 5, + // Ie 1 Jan. + 'fixed_period_start_day' => '101', + // Ie. 1 Nov. + 'fixed_period_rollover_day' => '1101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-01', $result['start_date']); + $this->assertEquals('2019-12-31', $result['end_date']); + } + + /** + * Test that correct end date is calculated for fixed multi year memberships and start date is not changed. + * + * In this set our start date is after the start day and before the rollover day so we don't get an extra year + * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec + * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019 + */ + public function testFixedMultiYearDateSetTwoEmptyEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 5, + // Ie 1 Jan. + 'fixed_period_start_day' => '101', + // Ie. 1 Nov. + 'fixed_period_rollover_day' => '1101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'start_date' => '28-Jan 2015', + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-28', $result['start_date']); + $this->assertEquals('2019-12-31', $result['end_date']); + } + + /** + * Test correct end and start dates are calculated for fixed multi year memberships. + * + * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15) + * + * In this set our start date is after the start day and before the rollover day so we don't get an extra year + * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec + * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015 + */ + public function testFixedSingleYearDateSetTwoEmptyStartEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 1, + // Ie 1 Jan. + 'fixed_period_start_day' => '101', + // Ie. 1 Nov. + 'fixed_period_rollover_day' => '1101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-01', $result['start_date']); + $this->assertEquals('2015-12-31', $result['end_date']); + } + + /** + * Test correct end date for fixed single year memberships is calculated and start_date is not changed. + * + * In this set our start date is after the start day and before the rollover day so we don't get an extra year + * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec + * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015 + */ + public function testFixedSingleYearDateSetTwoEmptyEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 1, + // Ie 1 Jan. + 'fixed_period_start_day' => '101', + // Ie. 1 Nov. + 'fixed_period_rollover_day' => '1101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'start_date' => '28-Jan 2015', + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-28', $result['start_date']); + $this->assertEquals('2015-12-31', $result['end_date']); + } + + /** + * Test that correct end date is calculated for fixed multi year memberships and start date is not changed. + * + * In this set our start date is after the start day and after the rollover day so we do get an extra year + * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct + * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016 + */ + public function testFixedSingleYearDateSetThreeEmptyEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 1, + // Ie. 1 Nov. + 'fixed_period_start_day' => '1101', + // Ie 1 Jan. + 'fixed_period_rollover_day' => '101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'start_date' => '28-Jan 2015', + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-28', $result['start_date']); + $this->assertEquals('2016-10-31', $result['end_date']); + } + + /** + * Test correct end and start dates are calculated for fixed multi year memberships. + * + * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14) + * + * In this set our start date is after the start day and after the rollover day so we do get an extra year + * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct + * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016 + */ + public function testFixedSingleYearDateSetThreeEmptyStartEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 1, + // Ie. 1 Nov. + 'fixed_period_start_day' => '1101', + // Ie 1 Jan. + 'fixed_period_rollover_day' => '101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2014-11-01', $result['start_date']); + $this->assertEquals('2016-10-31', $result['end_date']); + } + + /** + * Test that correct end date is calculated for fixed multi year memberships and start date is not changed. + * + * In this set our start date is after the start day and after the rollover day so we do get an extra year + * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct + * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020 + */ + public function testFixedMultiYearDateSetThreeEmptyEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 5, + // Ie. 1 Nov. + 'fixed_period_start_day' => '1101', + // Ie 1 Jan. + 'fixed_period_rollover_day' => '101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'start_date' => '28-Jan 2015', + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2015-01-28', $result['start_date']); + $this->assertEquals('2020-10-31', $result['end_date']); + } + + /** + * Test correct end and start dates are calculated for fixed multi year memberships. + * + * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14) + * + * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14) + * In this set our join date is after the start day and after the rollover day so we do get an extra year + * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct + * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020 + */ + public function testFixedMultiYearDateSetThreeEmptyStartEndDate() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + + $this->callAPISuccess('membership_type', 'create', array( + 'id' => $this->_membershipTypeID2, + 'duration_interval' => 5, + // Ie. 1 Nov. + 'fixed_period_start_day' => '1101', + // Ie 1 Jan. + 'fixed_period_rollover_day' => '101', + )); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $dates = array( + 'join_date' => '28-Jan 2015', + ); + $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates)); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2015-01-28', $result['join_date']); + $this->assertEquals('2014-11-01', $result['start_date']); + $this->assertEquals('2020-10-31', $result['end_date']); + } + + /** + * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships. + */ public function testEmptyStartDateRolling() { unset($this->_params['start_date'], $this->_params['is_override']); $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); @@ -990,7 +1304,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test that if membership end date is not set it defaults to correct end date + * Test that if membership end date is not set it defaults to correct end date. * - rolling */ public function testEmptyEndDateFixed() { @@ -1004,7 +1318,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { } /** - * Test that if membership end date is not set it defaults to correct end date + * Test that if membership end date is not set it defaults to correct end date. * - rolling */ public function testEmptyEndDateRolling() {