3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | Use of this source code is governed by the AGPL license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Contribute_Page_AjaxTest
16 class CRM_Contribute_Page_AjaxTest
extends CiviUnitTestCase
{
21 public function setUp(): void
{
30 $softContactParams = [
31 'first_name' => 'soft',
32 'last_name' => 'Contact',
34 $this->ids
['Contact']['SoftCredit'] = $this->individualCreate($softContactParams);
36 // Create three sample contacts.
37 foreach ([0, 1, 2] as $seq) {
38 $this->individualCreate([], $seq);
45 public function tearDown(): void
{
46 $this->quickCleanUpFinancialEntities();
51 * Test retrieve Soft Contribution through AJAX.
53 public function testGetSoftContributionSelector(): void
{
54 $softCreditList = $amountSortedList = $softTypeSortedList = [];
55 $this->createThreeSoftCredits();
57 $_GET = array_merge($this->_params
,
59 'cid' => $this->ids
['Contact']['SoftCredit'],
60 'context' => 'contribution',
64 CRM_Contribute_Page_AJAX
::getSoftContributionRows();
66 catch (CRM_Core_Exception_PrematureExitException
$e) {
67 $softCreditList = $e->errorData
;
70 $_GET['columns'] = [['data' => 'amount'], ['data' => 'sct_label']];
71 // get the results in descending order
79 CRM_Contribute_Page_AJAX
::getSoftContributionRows();
81 catch (CRM_Core_Exception_PrematureExitException
$e) {
82 $amountSortedList = $e->errorData
;
85 $this->assertEquals(3, $softCreditList['recordsTotal']);
86 $this->assertEquals(3, $amountSortedList['recordsTotal']);
88 $this->assertEquals('$600.00', $amountSortedList['data'][0]['amount']);
89 $this->assertEquals('$150.00', $amountSortedList['data'][1]['amount']);
90 $this->assertEquals('$100.00', $amountSortedList['data'][2]['amount']);
92 // sort with soft credit types
93 $_GET['order'][0]['column'] = 1;
95 CRM_Contribute_Page_AJAX
::getSoftContributionRows();
97 catch (CRM_Core_Exception_PrematureExitException
$e) {
98 $softTypeSortedList = $e->errorData
;
101 $this->assertEquals('Workplace Giving', $softTypeSortedList['data'][0]['sct_label']);
102 $this->assertEquals('Solicited', $softTypeSortedList['data'][1]['sct_label']);
103 $this->assertEquals('In Memory of', $softTypeSortedList['data'][2]['sct_label']);
107 * Test retrieve Soft Contribution For Membership.
109 public function testGetSoftContributionForMembership(): void
{
110 $softCreditList = [];
111 $this->createThreeSoftCredits();
112 // Check soft credit for membership.
113 $membershipParams = [
114 'contribution_contact_id' => $this->ids
['Contact']['individual_0'],
115 'contact_id' => $this->ids
['Contact']['SoftCredit'],
116 'contribution_status_id' => 1,
117 'financial_type_id' => 2,
119 'total_amount' => 100,
120 'receive_date' => '2018-06-08',
122 'soft_credit_type_id' => 11,
123 'contact_id' => $this->ids
['Contact']['SoftCredit'],
126 $membershipID = $this->contactMembershipCreate($membershipParams);
127 $_GET = array_merge($this->_params
,
129 'cid' => $this->ids
['Contact']['SoftCredit'],
130 'context' => 'membership',
131 'entityID' => $membershipID,
136 CRM_Contribute_Page_AJAX
::getSoftContributionRows();
138 catch (CRM_Core_Exception_PrematureExitException
$e) {
139 $softCreditList = $e->errorData
;
141 $this->assertEquals(1, $softCreditList['recordsTotal']);
142 $this->assertEquals('Gift', $softCreditList['data'][0]['sct_label']);
143 $this->assertEquals('$100.00', $softCreditList['data'][0]['amount']);
144 $this->assertEquals('Member Dues', $softCreditList['data'][0]['financial_type']);
148 * Create three soft credits against different contacts.
150 private function createThreeSoftCredits(): void
{
153 'contact_id' => $this->ids
['Contact']['individual_0'],
154 'soft_credit_type_id' => 3,
158 'contact_id' => $this->ids
['Contact']['individual_1'],
159 'soft_credit_type_id' => 2,
163 'contact_id' => $this->ids
['Contact']['individual_2'],
164 'soft_credit_type_id' => 5,
168 // Create sample soft contribution for contact.
169 foreach ($credits as $index => $credit) {
170 $this->callAPISuccess('Contribution', 'create', [
171 'contact_id' => $credit['contact_id'],
172 // The assumption is the last to be created will have a later time.
173 'receive_date' => ' + ' . $index . ' minutes',
174 'total_amount' => $credit['amount'],
175 'financial_type_id' => 1,
176 'non_deductible_amount' => '10',
177 'contribution_status_id' => 1,
180 'contact_id' => $this->ids
['Contact']['SoftCredit'],
181 'amount' => $credit['amount'],
182 'soft_credit_type_id' => $credit['soft_credit_type_id'],