Skip to content

Commit

Permalink
Add test to ensure that custom data on Contribution.create can be upd…
Browse files Browse the repository at this point in the history
…ated by hook
  • Loading branch information
mattwire committed Jan 28, 2022
1 parent 1671a4f commit b0ccbba
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public function testGetTemplateContributionMatchTest(): void {
public function testCreateTemplateContributionFromFirstContributionTest(): void {
$custom_group = $this->customGroupCreate(['extends' => 'Contribution', 'name' => 'template']);
$custom_field = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field']);
$custom_field2 = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field2', 'label' => 'Field 2']);

$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
// Create a first test contrib
Expand All @@ -246,6 +247,7 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'First Contribution',
'custom_' . $custom_field2['id'] => 'First Contribution custom field 2',
]);
$date->modify('+2 days');
$secondContrib = $this->callAPISuccess('Contribution', 'create', [
Expand All @@ -258,6 +260,7 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'Second and most recent Contribution',
'custom_' . $custom_field2['id'] => 'Second and most recent Contribution field 2',
]);

$date->modify('-1 week');
Expand All @@ -271,8 +274,13 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
'contribution_status_id' => 1,
'receive_date' => $date->format('YmdHis'),
'custom_' . $custom_field['id'] => 'Third Contribution',
'custom_' . $custom_field2['id'] => 'Third Contribution field 2',
]);

// Register "contribution create" hook
$this->hookClass->setHook('civicrm_post', array($this, 'implementHookPost'));
\Civi::$statics['testCreateTemplateContributionFromFirstContributionTest']['custom_field_id'] = $custom_field['id'];

// Make sure a template contribution exists.
$templateContributionId = CRM_Contribute_BAO_ContributionRecur::ensureTemplateContributionExists($contributionRecur['id']);
$fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
Expand All @@ -294,10 +302,25 @@ public function testCreateTemplateContributionFromFirstContributionTest(): void
$templateContribution = $templateContribution->first();
$this->assertNotNull($templateContribution['template.field']);
$this->assertEquals('Second and most recent Contribution', $templateContribution['template.field']);
$this->assertEquals('Template contribution custom data inserted by hook', $templateContribution['template.field2']);
$this->callAPISuccess('CustomField', 'delete', ['id' => $custom_field['id']]);
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $custom_group['id']]);
}

public function implementHookPost($op, $objectName, $objectId, &$objectRef) {
if ($objectName !== 'Contribution') {
return;
}
if ($op !== 'create') {
return;
}

// Simulate an extension updating the custom data on the new contribution
$contributionParams['entity_id'] = $objectId;
$contributionParams['custom_2'] = 'Template contribution custom data inserted by hook';
civicrm_api3('CustomValue', 'create', $contributionParams);
}

/**
* Test that is_template contribution is used where available
*
Expand Down

0 comments on commit b0ccbba

Please sign in to comment.