From: eileen Date: Thu, 29 Apr 2021 05:40:32 +0000 (+1200) Subject: [Test] Add test cover for contribution tokens in pdf letter X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=edcbcbb07c3855ebcd9889447ba06e584682e530;p=civicrm-core.git [Test] Add test cover for contribution tokens in pdf letter --- diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index b6ac0faef9..41a9286306 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -18,6 +18,8 @@ */ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; + protected $_individualId; protected $_docTypes; @@ -42,12 +44,15 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { /** * Clean up after each test. * + * @throws \API_Exception * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public function tearDown(): void { $this->quickCleanUpFinancialEntities(); - $this->quickCleanup(['civicrm_uf_match']); + $this->quickCleanup(['civicrm_uf_match', 'civicrm_campaign'], TRUE); CRM_Utils_Hook::singleton()->reset(); + parent::tearDown(); } /** @@ -248,6 +253,124 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { $this->assertStringContainsString('Mr. Anthony Anderson II', $html); } + /** + * Test all contribution tokens. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testAllContributionTokens(): void { + $this->createLoggedInUser(); + $this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Contribution']); + $this->campaignCreate(['name' => 'Big one', 'title' => 'Big one']); + $tokens = $this->getAllContributionTokens(); + $formValues = [ + 'document_type' => 'pdf', + 'html_message' => '', + ]; + foreach (array_keys($this->getAllContributionTokens()) as $token) { + $formValues['html_message'] .= "$token : {contribution.$token}\n"; + } + /* @var $form CRM_Contribute_Form_Task_PDFLetter */ + $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); + $form->setContributionIds([$this->createContribution(array_merge(['campaign_id' => $tokens['campaign']], $tokens))]); + try { + $form->postProcess(); + } + catch (CRM_Core_Exception_PrematureExitException $e) { + $html = $e->errorData['html']; + } + $this->assertEquals(' + + + + + +' . " \n" . ' + +
+contribution_id : 1 +total_amount : € 9,999.99 +fee_amount : € 1,111.11 +net_amount : € 7,777.78 +non_deductible_amount : € 2,222.22 +receive_date : July 20th, 2018 12:00 AM +payment_instrument : Check +trxn_id : 1234 +invoice_id : 568 +currency : EUR +cancel_date : 2019-12-30 00:00:00 +cancel_reason : Contribution Cancel Reason +receipt_date : October 30th, 2019 12:00 AM +thankyou_date : 2019-11-30 00:00:00 +contribution_source : Contribution Source +amount_level : Amount Level +contribution_status_id : 2 +check_number : 6789 +campaign : Big one +' . $this->getCustomFieldName('text') . ' : Bobsled +' . $this->getCustomFieldName('select_string') . ' : Red +' . $this->getCustomFieldName('select_date') . ' : 01/20/2021 12:00AM +' . $this->getCustomFieldName('int') . ' : 999 +' . $this->getCustomFieldName('link') . ' : http://civicrm.org +' . $this->getCustomFieldName('country') . ' : New Zealand +' . $this->getCustomFieldName('multi_country') . ' : France, Canada +' . $this->getCustomFieldName('contact_reference') . ' : Mr. Spider Man II +' . $this->getCustomFieldName('state') . ' : Queensland +' . $this->getCustomFieldName('multi_state') . ' : Victoria, New South Wales +' . $this->getCustomFieldName('boolean') . ' : Yes +' . $this->getCustomFieldName('checkbox') . ' : Purple +
+ +', $html); + } + + /** + * Get all the tokens available to contributions. + * + * @return array + * @throws \CiviCRM_API3_Exception + */ + public function getAllContributionTokens(): array { + return [ + 'contribution_id' => '', + 'total_amount' => '9999.99', + 'fee_amount' => '1111.11', + 'net_amount' => '7777.78', + 'non_deductible_amount' => '2222.22', + 'receive_date' => '2018-07-20', + 'payment_instrument' => 'Check', + 'trxn_id' => '1234', + 'invoice_id' => '568', + 'currency' => 'EUR', + 'cancel_date' => '2019-12-30', + 'cancel_reason' => 'Contribution Cancel Reason', + 'receipt_date' => '2019-10-30', + 'thankyou_date' => '2019-11-30', + 'contribution_source' => 'Contribution Source', + 'amount_level' => 'Amount Level', + 'contribution_status_id' => 'Pending', + 'check_number' => '6789', + 'campaign' => 'Big one', + $this->getCustomFieldName('text') => 'Bobsled', + $this->getCustomFieldName('select_string') => 'R', + $this->getCustomFieldName('select_date') => '2021-01-20', + $this->getCustomFieldName('int') => 999, + $this->getCustomFieldName('link') => 'http://civicrm.org', + $this->getCustomFieldName('country') => 'New Zealand', + $this->getCustomFieldName('multi_country') => ['France', 'Canada'], + $this->getCustomFieldName('contact_reference') => $this->individualCreate(['first_name' => 'Spider', 'last_name' => 'Man']), + $this->getCustomFieldName('state') => 'Queensland', + $this->getCustomFieldName('multi_state') => ['Victoria', 'New South Wales'], + $this->getCustomFieldName('boolean') => TRUE, + $this->getCustomFieldName('checkbox') => 'P', + $this->getCustomFieldName('contact_reference') => $this->individualCreate(['first_name' => 'Spider', 'last_name' => 'Man']), + + ]; + } + /** * Test assignment of variables when using the group by function. * @@ -537,15 +660,13 @@ value=$contact_aggregate+$contribution.total_amount} * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - protected function createContribution() { - $contributionParams = [ + protected function createContribution($contributionParams = []) { + $contributionParams = array_merge([ 'contact_id' => $this->individualCreate(), 'total_amount' => 100, 'financial_type_id' => 'Donation', - ]; - $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); - $contributionId = $contribution['id']; - return $contributionId; + ], $contributionParams); + return $this->callAPISuccess('Contribution', 'create', $contributionParams)['id']; } } diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index 3c846af07b..010999d597 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -795,8 +795,6 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { 'end_date' => 'date_field', 'custom_field_name' => 'custom_' . $customDateField['id'], ]; - - $this->_setUp(); } /** @@ -805,12 +803,10 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { * This method is called after a test is executed. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \API_Exception */ public function tearDown(): void { - parent::tearDown(); - $this->mut->clearMessages(); - $this->mut->stop(); - unset($this->mut); $this->quickCleanup([ 'civicrm_action_schedule', 'civicrm_action_log', @@ -820,7 +816,8 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { 'civicrm_event', 'civicrm_email', ], TRUE); - $this->_tearDown(); + $this->deleteTestObjects(); + parent::tearDown(); } /** @@ -1742,7 +1739,6 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { */ public function testContactCreatedNoAnniversary(): void { $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']); - $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id']; $this->createScheduleFromFixtures('sched_contact_created_yesterday'); $this->assertCronRuns([ [ @@ -2208,26 +2204,7 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { * * (DAO_Name => array(int)) List of items to garbage-collect during tearDown */ - private $_testObjects; - - /** - * Sets up the fixture, for example, opens a network connection. - * - * This method is called before a test is executed. - */ - protected function _setUp(): void { - $this->_testObjects = []; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * - * This method is called after a test is executed. - */ - protected function _tearDown(): void { - parent::tearDown(); - $this->deleteTestObjects(); - } + private $_testObjects = []; /** * This is a wrapper for CRM_Core_DAO::createTestObject which tracks diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index de13fa74ab..725a7294b7 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -494,6 +494,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * * @throws \CiviCRM_API3_Exception * @throws \CRM_Core_Exception + * @throws \API_Exception */ protected function tearDown(): void { $this->_apiversion = 3; @@ -526,6 +527,10 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { $this->unsetExtensionSystem(); $this->assertEquals([], CRM_Core_DAO::$_nullArray); $this->assertEquals(NULL, CRM_Core_DAO::$_nullObject); + // Ensure the destruct runs by unsetting it. Also, unsetting + // classes frees memory as they are not otherwise unset until the + // very end. + unset($this->mut); } /**