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