MembershipRenewalTest - Use mocked time. Fix assertions that fail in 2021
authorTim Otten <totten@civicrm.org>
Sat, 2 Jan 2021 04:57:30 +0000 (20:57 -0800)
committerTim Otten <totten@civicrm.org>
Fri, 8 Jan 2021 05:24:46 +0000 (21:24 -0800)
Circa Jan 1, 2021, this tested started failing reliable -- most likely
because the example values only make sense in a certain time-frame (ie after
Apr 2020 but before Jan 20201).

This patch makes the time-frame explicit by mocking the 'current time'.

tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php

index c47906e4ae1455df12dde3da6039755dd13d9336..a7b9c9d5178a7b112345668523cfd263bcac8aff 100644 (file)
@@ -78,7 +78,8 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
   public function setUp() {
     parent::setUp();
 
-    timecop_travel(mktime(1, 0, 0, 8, 1, 2020));
+    // NOTE: This will mock time for PHP. However, some values populated by MySQL ("modified_date") may leak through.
+    CRM_Utils_Time::setTime('2020-08-01 01:00:00');
 
     $this->_individualId = $this->individualCreate();
     $this->_paymentProcessorID = $this->processorCreate();
@@ -128,7 +129,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     foreach ($this->ids['contact'] as $contactID) {
       $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
     }
-    timecop_return();
+    CRM_Utils_Time::resetTime();
   }
 
   /**
@@ -245,7 +246,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
     $this->assertEquals($newMembershipTypeID, $membership['membership_type_id']);
     // The date (31 Dec this year) should be progressed by 2 months to 28 Dec next year.
-    $this->assertEquals(date('Y', strtotime($membershipBefore['end_date'])) + 1 . '-02-28', $membership['end_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y', strtotime($membershipBefore['end_date'])) + 1 . '-02-28', $membership['end_date']);
   }
 
   /**
@@ -268,7 +269,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $params = [
       'cid' => $this->_individualId,
       'price_set_id' => 0,
-      'join_date' => date('m/d/Y'),
+      'join_date' => CRM_Utils_Time::date('m/d/Y'),
       'start_date' => '',
       'end_date' => '',
       'campaign_id' => '',
@@ -288,7 +289,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
       'cvv2' => '123',
       'credit_card_exp_date' => [
         'M' => '9',
-        'Y' => date('Y') + 1,
+        'Y' => CRM_Utils_Time::date('Y') + 1,
       ],
       'credit_card_type' => 'Visa',
       'billing_first_name' => 'Test',
@@ -308,7 +309,6 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
     $this->assertEquals(1, $contributionRecur['is_email_receipt']);
     $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
-    $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
     $this->assertNotEmpty($contributionRecur['invoice_id']);
     $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id',
       'Pending'), $contributionRecur['contribution_status_id']);
@@ -373,23 +373,23 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $form->testSubmit($params);
     $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
     $this->assertEquals('2020-04-13', $membership['join_date']);
-    $this->assertEquals(date('Y-01-01'), $membership['start_date']);
-    $nextYear = date('Y') + 1;
-    $this->assertEquals(date($nextYear . '-01-31'), $membership['end_date']);
-    $expectedStatus = (strtotime(date('Y-07-14')) > time()) ? 'New' : 'Current';
+    $this->assertEquals(CRM_Utils_Time::date('Y-01-01'), $membership['start_date']);
+    $nextYear = CRM_Utils_Time::date('Y') + 1;
+    $this->assertEquals(CRM_Utils_Time::date($nextYear . '-01-31'), $membership['end_date']);
+    $expectedStatus = (strtotime(CRM_Utils_Time::date('Y-07-14')) > CRM_Utils_Time::time()) ? 'New' : 'Current';
     $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', $expectedStatus), $membership['status_id']);
     $this->assertNotEmpty($membership['contribution_recur_id']);
     $this->assertNotEmpty('original_source', $membership['source']);
 
     $log = $this->callAPISuccessGetSingle('MembershipLog', ['membership_id' => $membership['id'], 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
-    $this->assertEquals(date($nextYear . '-01-01'), $log['start_date']);
-    $this->assertEquals(date($nextYear . '-01-31'), $log['end_date']);
-    $this->assertEquals(date('Y-m-d'), $log['modified_date']);
+    $this->assertEquals(CRM_Utils_Time::date($nextYear . '-01-01'), $log['start_date']);
+    $this->assertEquals(CRM_Utils_Time::date($nextYear . '-01-31'), $log['end_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-m-d'), $log['modified_date']);
 
     $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
     $this->assertEquals($contributionRecur['id'], $membership['contribution_recur_id']);
     $this->assertEquals(0, $contributionRecur['is_email_receipt']);
-    $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
+    $this->assertEquals(date('Y-m-d'), CRM_Utils_Time::date('Y-m-d', strtotime($contributionRecur['modified_date'])));
     $this->assertNotEmpty($contributionRecur['invoice_id']);
     // @todo fix this part!
     /*
@@ -486,7 +486,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $originalMembership = $this->callAPISuccessGetSingle('membership', []);
     $params = [
       'cid' => $this->_individualId,
-      'join_date' => date('m/d/Y'),
+      'join_date' => CRM_Utils_Time::date('m/d/Y'),
       'start_date' => '',
       'end_date' => '',
       // This format reflects the 23 being the organisation & the 25 being the type.
@@ -597,7 +597,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $originalMembership = $this->callAPISuccessGetSingle('membership', []);
     $params = [
       'cid' => $this->_individualId,
-      'join_date' => date('m/d/Y'),
+      'join_date' => CRM_Utils_Time::date('m/d/Y'),
       'start_date' => '',
       'end_date' => '',
       // This format reflects the 23 being the organisation & the 25 being the type.
@@ -680,7 +680,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
       'cvv2' => '123',
       'credit_card_exp_date' => [
         'M' => '9',
-        'Y' => date('Y') + 1,
+        'Y' => CRM_Utils_Time::date('Y') + 1,
       ],
       'credit_card_type' => 'Visa',
       'billing_first_name' => 'Test',
@@ -729,13 +729,13 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $form->testSubmit($params);
     $renewedMembership = $this->callAPISuccessGetSingle('Membership', ['id' => $originalMembership['id']]);
     $this->assertEquals('sauce', $renewedMembership['source']);
-    $this->assertEquals(date('Y-01-01'), $renewedMembership['start_date']);
-    $this->assertEquals(date('2019-03-01'), $renewedMembership['join_date']);
-    $this->assertEquals(date('Y-12-31'), $renewedMembership['end_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-01-01'), $renewedMembership['start_date']);
+    $this->assertEquals(CRM_Utils_Time::date('2019-03-01'), $renewedMembership['join_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-12-31'), $renewedMembership['end_date']);
     $log = $this->callAPISuccessGetSingle('MembershipLog', ['membership_id' => $renewedMembership['id'], 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
-    $this->assertEquals(date('Y-01-01'), $log['start_date']);
-    $this->assertEquals(date('Y-12-31'), $log['end_date']);
-    $this->assertEquals(date('Y-m-d'), $log['modified_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-01-01'), $log['start_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-12-31'), $log['end_date']);
+    $this->assertEquals(CRM_Utils_Time::date('Y-m-d'), $log['modified_date']);
     $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Current'), $log['status_id']);
   }