X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FContribute%2FBAO%2FContributionRecurTest.php;h=464bd3a81f2412adf1e42a604c5990d1ff1c790d;hb=7d238e3572ee3d61e988dd7dd32545e9820e0753;hp=108a540892ef1d6f3b1721155c9f12968b92de12;hpb=0a7b86717573ed909202ca4d7fa248bbad203971;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php index 108a540892..464bd3a81f 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php @@ -214,6 +214,88 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']); } + /** + * Check whether template contribution is created based on the first contribution. + * + * There are three contributions created. Each of them with a different value at a custom field. + * The first contribution created should be copied as a template contribution. + * The other two should not be used as a template. + * + * Then we delete the template contribution and make sure a new one exists. + * At that time the second contribution should be used a template as that is the most recent one (according to the date). + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testCreateTemplateContributionFromFirstContributionTest(): void { + $custom_group = $this->customGroupCreate(['extends' => 'Contribution', 'name' => 'template']); + $custom_field = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field']); + + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params); + // Create a first test contrib + $date = new DateTime(); + $firstContrib = $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'payment_instrument_id' => 1, + 'currency' => 'USD', + 'contact_id' => $this->_params['contact_id'], + 'contribution_status_id' => 1, + 'receive_date' => $date->format('YmdHis'), + 'custom_' . $custom_field['id'] => 'First Contribution', + ]); + $date->modify('+2 days'); + $secondContrib = $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'payment_instrument_id' => 1, + 'currency' => 'USD', + 'contact_id' => $this->_params['contact_id'], + 'contribution_status_id' => 1, + 'receive_date' => $date->format('YmdHis'), + 'custom_' . $custom_field['id'] => 'Second and most recent Contribution', + ]); + + $date->modify('-1 week'); + $thirdContrib = $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'payment_instrument_id' => 1, + 'currency' => 'USD', + 'contact_id' => $this->_params['contact_id'], + 'contribution_status_id' => 1, + 'receive_date' => $date->format('YmdHis'), + 'custom_' . $custom_field['id'] => 'Third Contribution', + ]); + + // Make sure a template contribution exists. + $templateContributionId = CRM_Contribute_BAO_ContributionRecur::ensureTemplateContributionExists($contributionRecur['id']); + $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']); + $templateContribution = \Civi\Api4\Contribution::get(FALSE) + ->addSelect('*', 'custom.*') + ->addWhere('contribution_recur_id', '=', $contributionRecur['id']) + ->addWhere('is_template', '=', 1) + ->addWhere('is_test', '=', 0) + ->addOrderBy('id', 'DESC') + ->execute(); + + $this->assertNotEquals($firstContrib['id'], $fetchedTemplate['id']); + $this->assertNotEquals($secondContrib['id'], $fetchedTemplate['id']); + $this->assertNotEquals($thirdContrib['id'], $fetchedTemplate['id']); + $this->assertEquals($templateContributionId, $fetchedTemplate['id']); + $this->assertTrue($fetchedTemplate['is_template']); + $this->assertFalse($fetchedTemplate['is_test']); + $this->assertEquals(1, $templateContribution->count()); + $templateContribution = $templateContribution->first(); + $this->assertNotNull($templateContribution['template.field']); + $this->assertEquals('Second and most recent Contribution', $templateContribution['template.field']); + } + /** * Test that is_template contribution is used where available *