Build query using CRM_Utils_SQL_Select.
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
index c3402c8e389b8c59ee1742b27d04acbf7389924c..aa5d0937a20e895b0f81722d0c9924c1fa245864 100644 (file)
@@ -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.                                    |
  |                                                                    |
@@ -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",
@@ -864,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() {
@@ -961,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']);
@@ -973,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() {
@@ -1002,7 +1033,266 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test that if membership start date is not set it defaults to correct end date for rolling memberships.
+   * 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']);
@@ -1014,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() {
@@ -1028,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() {