[NFC] [Test] Preliminary cleanup
authoreileen <emcnaughton@wikimedia.org>
Thu, 8 Apr 2021 23:52:07 +0000 (11:52 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 8 Apr 2021 23:52:07 +0000 (11:52 +1200)
tests/phpunit/CRM/Member/BAO/MembershipTest.php

index 0358ea0d6ea06f031812f92ebe1a6c17166d0fcf..dbbcaa2a834517dfbcddc05be1470d597fe8c105 100644 (file)
@@ -19,13 +19,18 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   private $_membershipStatusID;
   private $_membershipTypeID;
 
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   * @throws \Exception
+   */
   public function setUp(): void {
     parent::setUp();
 
     $this->_contactID = $this->organizationCreate();
     $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]);
     // add a random number to avoid silly conflicts with old data
-    $this->_membershipStatusID = $this->membershipStatusCreate('test status' . rand(1, 1000));
+    $this->_membershipStatusID = $this->membershipStatusCreate('test status' . random_int(1, 1000));
   }
 
   /**
@@ -64,7 +69,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       'duration_unit' => "year",
       'duration_interval' => 1,
       'period_type' => "rolling",
-      'name' => 'Organiation Membership Type',
+      'name' => 'Organization Membership Type',
       'relationship_type_id' => ($withRelationship) ? 5 : NULL,
       'relationship_direction' => ($withRelationship) ? 'b_a' : NULL,
       'max_related' => $maxRelated ?: NULL,
@@ -75,8 +80,11 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
   /**
    * Get count of related memberships by parent membership id.
+   *
    * @param $membershipId
+   *
    * @return array|int
+   * @throws \CRM_Core_Exception
    */
   private function getRelatedMembershipsCount($membershipId) {
     return $this->callAPISuccess("Membership", "getcount", [
@@ -85,10 +93,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test to delete related membership when type of parent memebrship is changed which does not have relation type associated.
-   * @throws CRM_Core_Exception
+   * Test to delete related membership when type of parent membership is changed which does not have relation type associated.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testDeleteRelatedMembershipsOnParentTypeChanged() {
+  public function testDeleteRelatedMembershipsOnParentTypeChanged(): void {
 
     $contactId = $this->individualCreate();
     $membershipOrganizationId = $this->organizationCreate();
@@ -122,7 +132,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
     // Update membership by changing it's type. New membership type is without relationship.
     $membership["membership_type_id"] = $membershipTypeWithoutRelationship["id"];
-    $updatedMembership = $this->callAPISuccess("Membership", "create", $membership);
+    $this->callAPISuccess("Membership", "create", $membership);
 
     // Check count of related memberships again. It should be zero as we changed the membership type.
     $relatedMembershipsCount = $this->getRelatedMembershipsCount($membership["id"]);
@@ -132,9 +142,13 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->membershipDelete($membership["id"]);
   }
 
-  public function testCreate() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testCreate(): void {
 
-    list($contactId, $membershipId) = $this->setupMembership();
+    [$contactId, $membershipId] = $this->setupMembership();
 
     // Now call create() to modify an existing Membership
     $params = [
@@ -160,7 +174,11 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
-  public function testGetValues() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testGetValues(): void {
     //        $this->markTestSkipped( 'causes mysterious exit, needs fixing!' );
     //  Calculate membership dates based on the current date
     $now = time();
@@ -217,8 +235,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
-  public function testRetrieve() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testRetrieve(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     $params = ['id' => $membershipId];
     $values = [];
     CRM_Member_BAO_Membership::retrieve($params, $values);
@@ -228,7 +250,11 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
-  public function testActiveMembers() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testActiveMembers(): void {
     $contactId = $this->individualCreate();
 
     $params = [
@@ -281,15 +307,19 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->assertEquals($activeMembers[$membershipId1]['id'], $membership[$membershipId1]['id'], 'Verify active membership record is retrieved.');
     $this->assertEquals($activeMembers[$membershipId2]['id'], $membership[$membershipId2]['id'], 'Verify active membership record is retrieved.');
 
-    $this->assertEquals(0, count($inActiveMembers), 'Verify No inactive membership record is retrieved.');
+    $this->assertCount(0, $inActiveMembers, 'Verify No inactive membership record is retrieved.');
 
     $this->membershipDelete($membershipId1);
     $this->membershipDelete($membershipId2);
     $this->contactDelete($contactId);
   }
 
-  public function testDeleteMembership() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testDeleteMembership(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     CRM_Member_BAO_Membership::del($membershipId);
 
     $this->assertDBNull('CRM_Member_BAO_Membership', $contactId, 'id',
@@ -301,9 +331,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
-  public function testGetContactMembership() {
-    list($contactId, $membershipId) = $this->setupMembership();
-
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testGetContactMembership(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     $membership = CRM_Member_BAO_Membership::getContactMembership($contactId, $this->_membershipTypeID, FALSE);
 
     $this->assertEquals($membership['id'], $membershipId, 'Verify membership record is retrieved.');
@@ -315,9 +348,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   /**
    * Get the contribution.
    * page id from the membership record
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testgetContributionPageId() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  public function testGetContributionPageId(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     $membership[$membershipId]['renewPageId'] = CRM_Member_BAO_Membership::getContributionPageId($membershipId);
 
     $this->membershipDelete($membershipId);
@@ -328,9 +364,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    * Get membership joins/renewals
    * for a specified membership
    * type.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testgetMembershipStarts() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  public function testGetMembershipStarts(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     $yearStart = date('Y') . '0101';
     $currentDate = date('Ymd');
     CRM_Member_BAO_Membership::getMembershipStarts($this->_membershipTypeID, $yearStart, $currentDate);
@@ -342,9 +381,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   /**
    * Get a count of membership for a specified membership type,
    * optionally for a specified date.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testGetMembershipCount() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  public function testGetMembershipCount(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
     $currentDate = date('Ymd');
     $test = 0;
     CRM_Member_BAO_Membership::getMembershipCount($this->_membershipTypeID, $currentDate, $test);
@@ -355,8 +397,11 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
   /**
    * Checkup sort name function.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testSortName() {
+  public function testSortName(): void {
     $contactId = $this->individualCreate();
 
     $params = [
@@ -380,9 +425,12 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
   /**
    * Delete related memberships.
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testdeleteRelatedMemberships() {
-    list($contactId, $membershipId) = $this->setupMembership();
+  public function testDeleteRelatedMemberships(): void {
+    [$contactId, $membershipId] = $this->setupMembership();
 
     CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId);
 
@@ -393,14 +441,14 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   /**
    * Renew membership with change in membership type.
    *
-   * @fixme Note that this test fails when today is August 29 2019 (and maybe other years?):
-   *   Verify correct end date is calculated after membership renewal
-   *   Failed asserting that two strings are equal.
-   *   Expected-'2021-03-01'
-   *   Actual+'2021-02-28'
-   *   /home/jenkins/bknix-dfl/build/core-15165-73etc/web/sites/all/modules/civicrm/tests/phpunit/CRM/Member/BAO/MembershipTest.php:609
+   * @fixme Note that this test fails when today is August 29 2019 (and maybe
+   *   other years?): Verify correct end date is calculated after membership
+   *   renewal Failed asserting that two strings are equal.
+   *   Expected-'2021-03-01' Actual+'2021-02-28'
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testRenewMembership() {
+  public function testRenewMembership(): void {
     $contactId = $this->individualCreate();
     $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -6 month"));
     $endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
@@ -423,7 +471,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       $membership['id'],
       'id',
       'membership_id',
-      'Database checked on membershiplog record.'
+      'Database checked on membership log record.'
     );
 
     // this is a test and we dont want qfKey generation / validation
@@ -432,7 +480,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $config->keyDisable = TRUE;
 
     $isTestMembership = 0;
-    list($MembershipRenew) = CRM_Member_BAO_Membership::processMembership(
+    [$MembershipRenew] = CRM_Member_BAO_Membership::processMembership(
       $contactId,
       $this->_membershipTypeID,
       $isTestMembership,
@@ -452,7 +500,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       $MembershipRenew->id,
       'id',
       'membership_id',
-      'Database checked on membershiplog record.'
+      'Database checked on membership log record.'
     );
     $this->assertEquals($this->_membershipTypeID, $MembershipRenew->membership_type_id, 'Verify membership type is changed during renewal.');
     $this->assertEquals($endDate, $MembershipRenew->end_date, 'Verify correct end date is calculated after membership renewal');
@@ -467,7 +515,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testStaleMembership() {
+  public function testStaleMembership(): void {
     $statusId = 3;
     $contactId = $this->individualCreate();
     $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -1 year -15 days"));
@@ -501,7 +549,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       'Database checked on membership log record.'
     );
 
-    list($MembershipRenew) = CRM_Member_BAO_Membership::processMembership(
+    [$MembershipRenew] = CRM_Member_BAO_Membership::processMembership(
       $contactId,
       $this->_membershipTypeID,
       FALSE,
@@ -527,12 +575,16 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
-  public function testUpdateAllMembershipStatusConvertExpiredOverriddenStatusToNormal() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testUpdateAllMembershipStatusConvertExpiredOverriddenStatusToNormal(): void {
     $params = [
       'contact_id' => $this->individualCreate(),
       'membership_type_id' => $this->_membershipTypeID,
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'source' => 'Payment',
       'is_override' => 1,
@@ -555,16 +607,20 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->assertArrayNotHasKey('status_override_end_date', $membershipAfterProcess);
   }
 
-  public function testUpdateAllMembershipStatusHandleOverriddenWithEndOverrideDateEqualTodayAsExpired() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testUpdateAllMembershipStatusHandleOverriddenWithEndOverrideDateEqualTodayAsExpired(): void {
     $params = [
       'contact_id' => $this->individualCreate(),
       'membership_type_id' => $this->_membershipTypeID,
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'source' => 'Payment',
       'is_override' => 1,
-      'status_override_end_date' => date('Ymd', time()),
+      'status_override_end_date' => date('Ymd'),
       'status_id' => $this->_membershipStatusID,
     ];
 
@@ -583,12 +639,16 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->assertArrayNotHasKey('status_override_end_date', $membershipAfterProcess);
   }
 
-  public function testUpdateAllMembershipStatusDoesNotConvertOverridenMembershipWithoutEndOverrideDateToNormal() {
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function testUpdateAllMembershipStatusDoesNotConvertOverriddenMembershipWithoutEndOverrideDateToNormal(): void {
     $params = [
       'contact_id' => $this->individualCreate(),
       'membership_type_id' => $this->_membershipTypeID,
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'source' => 'Payment',
       'is_override' => 1,
@@ -597,7 +657,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
     $createdMembershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
 
-    civicrm_api3('Job', 'process_membership');
+    $this->callAPISuccess('Job', 'process_membership');
 
     $membershipAfterProcess = civicrm_api3('Membership', 'get', [
       'sequential' => 1,
@@ -611,8 +671,9 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
 
   /**
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  public function testMembershipPaymentForSingleContributionMultipleMembership() {
+  public function testMembershipPaymentForSingleContributionMultipleMembership(): void {
     $membershipTypeID1 = $this->membershipTypeCreate(['name' => 'Parent']);
     $membershipTypeID2 = $this->membershipTypeCreate(['name' => 'Child']);
     $financialTypeId = $this->getFinancialTypeId('Member Dues');
@@ -677,8 +738,8 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       'contact_id' => $this->individualCreate(),
       'membership_type_id' => $membershipTypeID2,
       'contribution_recur_id' => $contributionRecur['id'],
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'source' => 'Payment',
     ];
@@ -686,8 +747,8 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       'contact_id' => $this->individualCreate(),
       'membership_type_id' => $membershipTypeID2,
       'contribution_recur_id' => $contributionRecur['id'],
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'source' => 'Payment',
     ];
@@ -738,8 +799,8 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
       'contact_id' => $parentContactId,
       'membership_type_id' => $membershipTypeID1,
       'contribution_recur_id' => $contributionRecur['id'],
-      'join_date' => date('Ymd', time()),
-      'start_date' => date('Ymd', time()),
+      'join_date' => date('Ymd'),
+      'start_date' => date('Ymd'),
       'end_date' => date('Ymd', strtotime('+1 year')),
       'skipLineItem' => TRUE,
       'source' => 'Payment',
@@ -766,7 +827,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    *
    * @throws \CiviCRM_API3_Exception
    */
-  public function testBuildMembershipTypeValues() {
+  public function testBuildMembershipTypeValues(): void {
     $this->restoreMembershipTypes();
     $form = new CRM_Core_Form();
     $values = CRM_Member_BAO_Membership::buildMembershipTypeValues($form);
@@ -837,8 +898,9 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    * https://lab.civicrm.org/dev/core/-/issues/1854
    *
    * @throws \CRM_Core_Exception
+   * @throws \Exception
    */
-  public function testMembershipUpdateDoesNotDeleteRelatedMembershipsByMistake() {
+  public function testMembershipUpdateDoesNotDeleteRelatedMembershipsByMistake(): void {
     $membershipOrganizationId = $this->organizationCreate();
     $employerId = $this->organizationCreate();
 
@@ -864,7 +926,7 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
         'is_active'            => 1,
       ]);
     }
-    $this->deleteRelatedMemberhsips($membership["id"]);
+    $this->deleteRelatedMemberships($membership["id"]);
     $this->assertEquals(0, $this->getRelatedMembershipsCount($membership["id"]), 'Related membership count should be 0.');
 
     // Create related memberships for first and last contact.
@@ -873,10 +935,10 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->assertEquals(2, $this->getRelatedMembershipsCount($membership["id"]), 'Related membership count should be 2.');
 
     // Reset statics.
-    unset(\Civi::$statics[CRM_Member_BAO_Membership::class]['related_contacts']);
+    unset(Civi::$statics[CRM_Member_BAO_Membership::class]['related_contacts']);
 
     // Update membership by changing its status.
-    $otherStatusID = $this->membershipStatusCreate('another status ' . rand(1, 1000));
+    $otherStatusID = $this->membershipStatusCreate('another status ' . random_int(1, 1000));
     $membership["status_id"] = $otherStatusID;
     $this->callAPISuccess("Membership", "create", $membership);
 
@@ -886,8 +948,8 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
     $this->assertMembershipExists($relatedMembership1['id']);
     $this->assertMembershipExists($relatedMembership2['id']);
 
-    // Clean up: Delete exerything!
-    $this->membershipDelete($membership["id"]);
+    // Clean up: Delete everything!
+    $this->membershipDelete($membership['id']);
     $this->membershipStatusDelete($otherStatusID);
   }
 
@@ -897,9 +959,9 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    * @param int $count
    *
    * @return array
-   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
-  private function createContacts($count) {
+  private function createContacts(int $count): array {
     $contacts = [];
     for ($i = 0; $i < $count; $i++) {
       $contacts[] = $this->individualCreate();
@@ -915,8 +977,8 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    *
    * @throws \CRM_Core_Exception
    */
-  private function deleteRelatedMemberhsips($parentMembershipID) {
-    $memberships = $this->callAPISuccess("Membership", "get", [
+  private function deleteRelatedMemberships(int $parentMembershipID): void {
+    $this->callAPISuccess('Membership', 'get', [
       'owner_membership_id' => $parentMembershipID,
       'api.Membership.delete' => ['id' => '$value.id'],
     ]);
@@ -931,25 +993,23 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
    * @return array
    * @throws \CRM_Core_Exception
    */
-  private function createRelatedMembershipForContact($contactID, $parentMembership) {
-    $membership = $this->callAPISuccess("Membership", "create", [
-      'membership_type_id' => $parentMembership["membership_type_id"],
+  private function createRelatedMembershipForContact(int $contactID, array $parentMembership): array {
+    return $this->callAPISuccess('Membership', 'create', [
+      'membership_type_id' => $parentMembership['membership_type_id'],
       'contact_id'         => $contactID,
       'status_id'          => $this->_membershipStatusID,
       'owner_membership_id' => $parentMembership['id'],
     ]);
-
-    return $membership;
   }
 
   /**
    * Checks the given membership ID can be found.
    *
-   * @param $membershipID
+   * @param int $membershipID
    *
    * @throws \CRM_Core_Exception
    */
-  private function assertMembershipExists($membershipID) {
+  private function assertMembershipExists(int $membershipID): void {
     $membership = $this->callAPISuccess("Membership", "get", [
       'id' => $membershipID,
     ]);