Merge pull request #23603 from eileenmcnaughton/rel2
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Page / AjaxTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Contribute_Page_AjaxTest
14 * @group headless
15 */
16 class CRM_Contribute_Page_AjaxTest extends CiviUnitTestCase {
17
18 /**
19 * Setup for test.
20 */
21 public function setUp(): void {
22 parent::setUp();
23 $this->_params = [
24 'page' => 1,
25 'rp' => 50,
26 'offset' => 0,
27 'rowCount' => 50,
28 'sort' => NULL,
29 ];
30 $softContactParams = [
31 'first_name' => 'soft',
32 'last_name' => 'Contact',
33 ];
34 $this->ids['Contact']['SoftCredit'] = $this->individualCreate($softContactParams);
35
36 // Create three sample contacts.
37 foreach ([0, 1, 2] as $seq) {
38 $this->individualCreate([], $seq);
39 }
40 }
41
42 /**
43 * Cleanup after test.
44 */
45 public function tearDown(): void {
46 $this->quickCleanUpFinancialEntities();
47 parent::tearDown();
48 }
49
50 /**
51 * Test retrieve Soft Contribution through AJAX.
52 */
53 public function testGetSoftContributionSelector(): void {
54 $softCreditList = $amountSortedList = $softTypeSortedList = [];
55 $this->createThreeSoftCredits();
56
57 $_GET = array_merge($this->_params,
58 [
59 'cid' => $this->ids['Contact']['SoftCredit'],
60 'context' => 'contribution',
61 ]
62 );
63 try {
64 CRM_Contribute_Page_AJAX::getSoftContributionRows();
65 }
66 catch (CRM_Core_Exception_PrematureExitException $e) {
67 $softCreditList = $e->errorData;
68 }
69
70 $_GET['columns'] = [['data' => 'amount'], ['data' => 'sct_label']];
71 // get the results in descending order
72 $_GET['order'] = [
73 '0' => [
74 'column' => 0,
75 'dir' => 'desc',
76 ],
77 ];
78 try {
79 CRM_Contribute_Page_AJAX::getSoftContributionRows();
80 }
81 catch (CRM_Core_Exception_PrematureExitException $e) {
82 $amountSortedList = $e->errorData;
83 }
84
85 $this->assertEquals(3, $softCreditList['recordsTotal']);
86 $this->assertEquals(3, $amountSortedList['recordsTotal']);
87
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']);
91
92 // sort with soft credit types
93 $_GET['order'][0]['column'] = 1;
94 try {
95 CRM_Contribute_Page_AJAX::getSoftContributionRows();
96 }
97 catch (CRM_Core_Exception_PrematureExitException $e) {
98 $softTypeSortedList = $e->errorData;
99 }
100
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']);
104 }
105
106 /**
107 * Test retrieve Soft Contribution For Membership.
108 */
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,
118 'status_id' => 1,
119 'total_amount' => 100,
120 'receive_date' => '2018-06-08',
121 'soft_credit' => [
122 'soft_credit_type_id' => 11,
123 'contact_id' => $this->ids['Contact']['SoftCredit'],
124 ],
125 ];
126 $membershipID = $this->contactMembershipCreate($membershipParams);
127 $_GET = array_merge($this->_params,
128 [
129 'cid' => $this->ids['Contact']['SoftCredit'],
130 'context' => 'membership',
131 'entityID' => $membershipID,
132 ]
133 );
134
135 try {
136 CRM_Contribute_Page_AJAX::getSoftContributionRows();
137 }
138 catch (CRM_Core_Exception_PrematureExitException $e) {
139 $softCreditList = $e->errorData;
140 }
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']);
145 }
146
147 /**
148 * Create three soft credits against different contacts.
149 */
150 private function createThreeSoftCredits(): void {
151 $credits = [
152 [
153 'contact_id' => $this->ids['Contact']['individual_0'],
154 'soft_credit_type_id' => 3,
155 'amount' => 100,
156 ],
157 [
158 'contact_id' => $this->ids['Contact']['individual_1'],
159 'soft_credit_type_id' => 2,
160 'amount' => 600,
161 ],
162 [
163 'contact_id' => $this->ids['Contact']['individual_2'],
164 'soft_credit_type_id' => 5,
165 'amount' => 150,
166 ],
167 ];
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,
178 'soft_credit' => [
179 '1' => [
180 'contact_id' => $this->ids['Contact']['SoftCredit'],
181 'amount' => $credit['amount'],
182 'soft_credit_type_id' => $credit['soft_credit_type_id'],
183 ],
184 ],
185 ]);
186 }
187 }
188
189 }