CRM-18465 add test to check correct setting of token variables.
authoreileen <emcnaughton@wikimedia.org>
Thu, 19 May 2016 00:24:52 +0000 (12:24 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 19 May 2016 00:25:38 +0000 (12:25 +1200)
CRM/Contribute/Form/Task/PDFLetterCommon.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php [new file with mode: 0644]

index bd87f08561efc4b5e35c437bab74b31d48abb3c1..4dcba8ef7e1c8ecac33f86c2205a907cb5f07da5 100644 (file)
@@ -49,8 +49,12 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
     // skip some contacts ?
     $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
     $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
-
-    list($contributions, $contacts) = self::buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator);
+    $contributionIDs = $form->getVar('_contributionIds');
+    if ($form->_includesSoftCredits) {
+      //@todo - comment on what is stored there
+      $contributionIDs = $form->getVar('_contributionContactIds');
+    }
+    list($contributions, $contacts) = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->_includesSoftCredits);
     $html = array();
     foreach ($contributions as $contributionId => $contribution) {
       $contact = &$contacts[$contribution['contact_id']];
@@ -204,23 +208,19 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
    * around contact_id of contribution_recur_id
    *
    * @param string $groupBy
-   * @param CRM_Contribute_Form_Task $form
+   * @param array $contributionIDs
    * @param array $returnProperties
    * @param bool $skipOnHold
    * @param bool $skipDeceased
    * @param array $messageToken
    * @param string $task
    * @param string $separator
+   * @param bool $isIncludeSoftCredits
    *
    * @return array
    */
-  public static function buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator) {
+  public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
     $contributions = $contacts = $notSent = array();
-    $contributionIDs = $form->getVar('_contributionIds');
-    if ($form->_includesSoftCredits) {
-      //@todo - comment on what is stored there
-      $contributionIDs = $form->getVar('_contributionContactIds');
-    }
     foreach ($contributionIDs as $item => $contributionId) {
       // get contribution information
       $contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId),
@@ -231,7 +231,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
       );
       $contribution = $contributions[$contributionId] = $contribution[$contributionId];
 
-      if ($form->_includesSoftCredits) {
+      if ($isIncludeSoftCredits) {
         //@todo find out why this happens & add comments
         list($contactID) = explode('-', $item);
         $contactID = (int) $contactID;
diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
new file mode 100644 (file)
index 0000000..297057b
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *  Test APIv3 civicrm_contribute_* functions
+ *
+ * @package CiviCRM_APIv3
+ * @subpackage API_Contribution
+ * @group headless
+ */
+class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
+
+  /**
+   * Assume empty database with just civicrm_data.
+   */
+  protected $_individualId;
+
+  /**
+   * Clean up after each test.
+   */
+  public function tearDown() {
+    $this->quickCleanUpFinancialEntities();
+    CRM_Utils_Hook::singleton()->reset();
+  }
+
+  /**
+   * Test the buildContributionArray function.
+   */
+  public function testBuildContributionArray() {
+    $this->_individualId = $this->individualCreate();
+    $params = array('contact_id' => $this->_individualId, 'total_amount' => 6, 'financial_type_id' => 'Donation');
+    $contributionIDs = $returnProperties = $messageToken = array();
+    $result = $this->callAPISuccess('Contribution', 'create', $params);
+    $contributionIDs[] = $result['id'];
+    $result = $this->callAPISuccess('Contribution', 'create', $params);
+    $contributionIDs[] = $result['id'];
+    $this->hookClass->setHook('civicrm_tokenValues', array($this, 'hookTokenValues'));
+
+    list($contributions, $contacts) = CRM_Contribute_Form_Task_PDFLetterCommon::buildContributionArray('contact_id', $contributionIDs, $returnProperties, TRUE, TRUE, $messageToken, 'test', '**', FALSE);
+
+    $this->assertEquals('Anthony', $contacts[$this->_individualId]['first_name']);
+    $this->assertEquals('emo', $contacts[$this->_individualId]['favourite_emoticon']);
+    $this->assertEquals('Donation', $contributions[$result['id']]['financial_type']);
+  }
+
+  /**
+   * Implement token values hook.
+   *
+   * @param array $details
+   * @param array $contactIDs
+   * @param int $jobID
+   * @param array $tokens
+   * @param string $className
+   */
+  public function hookTokenValues(&$details, $contactIDs, $jobID, $tokens, $className) {
+    foreach ($details as $index => $detail) {
+      $details[$index]['favourite_emoticon'] = 'emo';
+    }
+  }
+
+}