Skip to content

Commit

Permalink
dev/core#2568 Enotice fix + test
Browse files Browse the repository at this point in the history
Same as civicrm#20178 but with a test
(and a couple of changes to support that)
  • Loading branch information
eileenmcnaughton committed Apr 29, 2021
1 parent ca4f796 commit 9da5951
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5296,7 +5296,7 @@ public static function getContributionTokenValues($id, $messageToken) {
}
$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);
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contribute/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public static function preProcessCommon(&$form): void {
*
* @param array $contributionIds
*/
public function setContributionIds($contributionIds) {
$this->_contributionIds = $contributionIds;
public function setContributionIds(array $contributionIds): void {
$this->ids = $contributionIds;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public function buildQuickForm() {

/**
* Process the form after the input has been submitted and validated.
*
* @throws \CRM_Core_Exception
*/
public function postProcess() {
$formValues = $this->controller->exportValues($this->getName());
Expand Down Expand Up @@ -174,12 +176,12 @@ public function postProcess() {
}
// 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();
Expand Down Expand Up @@ -239,7 +241,7 @@ public function postProcess() {

//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);
Expand Down
4 changes: 3 additions & 1 deletion CRM/Contribute/Form/Task/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public function preProcess() {

/**
* 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,
Expand Down
51 changes: 42 additions & 9 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ public function hookTokenValues(&$details, $contactIDs, $jobID, $tokens, $classN
* @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,
Expand All @@ -202,13 +201,7 @@ public function testPostProcess(): void {
],
];

$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]);
Expand All @@ -231,6 +224,30 @@ public function testPostProcess(): void {
}
}

/**
* 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.
*
Expand Down Expand Up @@ -515,4 +532,20 @@ protected function setSearchSelection(array $entities, CRM_Core_Form $form): voi
}
}

/**
* @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;
}

}
6 changes: 5 additions & 1 deletion tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public function tearDown(): void {

/**
* 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();

Expand Down

0 comments on commit 9da5951

Please sign in to comment.