Skip to content

Commit

Permalink
Merge pull request #13406 from eileenmcnaughton/api_getsingle
Browse files Browse the repository at this point in the history
Fix bug where getsingle calls chained actions twice
  • Loading branch information
seamuslee001 authored Jan 5, 2019
2 parents 775ddeb + d6f190f commit 2a8edd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Civi/API/Provider/MagicFunctionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ public function invoke($apiRequest) {
// Unlike normal API implementations, generic implementations require explicit
// knowledge of the entity and action (as well as $params). Bundle up these bits
// into a convenient data structure.
if ($apiRequest['action'] === 'getsingle') {
// strip any api nested parts here as otherwise chaining may happen twice
// see https://lab.civicrm.org/dev/core/issues/643
// testCreateBAODefaults fails without this.
foreach ($apiRequest['params'] as $key => $param) {
if ($key !== 'api.has_parent' && substr($key, 0, 4) === 'api.' || substr($key, 0, 4) === 'api_') {
unset($apiRequest['params'][$key]);
}
}
}
$result = $function($apiRequest);

}
elseif ($apiRequest['function'] && !$apiRequest['is_generic']) {
$result = $function($apiRequest['params']);
Expand Down
2 changes: 1 addition & 1 deletion api/v3/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function civicrm_api3_contribution_delete($params) {
return civicrm_api3_create_success(array($contributionID => 1));
}
else {
return civicrm_api3_create_error('Could not delete contribution');
throw new API_Exception('Could not delete contribution');
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,19 @@ public function testCreateBAODefaults() {
));
$this->assertEquals(1, $contribution['contribution_status_id']);
$this->assertEquals('Check', $contribution['payment_instrument']);
$this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
}

/**
* Test that getsingle can be chained with delete.
*/
public function testDeleteChainedGetSingle() {
$contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
$contribution = $this->callAPISuccess('contribution', 'getsingle', array(
'id' => $contribution['id'],
'api.contribution.delete' => 1,
));
$this->callAPISuccessGetCount('Contribution', ['id' => $contribution['id']], 0);
}

/**
Expand Down

0 comments on commit 2a8edd9

Please sign in to comment.