dev/core#2568 Enotice fix + test
authoreileen <emcnaughton@wikimedia.org>
Thu, 29 Apr 2021 03:44:01 +0000 (15:44 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 29 Apr 2021 06:07:59 +0000 (18:07 +1200)
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
CRM/Contribute/Form/Task.php
CRM/Contribute/Form/Task/PDFLetter.php
CRM/Contribute/Form/Task/Status.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php

index 7e030b8f38c23390a69a62972e007b378e541594..41c2b3a1cd6618a5ff3b08cd7db0aa5efe4d3379 100644 (file)
@@ -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);
index e9b72c8c923fb45a9c603d789e69b22bcac1b1e0..b15aa0e23894ccbd2fe93806d95fb1deae5f5cd9 100644 (file)
@@ -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;
   }
 
   /**
index b2cf7568057e64d06fbad21981d1ddb74b3aaf15..2e25054fcb72ad4f1b7ad96d8904e84727c82b76 100644 (file)
@@ -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);
index 496ca718dd4c15c1dac8e79af86568fc58947a77..6a71ef6bfc590eb8a7a7546d0b7076287bbf5ff8 100644 (file)
@@ -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,
index cd7495b0ece8a76ddeba58036c7581c192859d0e..b6ac0faef90d184d4c04c2c232e9dc4e7e142ba9 100644 (file)
@@ -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;
+  }
+
 }
index 4b2ce0c1dfaf2006e4488d146a659560765da639..48b86b3cd777ab875204a740888cac0f940d52de 100644 (file)
@@ -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();