From 9da5951329c9162fc4dda9cc28266ac26b833784 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 29 Apr 2021 15:44:01 +1200 Subject: [PATCH] dev/core#2568 Enotice fix + test Same as https://github.com/civicrm/civicrm-core/pull/20178 but with a test (and a couple of changes to support that) --- CRM/Contribute/BAO/Contribution.php | 2 +- CRM/Contribute/Form/Task.php | 4 +- CRM/Contribute/Form/Task/PDFLetter.php | 8 +-- CRM/Contribute/Form/Task/Status.php | 4 +- .../Form/Task/PDFLetterCommonTest.php | 51 +++++++++++++++---- .../CRM/Contribute/Form/Task/StatusTest.php | 6 ++- 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 7e030b8f38..41c2b3a1cd 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -5296,7 +5296,7 @@ LIMIT 1;"; } $result = civicrm_api3('Contribution', 'get', ['id' => $id]); // lab.c.o mail#46 - show labels, not values, for custom fields with option values. - if (!empty($messageToken)) { + if (!empty($messageToken['contribution'])) { foreach ($result['values'][$id] as $fieldName => $fieldValue) { if (strpos($fieldName, 'custom_') === 0 && array_search($fieldName, $messageToken['contribution']) !== FALSE) { $result['values'][$id][$fieldName] = CRM_Core_BAO_CustomField::displayValue($result['values'][$id][$fieldName], $fieldName); diff --git a/CRM/Contribute/Form/Task.php b/CRM/Contribute/Form/Task.php index e9b72c8c92..b15aa0e238 100644 --- a/CRM/Contribute/Form/Task.php +++ b/CRM/Contribute/Form/Task.php @@ -62,8 +62,8 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task { * * @param array $contributionIds */ - public function setContributionIds($contributionIds) { - $this->_contributionIds = $contributionIds; + public function setContributionIds(array $contributionIds): void { + $this->ids = $contributionIds; } /** diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index b2cf756805..2e25054fcb 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -129,6 +129,8 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { /** * Process the form after the input has been submitted and validated. + * + * @throws \CRM_Core_Exception */ public function postProcess() { $formValues = $this->controller->exportValues($this->getName()); @@ -174,12 +176,12 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { } // a placeholder in case the separator is common in the string - e.g ', ' $separator = '****~~~~'; - $groupBy = $formValues['group_by']; + $groupBy = $this->getSubmittedValue('group_by'); // skip some contacts ? $skipOnHold = $this->skipOnHold ?? FALSE; $skipDeceased = $this->skipDeceased ?? TRUE; - $contributionIDs = $this->getVar('_contributionIds'); + $contributionIDs = $this->getIDs(); if ($this->isQueryIncludesSoftCredits()) { $contributionIDs = []; $result = $this->getSearchQueryResults(); @@ -239,7 +241,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task { //CRM-19761 if (!empty($html)) { - $type = $formValues['document_type']; + $type = $this->getSubmittedValue('document_type'); if ($type === 'pdf') { CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues); diff --git a/CRM/Contribute/Form/Task/Status.php b/CRM/Contribute/Form/Task/Status.php index 496ca718dd..6a71ef6bfc 100644 --- a/CRM/Contribute/Form/Task/Status.php +++ b/CRM/Contribute/Form/Task/Status.php @@ -66,12 +66,14 @@ AND {$this->_componentClause}"; /** * Build the form object. + * + * @throws \CRM_Core_Exception */ public function buildQuickForm() { $this->add('checkbox', 'is_email_receipt', ts('Send e-mail receipt')); $this->setDefaults(['is_email_receipt' => 1]); - $contribIDs = implode(',', $this->_contributionIds); + $contribIDs = implode(',', $this->getIDs()); $query = " SELECT c.id as contact_id, co.id as contribution_id, diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index cd7495b0ec..b6ac0faef9 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -190,8 +190,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testPostProcess(): void { - $this->createLoggedInUser(); - $this->_individualId = $this->individualCreate(); + $this->createLoggedInUser();; foreach (['docx', 'odt'] as $docType) { $formValues = [ 'is_unit_test' => TRUE, @@ -202,13 +201,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { ], ]; - $contributionParams = [ - 'contact_id' => $this->_individualId, - 'total_amount' => 100, - 'financial_type_id' => 'Donation', - ]; - $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); - $contributionId = $contribution['id']; + $contributionId = $this->createContribution(); /* @var $form CRM_Contribute_Form_Task_PDFLetter */ $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); $form->setContributionIds([$contributionId]); @@ -231,6 +224,30 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { } } + /** + * Test that no notice or errors occur if no contribution tokens are requested. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testNoContributionTokens(): void { + $this->createLoggedInUser(); + $formValues = [ + 'html_message' => '{contact.display_name}', + 'document_type' => 'pdf', + ]; + /* @var $form CRM_Contribute_Form_Task_PDFLetter */ + $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); + $form->setContributionIds([$this->createContribution()]); + try { + $form->postProcess(); + } + catch (CRM_Core_Exception_PrematureExitException $e) { + $html = $e->errorData['html']; + } + $this->assertStringContainsString('Mr. Anthony Anderson II', $html); + } + /** * Test assignment of variables when using the group by function. * @@ -515,4 +532,20 @@ value=$contact_aggregate+$contribution.total_amount} } } + /** + * @return mixed + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + protected function createContribution() { + $contributionParams = [ + 'contact_id' => $this->individualCreate(), + 'total_amount' => 100, + 'financial_type_id' => 'Donation', + ]; + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + $contributionId = $contribution['id']; + return $contributionId; + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php b/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php index 4b2ce0c1df..48b86b3cd7 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php @@ -26,8 +26,12 @@ class CRM_Contribute_Form_Task_StatusTest extends CiviUnitTestCase { /** * Test update pending contribution with sending a confirmation mail. + * + * @throws \CiviCRM_API3_Exception + * @throws \CRM_Core_Exception + * @throws \Exception */ - public function testUpdatePendingContributionWithSendingEmail() { + public function testUpdatePendingContributionWithSendingEmail(): void { $this->_individualId = $this->individualCreate(); $form = new CRM_Contribute_Form_Task_Status(); -- 2.25.1