[nfc] Add Unit test for group by in pdf letters.
authoreileen <emcnaughton@wikimedia.org>
Sat, 13 May 2017 08:49:56 +0000 (20:49 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 28 Jul 2017 12:26:27 +0000 (00:26 +1200)
In addition to the unit test there is a minor change to not do the early return before assigning variables
if smarty is not set. This doesn't really have any performance advantage, as they are already calculated,
blocks the unit test and also blocks using these variables from outside extensions

CRM/Contribute/Form/Task/PDFLetterCommon.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php

index 7d2cd8bc300bbe10bd2a351251b6229e3c98e431..c26b46cd6354d3e4d959cc8f2b8dd70460a5d595 100644 (file)
@@ -334,9 +334,6 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
    * @param int $groupByID
    */
   public static function assignCombinedContributionValues($contact, $contributions, $groupBy, $groupByID) {
-    if (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY) {
-      return;
-    }
     CRM_Core_Smarty::singleton()->assign('contact_aggregate', $contact['contact_aggregate']);
     CRM_Core_Smarty::singleton()
       ->assign('contributions', array_intersect_key($contributions, $contact['contribution_ids'][$groupBy][$groupByID]));
index 4e7fccf31ed464e143e6bfbb76df33ed85d2e694..ca0104d0e8cecc5ecb810032ca384d8203afc5b2 100644 (file)
@@ -175,4 +175,179 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test assignment of variables when using the group by function.
+   *
+   * We are looking to see that the contribution aggregate and contributions arrays reflect the most
+   * recent contact rather than a total aggregate, since we are using group by.
+   */
+  public function testPostProcessGroupByContact() {
+    $this->hookClass->setHook('civicrm_tokenValues', array($this, 'hook_aggregateTokenValues'));
+    $this->hookClass->setHook('civicrm_tokens', array($this, 'hook_tokens'));
+    $this->mut = new CiviMailUtils($this, TRUE);
+    $this->_individualId = $this->individualCreate();
+    $this->_individualId2 = $this->individualCreate();
+    $htmlMessage = "{aggregate.rendered_token}";
+    $formValues = array(
+      'is_unit_test' => TRUE,
+      'group_by' => 'contact_id',
+      'html_message' => $htmlMessage,
+      'email_options' => 'both',
+      'subject' => 'Testy test test',
+    );
+
+    $contributionIDs = array();
+    $contribution = $this->callAPISuccess('Contribution', 'create', array(
+      'contact_id' => $this->_individualId,
+      'total_amount' => 100,
+      'financial_type_id' => 'Donation',
+    ));
+    $contributionIDs[] = $contribution['id'];
+    $contribution = $this->callAPISuccess('Contribution', 'create', array(
+      'contact_id' => $this->_individualId2,
+      'total_amount' => 10,
+      'financial_type_id' => 'Donation',
+    ));
+    $contributionIDs[] = $contribution['id'];
+
+    $contribution = $this->callAPISuccess('Contribution', 'create', array(
+      'contact_id' => $this->_individualId2,
+      'total_amount' => 1,
+      'financial_type_id' => 'Donation',
+    ));
+    $contributionIDs[] = $contribution['id'];
+
+    $form = new CRM_Contribute_Form_Task_PDFLetter();
+    $form->setContributionIds($contributionIDs);
+
+    $html = CRM_Contribute_Form_Task_PDFLetterCommon::postProcess($form, $formValues);
+    $this->assertEquals("<table border='1' cellpadding='2' cellspacing='0' class='table'>
+  <tbody>
+  <tr>
+    <th>Date</th>
+    <th>Amount</th>
+    <th>Financial Type</th>
+    <th>Source</th>
+  </tr>
+  <!--
+   -->
+  <tr>
+    <td></td>
+    <td>$ 100.00</td>
+    <td></td>
+    <td></td>
+  </tr>
+  <!--
+    -->
+  <tr>
+    <td><strong>Total</strong></td>
+    <td><strong>$ 100.00</strong></td>
+    <td></td>
+    <td></td>
+  </tr>
+  </tbody>
+</table>", $html[1]);
+    $this->assertEquals("<table border='1' cellpadding='2' cellspacing='0' class='table'>
+  <tbody>
+  <tr>
+    <th>Date</th>
+    <th>Amount</th>
+    <th>Financial Type</th>
+    <th>Source</th>
+  </tr>
+  <!--
+    -->
+  <tr>
+    <td></td>
+    <td>$ 10.00</td>
+    <td></td>
+    <td></td>
+  </tr>
+  <!--
+     -->
+  <tr>
+    <td></td>
+    <td>$ 1.00</td>
+    <td></td>
+    <td></td>
+  </tr>
+  <!--
+  -->
+  <tr>
+    <td><strong>Total</strong></td>
+    <td><strong>$ 11.00</strong></td>
+    <td></td>
+    <td></td>
+  </tr>
+  </tbody>
+</table>", $html[2]);
+
+  }
+
+  /**
+   * Implements civicrm_tokens().
+   */
+  function hook_tokens(&$tokens) {
+    $tokens['aggregate'] = array('rendered_token' => 'rendered_token');
+  }
+
+  /**
+   * Get the html message.
+   *
+   * @return string
+   */
+  public function getHtmlMessage() {
+    return '{assign var=\'contact_aggregate\' value=0}
+<table border=\'1\' cellpadding=\'2\' cellspacing=\'0\' class=\'table\'>
+  <tbody>
+  <tr>
+    <th>Date</th>
+    <th>Amount</th>
+    <th>Financial Type</th>
+    <th>Source</th>
+  </tr>
+  <!--
+{foreach from=$contributions item=contribution}
+ {if $contribution.contact_id == $messageContactID}
+ {assign var=\'date\' value=$contribution.receive_date|date_format:\'%d %B %Y\'}
+ {assign var=contact_aggregate
+value=$contact_aggregate+$contribution.total_amount}
+-->
+  <tr>
+    <td>{$date}</td>
+    <td>{$contribution.total_amount|crmMoney}</td>
+    <td>{$contribution.financial_type}</td>
+    <td>{$contribution.source}</td>
+  </tr>
+  <!--
+  {/if}
+{/foreach}
+-->
+  <tr>
+    <td><strong>Total</strong></td>
+    <td><strong>{$contact_aggregate|crmMoney}</strong></td>
+    <td></td>
+    <td></td>
+  </tr>
+  </tbody>
+</table>';
+  }
+
+  /**
+   * Implements CiviCRM hook.
+   *
+   * @param array $values
+   * @param array $contactIDs
+   * @param null $job
+   * @param array $tokens
+   * @param null $context
+   */
+  function hook_aggregateTokenValues(&$values, $contactIDs, $job = NULL, $tokens = array(), $context = NULL) {
+    foreach ($contactIDs as $contactID) {
+      CRM_Core_Smarty::singleton()->assign('messageContactID', $contactID);
+      $values[$contactID]['aggregate.rendered_token'] = CRM_Core_Smarty::singleton()
+        ->fetch('string:' . $this->getHtmlMessage());
+    }
+  }
+
 }