Skip to content

Commit

Permalink
Merge pull request #21167 from greenpeace-cee/fix-contribution-activi…
Browse files Browse the repository at this point in the history
…ty-campaign-propagation

dev/core#2758 - Fix contribution activity campaign propagation
  • Loading branch information
demeritcowboy authored Aug 17, 2021
2 parents cf766c7 + b89a5f6 commit 613f5b0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,17 +538,17 @@ public static function create(&$params) {
['activity_type_id:name', '=', 'Contribution'],
])->execute()->first();

$campaignParams = isset($params['campaign_id']) ? ['campaign_id' => ($params['campaign_id'] ?? NULL)] : [];
$activityParams = array_merge([
$activityParams = [
'activity_type_id:name' => 'Contribution',
'source_record_id' => $contribution->id,
'activity_date_time' => $contribution->receive_date,
'is_test' => (bool) $contribution->is_test,
'status_id:name' => $isCompleted ? 'Completed' : 'Scheduled',
'skipRecentView' => TRUE,
'subject' => CRM_Activity_BAO_Activity::getActivitySubject($contribution),
'campaign_id' => $contribution->campaign_id,
'id' => $existingActivity['id'] ?? NULL,
], $campaignParams);
];
if (!$activityParams['id']) {
$activityParams['source_contact_id'] = (int) ($params['source_contact_id'] ?? (CRM_Core_Session::getLoggedInContactID() ?: $contribution->contact_id));
$activityParams['target_contact_id'] = ($activityParams['source_contact_id'] === (int) $contribution->contact_id) ? [] : [$contribution->contact_id];
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/
use Civi\Api4\Activity;
use Civi\Api4\Contribution;
use Civi\Api4\PledgePayment;

/**
Expand Down Expand Up @@ -1801,4 +1802,54 @@ public function testUpdateActivityContactOnContributionContactChange(): void {
$this->assertEquals($activityContact['contact_id'], $contactId_2, 'Check target contact ID matches the second contact');
}

/**
* Test status updates triggering activity creation and value propagation
*
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testContributionStatusUpdateActivityPropagation() {
$contactId = $this->individualCreate();
$campaignId = $this->campaignCreate();
$contribution = Contribution::create()
->addValue('contact_id', $contactId)
->addValue('campaign_id', $campaignId)
->addValue('financial_type_id:name', 'Donation')
->addValue('total_amount', 50)
->addValue('contribution_status_id:name', 'Pending')
->execute()
->first();
$activityWhere = [
['source_record_id', '=', $contribution['id']],
['activity_type_id:name', '=', 'Contribution'],
];
$activity = Activity::get()->setWhere($activityWhere)->execute()->first();
$this->assertNull($activity, 'Should not create contribution activity for pending contribution');

Contribution::update()
->addWhere('id', '=', $contribution['id'])
->addValue('contribution_status_id:name', 'Completed')
->execute();

$activity = Activity::get()->setWhere($activityWhere)->execute()->first();
$this->assertEquals($campaignId, $activity['campaign_id'], 'Should have created contribution activity with campaign');

$newCampaignId = $this->campaignCreate();
Contribution::update()
->addWhere('id', '=', $contribution['id'])
->addValue('campaign_id', $newCampaignId)
->execute();

$activity = Activity::get()->setWhere($activityWhere)->execute()->first();
$this->assertEquals($newCampaignId, $activity['campaign_id'], 'Should have updated contribution activity to new campaign');

Contribution::update()
->addWhere('id', '=', $contribution['id'])
->addValue('campaign_id', NULL)
->execute();

$activity = Activity::get()->setWhere($activityWhere)->execute()->first();
$this->assertNull($activity['campaign_id'], 'Should have removed campaign from contribution activity');
}

}

0 comments on commit 613f5b0

Please sign in to comment.