From 2db00d1d8e2f5027ff1b2b80b9967bd8968a1a8d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 Sep 2023 12:25:35 +1200 Subject: [PATCH] Superficial clean up in test class --- .../api/v3/JobProcessMembershipTest.php | 166 ++++++------------ 1 file changed, 54 insertions(+), 112 deletions(-) diff --git a/tests/phpunit/api/v3/JobProcessMembershipTest.php b/tests/phpunit/api/v3/JobProcessMembershipTest.php index 7e084bbd45..6082de69ce 100644 --- a/tests/phpunit/api/v3/JobProcessMembershipTest.php +++ b/tests/phpunit/api/v3/JobProcessMembershipTest.php @@ -19,108 +19,54 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Api4\Membership; + /** * Class api_v3_JobProcessMembershipTest * @group headless */ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { - protected $_apiversion = 3; - - public $_entity = 'Job'; - - /** - * Caches membership status names in a key, value array - * - * @var array - */ - public $_statuses; - - /** - * Caches membership types in a key, value array - * - * @var array - */ - public $_types; /** * Caches some reference dates * * @var string */ - public $_yesterday; + private $yesterday; /** * @var string */ - public $_today; - - /** - * @var string - */ - public $_tomorrow; + private $tomorrow; public function setUp(): void { parent::setUp(); - $this->loadReferenceDates(); - $this->loadMembershipStatuses(); - $this->loadMembershipTypes(); - } - - public function loadMembershipStatuses() { - $statuses = civicrm_api3('MembershipStatus', 'get', ['options' => ['limit' => 0]])['values']; - $this->_statuses = array_map( - function($status) { - return $status['name']; - }, - $statuses - ); - } - - public function loadMembershipTypes() { - $this->membershipTypeCreate(['name' => 'General']); - $this->membershipTypeCreate(['name' => 'Old']); - $types = civicrm_api3('MembershipType', 'get', ['options' => ['limit' => 0]])['values']; - $this->_types = array_map( - function($type) { - return $type['name']; - }, - $types - ); - } - - public function loadReferenceDates() { - $this->_yesterday = date('Y-m-d', time() - 60 * 60 * 24); - $this->_today = date('Y-m-d'); - $this->_tomorrow = date('Y-m-d', time() + 60 * 60 * 24); + $this->yesterday = date('Y-m-d', time() - 60 * 60 * 24); + $this->tomorrow = date('Y-m-d', time() + 60 * 60 * 24); + $this->membershipTypeCreate(['name' => 'General'], 'General'); + $this->membershipTypeCreate(['name' => 'Old'], 'Old'); } public function tearDown(): void { + $this->restoreMembershipTypes(); parent::tearDown(); - - // For each case, the `old` membershipt type must start as - // active, so we can assign it (we'll disabled it after - // assigning it) - $this->callAPISuccess('MembershipType', 'create', [ - 'id' => array_search('Old', $this->_types), - 'is_active' => TRUE, - ]); } /** * Creates a membership that is expired but that should be ignored * by the process as it is in `deceased` status. */ - public function createDeceasedMembershipThatShouldBeExpired() { + public function createDeceasedMembershipThatShouldBeExpired(): int { $contactId = $this->individualCreate(['is_deceased' => FALSE]); $membershipId = $this->contactMembershipCreate([ 'contact_id' => $contactId, - 'start_date' => $this->_yesterday, - 'end_date' => $this->_yesterday, + 'start_date' => $this->yesterday, + 'end_date' => $this->yesterday, ]); $this->callAPISuccess('Membership', 'create', [ 'id' => $membershipId, - 'status_id' => array_search('Deceased', $this->_statuses), + 'status_id' => 'Deceased', ]); return $membershipId; @@ -131,21 +77,21 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * in `current` status but that won't be updated unless the process * is explicitly told not to exclude tests. */ - public function createTestMembershipThatShouldBeCurrent() { + public function createTestMembershipThatShouldBeCurrent(): int { $contactId = $this->individualCreate(); - $membershipId = $this->contactMembershipCreate([ + $membershipID = $this->contactMembershipCreate([ 'contact_id' => $contactId, - 'start_date' => $this->_yesterday, - 'end_date' => $this->_tomorrow, + 'start_date' => $this->yesterday, + 'end_date' => $this->tomorrow, 'is_test' => TRUE, ]); $this->callAPISuccess('Membership', 'create', [ - 'id' => $membershipId, - 'status_id' => array_search('Grace', $this->_statuses), + 'id' => $membershipID, + 'status_id' => 'Grace', ]); - return $membershipId; + return $membershipID; } /** @@ -153,17 +99,17 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * that should be fixed even when the process is executed with * the default parameters. */ - public function createGraceMembershipThatShouldBeCurrent() { + public function createGraceMembershipThatShouldBeCurrent(): int { $contactId = $this->individualCreate(); $membershipId = $this->contactMembershipCreate([ 'contact_id' => $contactId, - 'start_date' => $this->_yesterday, - 'end_date' => $this->_tomorrow, + 'start_date' => $this->yesterday, + 'end_date' => $this->tomorrow, ]); $this->callAPISuccess('Membership', 'create', [ 'id' => $membershipId, - 'status_id' => array_search('Grace', $this->_statuses), + 'status_id' => 'Grace', ]); return $membershipId; @@ -175,17 +121,17 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * with an explicit `exclude_membership_status_ids` list that * doesn't include it. */ - public function createPendingMembershipThatShouldBeCurrent() { + public function createPendingMembershipThatShouldBeCurrent(): int { $contactId = $this->individualCreate(); $membershipId = $this->contactMembershipCreate([ 'contact_id' => $contactId, - 'start_date' => $this->_yesterday, - 'end_date' => $this->_tomorrow, + 'start_date' => $this->yesterday, + 'end_date' => $this->tomorrow, ]); $this->callAPISuccess('Membership', 'create', [ 'id' => $membershipId, - 'status_id' => array_search('Pending', $this->_statuses), + 'status_id' => 'Pending', ]); return $membershipId; @@ -195,22 +141,22 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * Creates a membership that uses an inactive membership type * and should be in `current` status. */ - public function createOldMembershipThatShouldBeCurrent() { + public function createOldMembershipThatShouldBeCurrent(): int { $contactId = $this->individualCreate(); $membershipId = $this->contactMembershipCreate([ 'contact_id' => $contactId, - 'start_date' => $this->_yesterday, - 'end_date' => $this->_tomorrow, - 'membership_type_id' => array_search('Old', $this->_types), + 'start_date' => $this->yesterday, + 'end_date' => $this->tomorrow, + 'membership_type_id' => 'Old', ]); $this->callAPISuccess('Membership', 'create', [ 'id' => $membershipId, - 'status_id' => array_search('Grace', $this->_statuses), + 'status_id' => 'Grace', ]); $this->callAPISuccess('MembershipType', 'create', [ - 'id' => array_search('Old', $this->_types), + 'id' => $this->ids['MembershipType']['Old'], 'is_active' => FALSE, ]); @@ -219,11 +165,15 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { /** * Returns the name of the status of a membership given its id. + * + * @param int $membershipID + * + * @return string + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection */ - public function getMembershipStatus($membershipId) { - $membership = $this->callAPISuccess('Membership', 'getsingle', ['id' => $membershipId]); - $statusId = $membership['status_id']; - return $this->_statuses[$statusId]; + public function getMembershipStatus(int $membershipID): string { + return (string) Membership::get()->addWhere('id', '=', $membershipID)->addSelect('status_id:name')->execute()->first()['status_id:name']; } /** @@ -242,9 +192,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { */ public function testByDefaultInactiveAreExcluded(): void { $oldId = $this->createOldMembershipThatShouldBeCurrent(); - $this->callAPISuccess('job', 'process_membership', []); - $this->assertEquals('Grace', $this->getMembershipStatus($oldId)); } @@ -252,11 +200,9 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * Test that by default grace memberships are considered. */ public function testByDefaultGraceIsConsidered(): void { - $graceId = $this->createGraceMembershipThatShouldBeCurrent(); - + $graceID = $this->createGraceMembershipThatShouldBeCurrent(); $this->callAPISuccess('job', 'process_membership', []); - - $this->assertEquals('Current', $this->getMembershipStatus($graceId)); + $this->assertEquals('Current', $this->getMembershipStatus($graceID)); } /** @@ -266,22 +212,18 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { * exclude_membership_status_ids list by default. */ public function testByDefaultPendingIsExcluded(): void { - $pendingId = $this->createPendingMembershipThatShouldBeCurrent(); - + $pendingID = $this->createPendingMembershipThatShouldBeCurrent(); $this->callAPISuccess('job', 'process_membership', []); - - $this->assertEquals('Pending', $this->getMembershipStatus($pendingId)); + $this->assertEquals('Pending', $this->getMembershipStatus($pendingID)); } /** * Test that by default memberships of type deceased are excluded. */ public function testByDefaultDeceasedIsExcluded(): void { - $deceasedId = $this->createDeceasedMembershipThatShouldBeExpired(); - + $deceasedID = $this->createDeceasedMembershipThatShouldBeExpired(); $this->callAPISuccess('job', 'process_membership', []); - - $this->assertEquals('Deceased', $this->getMembershipStatus($deceasedId)); + $this->assertEquals('Deceased', $this->getMembershipStatus($deceasedID)); } /** @@ -331,7 +273,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { /** * Test that when including test memberships, - * acually includes test memberships. + * actually includes test memberships. */ public function testIncludingTestMembershipsActuallyIncludesThem(): void { $testId = $this->createTestMembershipThatShouldBeCurrent(); @@ -439,7 +381,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { $this->callAPISuccess('job', 'process_membership', [ 'exclude_membership_status_ids' => [ - array_search('Cancelled', $this->_statuses), + CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ], ]); @@ -455,7 +397,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { $this->callAPISuccess('job', 'process_membership', [ 'exclude_membership_status_ids' => [ - array_search('Cancelled', $this->_statuses), + CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ], ]); @@ -471,7 +413,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { $this->callAPISuccess('job', 'process_membership', [ 'exclude_membership_status_ids' => [ - array_search('Cancelled', $this->_statuses), + CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ], ]); @@ -487,7 +429,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { $this->callAPISuccess('job', 'process_membership', [ 'exclude_membership_status_ids' => [ - array_search('Cancelled', $this->_statuses), + CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ], ]); @@ -504,7 +446,7 @@ class api_v3_JobProcessMembershipTest extends CiviUnitTestCase { $this->callAPISuccess('job', 'process_membership', [ 'exclude_membership_status_ids' => [ - array_search('Cancelled', $this->_statuses), + CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'), ], ]); -- 2.25.1