From ac4760c52fe000ff4f429e0fd7d71f9e8660dc61 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 24 May 2021 17:48:28 +1200 Subject: [PATCH] [REF] Extend testing on membership batch entry & minor cleanup This extends the cover on the batch entry test (by doing post test validation) and does a minor extraction on the getDomainEmail address --- CRM/Batch/Form/Entry.php | 29 ++++--- tests/phpunit/CRM/Batch/Form/EntryTest.php | 90 +++++++++++++++------- 2 files changed, 75 insertions(+), 44 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 01f754355b..334266185c 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -644,10 +644,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { //send receipt mail. if ($contribution->id && !empty($value['send_receipt'])) { - // add the domain email id - $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); - $domainEmail = "$domainEmail[0] <$domainEmail[1]>"; - $value['from_email_address'] = $domainEmail; + $value['from_email_address'] = $this->getFromEmailAddress(); $value['contribution_id'] = $contribution->id; if (!empty($value['soft_credit'])) { $value = array_merge($value, CRM_Contribute_BAO_ContributionSoft::getSoftContribution($contribution->id)); @@ -868,12 +865,6 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { //send receipt mail. if ($membership->id && !empty($value['send_receipt'])) { - - // add the domain email id - $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); - $domainEmail = "$domainEmail[0] <$domainEmail[1]>"; - - $value['from_email_address'] = $domainEmail; $value['membership_id'] = $membership->id; $value['contribution_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', $membership->id, 'contribution_id', 'membership_id'); $this->emailReceipt($this, $value, $membership); @@ -894,17 +885,14 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { * * @return bool * true if mail was sent successfully - * @throws \CRM_Core_Exception + * @throws \CRM_Core_Exception|\API_Exception * * @deprecated * This function is shared with Batch_Entry which has limited overlap * & needs rationalising. * */ - public function emailReceipt($form, &$formValues, $membership) { - // retrieve 'from email id' for acknowledgement - $receiptFrom = $formValues['from_email_address'] ?? NULL; - + protected function emailReceipt($form, &$formValues, $membership): bool { // @todo figure out how much of the stuff below is genuinely shared with the batch form & a logical shared place. if (!empty($formValues['payment_instrument_id'])) { $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); @@ -949,7 +937,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { 'groupName' => 'msg_tpl_workflow_membership', 'valueName' => 'membership_offline_receipt', 'contactId' => $form->_receiptContactId, - 'from' => $receiptFrom, + 'from' => $this->getFromEmailAddress(), 'toName' => $form->_contributorDisplayName, 'toEmail' => $form->_contributorEmail, 'PDFFilename' => ts('receipt') . '.pdf', @@ -1231,4 +1219,13 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { return $membership; } + /** + * @return string + * @throws \CRM_Core_Exception + */ + private function getFromEmailAddress(): string { + $domainEmail = CRM_Core_BAO_Domain::getNameAndEmail(); + return "$domainEmail[0] <$domainEmail[1]>"; + } + } diff --git a/tests/phpunit/CRM/Batch/Form/EntryTest.php b/tests/phpunit/CRM/Batch/Form/EntryTest.php index d080eee3ee..e6c74018b1 100644 --- a/tests/phpunit/CRM/Batch/Form/EntryTest.php +++ b/tests/phpunit/CRM/Batch/Form/EntryTest.php @@ -53,7 +53,8 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { * * @var string */ - protected $_contactID = NULL; + protected $_contactID; + /** * Contact id used in test function. * @@ -75,6 +76,12 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { */ protected $_contactID4 = NULL; + /** + * Validate all financial entities before tear down. + * @var bool + */ + protected $isValidateFinancialsOnPostAssert = TRUE; + /** * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception @@ -171,9 +178,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { * * @param string $thousandSeparator * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + * * @dataProvider getThousandSeparators */ - public function testProcessMembership($thousandSeparator) { + public function testProcessMembership(string $thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); $form = new CRM_Batch_Form_Entry(); @@ -182,25 +193,28 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { $params = $this->getMembershipData(); $this->assertEquals(4500.0, $form->testProcessMembership($params)); - $result = $this->callAPISuccess('membership', 'get'); - $this->assertEquals(3, $result['count']); + $memberships = $this->callAPISuccess('Membership', 'get')['values']; + $this->assertCount(3, $memberships); + + $this->assertNotEmpty($memberships[1]['campaign_id']); + //check start dates #1 should default to 1 Jan this year, #2 should be as entered - $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']); - $this->assertEquals('2013-02-03', $result['values'][2]['start_date']); + $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $memberships[1]['start_date']); + $this->assertEquals('2013-02-03', $memberships[2]['start_date']); //check start dates #1 should default to 1 Jan this year, #2 should be as entered - $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']); - $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']); - $this->assertEquals('2013-12-01', $result['values'][3]['end_date']); + $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $memberships[1]['end_date']); + $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $memberships[2]['end_date']); + $this->assertEquals('2013-12-01', $memberships[3]['end_date']); //check start dates #1 should default to 1 Jan this year, #2 should be as entered - $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']); - $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']); - $this->assertEquals(date('Y-m-d'), $result['values'][3]['join_date']); - $result = $this->callAPISuccess('contribution', 'get', ['return' => ['total_amount', 'trxn_id']]); - $this->assertEquals(3, $result['count']); - foreach ($result['values'] as $key => $contribution) { - $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', [ + $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $memberships[1]['join_date']); + $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $memberships[2]['join_date']); + $this->assertEquals(date('Y-m-d'), $memberships[3]['join_date']); + $memberships = $this->callAPISuccess('Contribution', 'get', ['return' => ['total_amount', 'trxn_id']]); + $this->assertEquals(3, $memberships['count']); + foreach ($memberships['values'] as $key => $contribution) { + $this->assertEquals($this->callAPISuccess('LineItem', 'getvalue', [ 'contribution_id' => $contribution['id'], 'return' => 'line_total', @@ -216,8 +230,9 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { * @param $thousandSeparator * * @dataProvider getThousandSeparators + * @throws \CRM_Core_Exception */ - public function testProcessContribution($thousandSeparator) { + public function testProcessContribution($thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); $this->offsetDefaultPriceSet(); $form = new CRM_Batch_Form_Entry(); @@ -241,17 +256,20 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { /** * CRM-18000 - Test start_date, end_date after renewal + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ - public function testMembershipRenewalDates() { + public function testMembershipRenewalDates(): void { $form = new CRM_Batch_Form_Entry(); - $campaignID = Campaign::create()->setValues(['name' => 'blah', 'title' => 'blah'])->execute()->first()['id']; foreach ([$this->_contactID, $this->_contactID2] as $contactID) { $membershipParams = [ 'membership_type_id' => $this->_membershipTypeID2, 'contact_id' => $contactID, - 'start_date' => "01/01/2015", - 'join_date' => "01/01/2010", - 'end_date' => "12/31/2015", + 'start_date' => '01/01/2015', + 'join_date' => '01/01/2010', + 'end_date' => '12/31/2015', ]; $this->contactMembershipCreate($membershipParams); } @@ -264,7 +282,6 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { ]; $params['field'][1]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2]; $params['field'][1]['receive_date'] = date('Y-m-d'); - $params['field'][1]['member_campaign_id'] = $campaignID; // explicitly specify start and end dates $params['field'][2]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2]; @@ -273,13 +290,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { $params['field'][2]['receive_date'] = "2016-04-01"; $this->assertEquals(3.0, $form->testProcessMembership($params)); - $result = $this->callAPISuccess('membership', 'get')['values']; + $result = $this->callAPISuccess('Membership', 'get')['values']; // renewal dates should be from current if start_date and end_date is passed as NULL $this->assertEquals(date('Y-m-d'), $result[1]['start_date']); - $endDate = date("Y-m-d", strtotime(date("Y-m-d") . " +1 year -1 day")); + $endDate = date('Y-m-d', strtotime(date("Y-m-d") . " +1 year -1 day")); $this->assertEquals($endDate, $result[1]['end_date']); - $this->assertEquals(1, $result[1]['campaign_id']); + $this->assertEquals($params['field'][1]['member_campaign_id'], $result[1]['campaign_id']); // verify if the modified dates asserts with the dates passed above $this->assertEquals('2016-04-01', $result[2]['start_date']); @@ -289,10 +306,12 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { /** * Data provider for test process membership. + * * @return array + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ - public function getMembershipData() { - + public function getMembershipData(): array { return [ 'batch_id' => 4, 'primary_profiles' => [1 => NULL, 2 => NULL, 3 => NULL], @@ -316,6 +335,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { 'trxn_id' => 'TX101', 'check_number' => NULL, 'contribution_status_id' => 1, + 'member_campaign_id' => $this->createCampaign(), ], 2 => [ 'membership_type' => [0 => $this->_orgContactID, 1 => $this->_membershipTypeID], @@ -403,4 +423,18 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { ]; } + /** + * Create a campaign. + * + * @return mixed + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + private function createCampaign(): int { + return (int) Campaign::create()->setValues([ + 'name' => 'blah', + 'title' => 'blah', + ])->execute()->first()['id']; + } + } -- 2.25.1