Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20247 Add test & ensure $is_recur is assigned to the message_template. #9966

Merged
merged 3 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,6 @@ public function loadRelatedObjects(&$input, &$ids, $loadAll = FALSE) {
* @param array $values
* Any values that may have already been compiled by calling process.
* This is augmented by values 'gathered' by gatherMessageValues
* @param bool $recur
* @param bool $returnMessageText
* Distinguishes between whether to send message or return.
* message text. We are working towards this function ALWAYS returning message text & calling
Expand All @@ -2426,7 +2425,7 @@ public function loadRelatedObjects(&$input, &$ids, $loadAll = FALSE) {
* messages
* @throws Exception
*/
public function composeMessageArray(&$input, &$ids, &$values, $recur = FALSE, $returnMessageText = TRUE) {
public function composeMessageArray(&$input, &$ids, &$values, $returnMessageText = TRUE) {
$this->loadRelatedObjects($input, $ids);

if (empty($this->_component)) {
Expand All @@ -2439,11 +2438,10 @@ public function composeMessageArray(&$input, &$ids, &$values, $recur = FALSE, $r
$values['receipt_date'] = $input['receipt_date'];
}

$template = CRM_Core_Smarty::singleton();
$this->_assignMessageVariablesToTemplate($values, $input, $template, $recur, $returnMessageText);
$template = $this->_assignMessageVariablesToTemplate($values, $input, $returnMessageText);
//what does recur 'mean here - to do with payment processor return functionality but
// what is the importance
if ($recur && !empty($this->_relatedObjects['paymentProcessor'])) {
if (!empty($this->contribution_recur_id) && !empty($this->_relatedObjects['paymentProcessor'])) {
$paymentObject = Civi\Payment\System::singleton()->getByProcessor($this->_relatedObjects['paymentProcessor']);

$entityID = $entity = NULL;
Expand Down Expand Up @@ -2552,7 +2550,7 @@ public function composeMessageArray(&$input, &$ids, &$values, $recur = FALSE, $r
$values['is_pay_later'] = 1;
}

if ($recur && $paymentObject) {
if (!empty($this->contribution_recur_id) && $paymentObject) {
$url = $paymentObject->subscriptionURL($membership->id, 'membership', 'cancel');
$template->assign('cancelSubscriptionUrl', $url);
$url = $paymentObject->subscriptionURL($membership->id, 'membership', 'billing');
Expand Down Expand Up @@ -2788,13 +2786,12 @@ public function _gatherMessageValues($input, &$values, $ids = array()) {
*
* @param $values
* @param $input
* @param CRM_Core_SMARTY $template
* @param bool $recur
* @param bool $returnMessageText
*
* @return mixed
*/
public function _assignMessageVariablesToTemplate(&$values, $input, &$template, $recur = FALSE, $returnMessageText = TRUE) {
public function _assignMessageVariablesToTemplate(&$values, $input, $returnMessageText = TRUE) {
$template = CRM_Core_Smarty::singleton();
$template->assign('first_name', $this->_relatedObjects['contact']->first_name);
$template->assign('last_name', $this->_relatedObjects['contact']->last_name);
$template->assign('displayName', $this->_relatedObjects['contact']->display_name);
Expand Down Expand Up @@ -2889,7 +2886,7 @@ public function _assignMessageVariablesToTemplate(&$values, $input, &$template,
)
);
$template->assign('is_monetary', 1);
$template->assign('is_recur', (bool) $recur);
$template->assign('is_recur', !empty($this->contribution_recur_id));
$template->assign('currency', $this->currency);
$template->assign('address', CRM_Utils_Address::format($input));
if (!empty($values['customGroup'])) {
Expand Down Expand Up @@ -4699,8 +4696,6 @@ public static function completeOrder(&$input, &$ids, $objects, $transaction, $re
* @param int $contributionID
* @param array $values
* Values related to objects that have already been loaded.
* @param bool $recur
* Is it part of a recurring contribution.
* @param bool $returnMessageText
* Should text be returned instead of sent. This.
* is because the function is also used to generate pdfs
Expand All @@ -4709,9 +4704,8 @@ public static function completeOrder(&$input, &$ids, $objects, $transaction, $re
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public static function sendMail(&$input, &$ids, $contributionID, &$values, $recur = FALSE,
public static function sendMail(&$input, &$ids, $contributionID, &$values,
$returnMessageText = FALSE) {
$input['is_recur'] = $recur;

$contribution = new CRM_Contribute_BAO_Contribution();
$contribution->id = $contributionID;
Expand All @@ -4723,7 +4717,7 @@ public static function sendMail(&$input, &$ids, $contributionID, &$values, $recu
if (!$returnMessageText) {
list($values['receipt_from_name'], $values['receipt_from_email']) = self::generateFromEmailAndName($input, $contribution);
}
$return = $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText);
$return = $contribution->composeMessageArray($input, $ids, $values, $returnMessageText);
// Contribution ID should really always be set. But ?
if (!$returnMessageText && (!isset($input['receipt_update']) || $input['receipt_update']) && empty($contribution->receipt_date)) {
civicrm_api3('Contribution', 'create', array('receipt_date' => 'now', 'id' => $contribution->id));
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function postProcess() {
$input['receipt_from_name'] = str_replace('"', '', $fromDetails[0]);
}

$mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, FALSE,
$mail = CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values,
$elements['createPdf']);

if ($mail['html']) {
Expand Down
6 changes: 3 additions & 3 deletions CRM/Core/Payment/BaseIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ public function getBillingID(&$ids) {
}

/**
* Send receipt from contribution.
*
* @deprecated
*
* @todo confirm this function is not being used by any payment processor outside core & remove.
*
* Note that the compose message part has been moved to contribution
* In general LoadObjects is called first to get the objects but the composeMessageArray function now calls it
*
Expand All @@ -494,7 +494,7 @@ public function getBillingID(&$ids) {
* @return array
*/
public function sendMail(&$input, &$ids, &$objects, &$values, $recur = FALSE, $returnMessageText = FALSE) {
return CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values, $recur,
return CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $objects['contribution']->id, $values,
$returnMessageText);
}

Expand Down
68 changes: 57 additions & 11 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ public function testCreateContributionWithSoftCreditDefaults() {
$this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
}

/**
* Test creating contribution with Soft Credit by passing in honor_contact_id.
*/
public function testCreateContributionWithHonoreeContact() {
$description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
$subfile = "ContributionCreateWithHonoreeContact";
Expand All @@ -839,7 +842,8 @@ public function testCreateContributionWithHonoreeContact() {
// Default soft credit amount = contribution.total_amount
// Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
$this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
$this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
$softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
$this->assertEquals('in_honor_of', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));

$this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
$this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
Expand Down Expand Up @@ -2424,16 +2428,8 @@ public function testCompleteTransactionWithReceiptDateSet() {
* CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
*/
public function testCompleteTransactionWithEmailReceiptInput() {
// Create a Contribution Page with is_email_receipt = TRUE
$contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
'receipt_from_name' => 'Mickey Mouse',
'receipt_from_email' => 'mickey@mouse.com',
'title' => "Test Contribution Page",
'financial_type_id' => 1,
'currency' => 'CAD',
'is_monetary' => TRUE,
'is_email_receipt' => TRUE,
));
$contributionPage = $this->createReceiptableContributionPage();

$this->_params['contribution_page_id'] = $contributionPage['id'];
$params = array_merge($this->_params, array('contribution_status_id' => 2));
$contribution = $this->callAPISuccess('contribution', 'create', $params);
Expand All @@ -2449,6 +2445,35 @@ public function testCompleteTransactionWithEmailReceiptInput() {
$this->assertEquals('', $receipt_date);
}

/**
* Test that $is_recur is assigned to the receipt.
*/
public function testCompleteTransactionForRecurring() {

$this->swapMessageTemplateForTestTemplate();
$recurring = $this->setUpRecurringContribution();
$contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));

$this->_params['contribution_page_id'] = $contributionPage['id'];
$this->_params['contribution_recur_id'] = $recurring['id'];

$contribution = $this->setUpForCompleteTransaction();

$this->callAPISuccess('contribution', 'completetransaction', array(
'id' => $contribution['id'],
'trxn_date' => date('2011-04-09'),
'trxn_id' => 'kazam',
'is_email_receipt' => 1,
));

$this->mut->checkMailLog(array(
'is_recur:::1',
'cancelSubscriptionUrl:::http://dummy.com',
));
$this->mut->stop();
$this->revertTemplateToReservedTemplate();
}

/**
* Complete the transaction using the template with all the possible.
*/
Expand Down Expand Up @@ -3490,4 +3515,25 @@ public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFro
$mut->stop();
}

/**
* Create a Contribution Page with is_email_receipt = TRUE.
*
* @param array $params
* Params to overwrite with.
*
* @return array|int
*/
protected function createReceiptableContributionPage($params = array()) {
$contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
'receipt_from_name' => 'Mickey Mouse',
'receipt_from_email' => 'mickey@mouse.com',
'title' => "Test Contribution Page",
'financial_type_id' => 1,
'currency' => 'CAD',
'is_monetary' => TRUE,
'is_email_receipt' => TRUE,
), $params));
return $contributionPage;
}

}