Merge pull request #22267 from eileenmcnaughton/cont_test2
[civicrm-core.git] / tests / phpunit / CRM / Contact / Page / View / UserDashBoardTest.php
CommitLineData
98867798 1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
98867798 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
98867798 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Test class for CRM_Contact_Page_View_UserDashBoard
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18class CRM_Contact_Page_View_UserDashBoardTest extends CiviUnitTestCase {
19
20 use CRMTraits_Page_PageTestTrait;
21
22 /**
23 * Contact ID of logged in user.
24 *
25 * @var int
26 */
27 protected $contactID;
28
e6507b1f 29 /**
30 * Contributions created for the test.
31 *
32 * @var array
33 */
34 protected $contributions = [];
35
98867798 36 /**
37 * Prepare for test
38 */
faba1457 39 public function setUp(): void {
98867798 40 parent::setUp();
41 $this->contactID = $this->createLoggedInUser();
42 $this->listenForPageContent();
43 }
44
d0df87f2 45 /**
46 * Clean up after each test.
47 */
594a9328 48 public function tearDown(): void {
d0df87f2 49 $this->quickCleanUpFinancialEntities();
50 $this->quickCleanup(['civicrm_uf_match']);
e0001b13 51 CRM_Utils_Hook::singleton()->reset();
e6507b1f 52 CRM_Core_Session::singleton()->reset();
53 CRM_Core_Smarty::singleton()->clearTemplateVars();
54 $this->callAPISuccess('Contact', 'delete', ['id' => $this->contactID]);
bc4d424d 55 parent::tearDown();
d0df87f2 56 }
57
98867798 58 /**
59 * Test the content of the dashboard.
60 */
61 public function testDashboardContentEmptyContact() {
62 $this->runUserDashboard();
63 $expectedStrings = [
64 'You are not currently subscribed to any Groups',
65 'There are no contributions on record for you.',
66 'There are no Pledges for your record.',
67 'You are not registered for any current or upcoming Events.',
68 'There are no memberships on record for you.',
69 'You do not have any active Personal Campaign pages.',
70 ];
71 $this->assertPageContains($expectedStrings);
72 }
73
74 /**
75 * Test the content of the dashboard.
76 */
78e2927c 77 public function testDashboardContentContributionsWithInvoicingEnabled(): void {
e6507b1f 78 $this->contributions[] = $this->contributionCreate([
79 'contact_id' => $this->contactID,
80 'receive_date' => '2018-11-21',
81 'receipt_date' => '2018-11-22',
82 ]);
83 $this->contributions[] = $this->contributionCreate([
84 'contact_id' => $this->contactID,
3c73a132
AH
85 'receive_date' => '2018-11-22',
86 'receipt_date' => '2018-11-23',
e6507b1f 87 'trxn_id' => '',
88 'invoice_id' => '',
89 ]);
10f949e6 90 $this->contributions[] = $this->contributionCreate([
91 'contact_id' => $this->contactID,
92 'receive_date' => '2018-11-24',
93 'receipt_date' => '2018-11-24',
94 'trxn_id' => '',
95 'invoice_id' => '',
96 'contribution_status_id' => 'Pending',
97 ]);
e6507b1f 98 $recur = $this->callAPISuccess('ContributionRecur', 'create', [
99 'contact_id' => $this->contactID,
100 'frequency_interval' => 1,
101 'amount' => 5,
102 ]);
103 $this->contributions[] = $this->contributionCreate([
104 'contact_id' => $this->contactID,
3c73a132 105 'receive_date' => '2018-11-20',
e6507b1f 106 'amount_level' => 'high',
107 'contribution_status_id' => 'Cancelled',
108 'invoice_id' => NULL,
109 'trxn_id' => NULL,
110 'contribution_recur_id' => $recur['id'],
111 ]);
112 $this->callAPISuccess('Setting', 'create', ['invoicing' => 1]);
10f949e6 113 $this->callAPISuccess('Setting', 'create', ['default_invoice_page' => $this->contributionPageCreate()['id']]);
98867798 114 $this->runUserDashboard();
115 $expectedStrings = [
116 'Your Contribution(s)',
dbfe939a 117 '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th><th></th>',
332848a7 118 '<td>Completed</td><td><a class="button no-popup nowrap"href="/index.php?q=civicrm/contribute/invoice&amp;reset=1&amp;id=1&amp;cid=' . $this->contactID . '"><i class="crm-i fa-download" aria-hidden="true"></i><span>Download Invoice</span></a></td></tr><tr id=\'rowid2\'',
10f949e6 119 'Pay Now',
98867798 120 ];
e6507b1f 121
98867798 122 $this->assertPageContains($expectedStrings);
3c73a132 123 $this->assertSmartyVariableArrayIncludes('contribute_rows', 1, [
e6507b1f 124 'contact_id' => $this->contactID,
125 'contribution_id' => '1',
126 'total_amount' => '100.00',
127 'financial_type' => 'Donation',
128 'contribution_source' => 'SSF',
129 'receive_date' => '2018-11-21 00:00:00',
130 'contribution_status' => 'Completed',
131 'currency' => 'USD',
8bfce657 132 'receipt_date' => '2018-11-22 00:00:00',
e6507b1f 133 ]);
134
98867798 135 }
136
137 /**
138 * Test the content of the dashboard.
ff6f993e 139 *
ff6f993e 140 * @throws \CiviCRM_API3_Exception
98867798 141 */
d6d93aa4 142 public function testDashboardContentContributions(): void {
98867798 143 $this->contributionCreate(['contact_id' => $this->contactID]);
e6507b1f 144 $this->contributions[] = civicrm_api3('Contribution', 'get', [
145 'contact_id' => $this->contactID,
146 'options' => ['limit' => 12, 'sort' => 'receive_date DESC'],
147 'sequential' => 1,
148 ])['values'];
98867798 149 $this->runUserDashboard();
150 $expectedStrings = [
151 'Your Contribution(s)',
dbfe939a 152 '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th>',
d6d93aa4 153 '<td>$100.00 </td><td>Donation</td>',
e6507b1f 154 '<td>Completed</td>',
98867798 155 ];
156 $this->assertPageContains($expectedStrings);
98867798 157 }
158
dbfe939a
JG
159 /**
160 * Test the presence of a "Pay Now" button on partial payments
161 *
162 * @throws \CRM_Core_Exception
163 * @throws \CiviCRM_API3_Exception
164 */
165 public function testDashboardPartialPayments() {
166 $contributionId = $this->contributionCreate([
167 'contact_id' => $this->contactID,
168 'contribution_status_id' => 'Pending',
169 'total_amount' => 25,
170 ]);
171 $result = civicrm_api3('Payment', 'create', [
172 'contribution_id' => $contributionId,
173 'total_amount' => 11,
174 'trxn_date' => "2021-05-11",
175 ]);
176 $this->contributions[] = civicrm_api3('Contribution', 'get', [
177 'contact_id' => $this->contactID,
178 'options' => ['limit' => 12, 'sort' => 'receive_date DESC'],
179 'sequential' => 1,
180 ])['values'];
181 $this->runUserDashboard();
182 $expectedStrings = [
183 'Your Contribution(s)',
184 '<table class="selector"><tr class="columnheader"><th>Total Amount</th><th>Financial Type</th><th>Received date</th><th>Receipt Sent</th><th>Balance</th><th>Status</th>',
d6d93aa4
EM
185 '<td>$25.00 </td><td>Donation</td>',
186 '<td>$14.00</td><td>Partially paid</td>',
dbfe939a
JG
187 'Pay Now',
188 ];
189 $this->assertPageContains($expectedStrings);
190 }
191
98867798 192 /**
193 * Run the user dashboard.
194 */
78e2927c 195 protected function runUserDashboard(): void {
e6507b1f 196 $_REQUEST = ['reset' => 1, 'id' => $this->contactID];
98867798 197 $dashboard = new CRM_Contact_Page_View_UserDashBoard();
e6507b1f 198 $dashboard->_contactId = $this->contactID;
6e492a9c 199 $dashboard->assign('formTpl', NULL);
88718e71 200 $dashboard->assign('action', CRM_Core_Action::VIEW);
98867798 201 $dashboard->run();
e6507b1f 202 $_REQUEST = [];
98867798 203 }
204
e58f9fcc
JG
205 /**
206 * Tests the event dashboard as a minimally permissioned user.
207 */
208 public function testEventDashboard() {
209 CRM_Core_Config::singleton()->userPermissionClass->permissions = [
210 'register for events',
211 'access Contact Dashboard',
212 ];
213 $event1id = $this->eventCreate()['id'];
214 $event2id = $this->eventCreate(['title' => 'Social Distancing Meetup Group'])['id'];
215 $params['contact_id'] = $this->contactID;
216 $params['event_id'] = $event1id;
217 $this->participantCreate($params);
218 $params['event_id'] = $event2id;
219 $this->participantCreate($params);
220 $this->runUserDashboard();
221 $expectedStrings = [
222 '<div class="header-dark">Your Event(s)</div>',
223 '<td class="crm-participant-event-id_1"><a href="/index.php?q=civicrm/event/info&amp;reset=1&amp;id=1&amp;context=dashboard">Annual CiviCRM meet</a></td>',
224 '<td class="crm-participant-event-id_2"><a href="/index.php?q=civicrm/event/info&amp;reset=1&amp;id=2&amp;context=dashboard">Social Distancing Meetup Group</a></td>',
225 ];
226 $this->assertPageContains($expectedStrings);
227 $this->individualCreate();
228 }
229
98867798 230}