From e9379b58ff321e2900972691fe921ff566724d98 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 19 May 2016 12:24:52 +1200 Subject: [PATCH] CRM-18465 add test to check correct setting of token variables. --- CRM/Contribute/Form/Task/PDFLetterCommon.php | 20 ++--- .../Form/Task/PDFLetterCommonTest.php | 85 +++++++++++++++++++ 2 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php diff --git a/CRM/Contribute/Form/Task/PDFLetterCommon.php b/CRM/Contribute/Form/Task/PDFLetterCommon.php index bd87f08561..4dcba8ef7e 100644 --- a/CRM/Contribute/Form/Task/PDFLetterCommon.php +++ b/CRM/Contribute/Form/Task/PDFLetterCommon.php @@ -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 index 0000000000..297057b91c --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -0,0 +1,85 @@ +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'; + } + } + +} -- 2.25.1