(NFC) (dev/core#878) Simplify copyright header (tests/*)
[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 protected $_params = [];
19
20 public function setUp() {
21 parent::setUp();
22
23 $this->_fields = ['amount', 'sct_label'];
24
25 $this->_params = [
26 'page' => 1,
27 'rp' => 50,
28 'offset' => 0,
29 'rowCount' => 50,
30 'sort' => NULL,
31 'is_unit_test' => TRUE,
32 ];
33 $softContactParams = [
34 'first_name' => 'soft',
35 'last_name' => 'Contact',
36 ];
37 $this->_softContactId = $this->individualCreate($softContactParams);
38
39 //create three sample contacts
40 foreach ([0, 1, 2] as $seq) {
41 $this->_primaryContacts[] = $this->individualCreate([], $seq);
42 }
43 }
44
45 /**
46 * Test retrieve Soft Contribution through AJAX
47 */
48 public function testGetSoftContributionSelector() {
49 $softTypes = [3, 2, 5];
50 $amounts = ['100', '600', '150'];
51
52 // create sample soft contribution for contact
53 foreach ($this->_primaryContacts as $seq => $contactId) {
54 $this->callAPISuccess('Contribution', 'create', [
55 'contact_id' => $contactId,
56 'receive_date' => date('Ymd'),
57 'total_amount' => $amounts[$seq],
58 'financial_type_id' => 1,
59 'non_deductible_amount' => '10',
60 'contribution_status_id' => 1,
61 'soft_credit' => [
62 '1' => [
63 'contact_id' => $this->_softContactId,
64 'amount' => $amounts[$seq],
65 'soft_credit_type_id' => $softTypes[$seq],
66 ],
67 ],
68 ]);
69 }
70
71 $_GET = array_merge($this->_params,
72 [
73 'cid' => $this->_softContactId,
74 'context' => 'contribution',
75 ]
76 );
77 $softCreditList = CRM_Contribute_Page_AJAX::getSoftContributionRows();
78
79 foreach ($this->_fields as $columnName) {
80 $_GET['columns'][] = [
81 'data' => $columnName,
82 ];
83 }
84 // get the results in descending order
85 $_GET['order'] = [
86 '0' => [
87 'column' => 0,
88 'dir' => 'desc',
89 ],
90 ];
91 $amountSortedList = CRM_Contribute_Page_AJAX::getSoftContributionRows();
92
93 $this->assertEquals(3, $softCreditList['recordsTotal']);
94 $this->assertEquals(3, $amountSortedList['recordsTotal']);
95 rsort($amounts);
96 foreach ($amounts as $key => $amount) {
97 $amount = CRM_Utils_Money::format($amount, 'USD');
98 $this->assertEquals($amount, $amountSortedList['data'][$key]['amount']);
99 }
100
101 // sort with soft credit types
102 $_GET['order'][0]['column'] = 1;
103 foreach ($softTypes as $id) {
104 $softLabels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $id);
105 }
106 rsort($softLabels);
107 $softTypeSortedList = CRM_Contribute_Page_AJAX::getSoftContributionRows();
108 foreach ($softLabels as $key => $labels) {
109 $this->assertEquals($labels, $softTypeSortedList['data'][$key]['sct_label']);
110 }
111 }
112
113 /**
114 * Test retrieve Soft Contribution For Membership
115 */
116 public function testGetSoftContributionForMembership() {
117 //Check soft credit for membership
118 $memParams = [
119 'contribution_contact_id' => $this->_primaryContacts[0],
120 'contact_id' => $this->_softContactId,
121 'contribution_status_id' => 1,
122 'financial_type_id' => 2,
123 'status_id' => 1,
124 'total_amount' => 100,
125 'receive_date' => '2018-06-08',
126 'soft_credit' => [
127 'soft_credit_type_id' => 11,
128 'contact_id' => $this->_softContactId,
129 ],
130 ];
131 $_GET = array_merge($this->_params,
132 [
133 'cid' => $this->_softContactId,
134 'context' => 'membership',
135 'entityID' => $this->contactMembershipCreate($memParams),
136 ]
137 );
138
139 $softCreditList = CRM_Contribute_Page_AJAX::getSoftContributionRows();
140 $this->assertEquals(1, $softCreditList['recordsTotal']);
141 $this->assertEquals('Gift', $softCreditList['data'][0]['sct_label']);
142 $this->assertEquals('$ 100.00', $softCreditList['data'][0]['amount']);
143 $this->assertEquals('Member Dues', $softCreditList['data'][0]['financial_type']);
144 }
145
146 }