From: Edsel Roque Lopez Date: Wed, 12 Apr 2017 21:36:40 +0000 (+0530) Subject: CRM-20359 Extended the use of contribution tokens to include custom fields using... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=39b231592ff925ec331ff9c0c2aa0e2495b615df;p=civicrm-core.git CRM-20359 Extended the use of contribution tokens to include custom fields using API (#10082) * CRM-20359 Extended the use of contribution tokens to include custom fields using API ---------------------------------------- * CRM-20359: Support contribution.custom_nn tokens for Thank You letters - print or email https://issues.civicrm.org/jira/browse/CRM-20359 * extended testBuildContributionArray unit test * Fix missing comma --- diff --git a/CRM/Contribute/Form/Task/PDFLetterCommon.php b/CRM/Contribute/Form/Task/PDFLetterCommon.php index 12410a3cc1..a010487a2a 100644 --- a/CRM/Contribute/Form/Task/PDFLetterCommon.php +++ b/CRM/Contribute/Form/Task/PDFLetterCommon.php @@ -247,13 +247,18 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF $contributions = $contacts = $notSent = array(); foreach ($contributionIDs as $item => $contributionId) { // get contribution information - $contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId), - $returnProperties, - NULL, - $messageToken, - $task - ); - $contribution = $contributions[$contributionId] = $contribution[$contributionId]; + + // basic return attributes needed, see below for there usage + $returnValues = array('contact_id', 'total_amount'); + if (!empty($messageToken['contribution'])) { + $returnValues = array_merge($messageToken['contribution'], $returnValues); + } + // retrieve contribution tokens listed in $returnProperties using Contribution.Get API + $contribution = civicrm_api3('Contribution', 'getsingle', array( + 'id' => $contributionId, + 'return' => $returnValues, + )); + $contributions[$contributionId] = $contribution; if ($isIncludeSoftCredits) { //@todo find out why this happens & add comments diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 5a51668cca..1353e26460 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1335,73 +1335,6 @@ class CRM_Utils_Token { return $details; } - /** - * Gives required details of contribuion in an indexed array format so we - * can iterate in a nice loop and do token evaluation - * - * @param array $contributionIDs - * @param array $returnProperties - * Of required properties. - * @param array $extraParams - * Extra params. - * @param array $tokens - * The list of tokens we've extracted from the content. - * @param string $className - * - * @return array - */ - public static function getContributionTokenDetails( - $contributionIDs, - $returnProperties = NULL, - $extraParams = NULL, - $tokens = array(), - $className = NULL - ) { - // @todo this function basically replicates calling - // civicrm_api3('contribution', 'get', array('id' => array('IN' => array()) - if (empty($contributionIDs)) { - // putting a fatal here so we can track if/when this happens - CRM_Core_Error::fatal(); - } - - $details = array(); - - // no apiQuery helper yet, so do a loop and find contribution by id - foreach ($contributionIDs as $contributionID) { - - $dao = new CRM_Contribute_DAO_Contribution(); - $dao->id = $contributionID; - - if ($dao->find(TRUE)) { - - $details[$dao->id] = array(); - CRM_Core_DAO::storeValues($dao, $details[$dao->id]); - - // do the necessary transformation - if (!empty($details[$dao->id]['payment_instrument_id'])) { - $piId = $details[$dao->id]['payment_instrument_id']; - $pis = CRM_Contribute_PseudoConstant::paymentInstrument(); - $details[$dao->id]['payment_instrument'] = $pis[$piId]; - } - if (!empty($details[$dao->id]['campaign_id'])) { - $campaignId = $details[$dao->id]['campaign_id']; - $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); - $details[$dao->id]['campaign'] = $campaigns[$campaignId]; - } - - if (!empty($details[$dao->id]['financial_type_id'])) { - $financialtypeId = $details[$dao->id]['financial_type_id']; - $ftis = CRM_Contribute_PseudoConstant::financialType(); - $details[$dao->id]['financial_type'] = $ftis[$financialtypeId]; - } - - // @todo call a hook to get token contribution details - } - } - - return $details; - } - /** * Get Membership Token Details. * @param array $membershipIDs diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index 7b6d81ccbe..372ad5e7cf 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -62,19 +62,55 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { */ public function testBuildContributionArray() { $this->_individualId = $this->individualCreate(); - $params = array('contact_id' => $this->_individualId, 'total_amount' => 6, 'financial_type_id' => 'Donation'); - $contributionIDs = $returnProperties = $messageToken = array(); + + $customGroup = $this->callAPISuccess('CustomGroup', 'create', array( + 'title' => 'Test Custom Set for Contribution', + 'extends' => 'Contribution', + 'is_active' => TRUE, + )); + $params = array( + 'custom_group_id' => $customGroup['id'], + 'label' => 'Text field', + 'html_type' => 'Text', + 'data_type' => 'String', + 'weight' => 1, + 'is_active' => 1, + ); + $customField = $this->callAPISuccess('CustomField', 'create', $params); + $customFieldKey = 'custom_' . $customField['id']; + + $params = array( + 'contact_id' => $this->_individualId, + 'total_amount' => 6, + 'financial_type_id' => 'Donation', + $customFieldKey => 'Text_' . substr(sha1(rand()), 0, 7), + ); + $contributionIDs = $returnProperties = 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')); + // assume that there are two token {contribution.financial_type} and + // {contribution.custom_N} in message content + $messageToken = array( + 'contribution' => array( + 'financial_type', + $customFieldKey, + ), + ); + 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']); + // CRM-20359: assert that contribution custom field token is rightfully replaced by its value + $this->assertEquals($params[$customFieldKey], $contributions[$result['id']][$customFieldKey]); + + $this->customFieldDelete($customField['id']); + $this->customGroupDelete($customGroup['id']); } /**