Remove instances of fatal
[civicrm-core.git] / tests / phpunit / CRM / Contribute / ActionMapping / ByTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Contribute_ActionMapping_ByTypeTest
14 * @group ActionSchedule
15 *
16 * This class tests various configurations of scheduled-reminders, with a focus on
17 * reminders for *contribution types*. It follows a design/pattern described in
18 * AbstractMappingTest.
19 *
20 * @see \Civi\ActionSchedule\AbstractMappingTest
21 * @group headless
22 */
23 class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\AbstractMappingTest {
24
25 /**
26 * Generate a list of test cases, where each is a distinct combination of
27 * data, schedule-rules, and schedule results.
28 *
29 * @return array
30 * - targetDate: string; eg "2015-02-01 00:00:01"
31 * - setupFuncs: string, space-separated list of setup functions
32 * - messages: array; each item is a message that's expected to be sent
33 * each message may include keys:
34 * - time: approximate time (give or take a few seconds)
35 * - recipients: array of emails
36 * - subject: regex
37 */
38 public function createTestCases() {
39 $cs = [];
40
41 // FIXME: CRM-19415: The right email content goes out, but it appears that the dates are incorrect.
42 // $cs[] = array(
43 // '2015-02-01 00:00:00',
44 // 'addAliceDues scheduleForAny startOnTime useHelloFirstName alsoRecipientBob',
45 // array(
46 // array(
47 // 'time' => '2015-02-01 00:00:00',
48 // 'to' => array('alice@example.org'),
49 // 'subject' => '/Hello, Alice.*via subject/',
50 // ),
51 // array(
52 // 'time' => '2015-02-01 00:00:00',
53 // 'to' => array('bob@example.org'),
54 // 'subject' => '/Hello, Bob.*via subject/',
55 // // It might make more sense to get Alice's details... but path of least resistance...
56 // ),
57 // ),
58 // );
59
60 $cs[] = [
61 '2015-02-01 00:00:00',
62 'addAliceDues scheduleForAny startOnTime useHelloFirstName limitToRecipientBob',
63 [],
64 ];
65
66 $cs[] = [
67 '2015-02-01 00:00:00',
68 'addAliceDues scheduleForAny startOnTime useHelloFirstName limitToRecipientAlice',
69 [
70 [
71 'time' => '2015-02-01 00:00:00',
72 'to' => ['alice@example.org'],
73 'subject' => '/Hello, Alice.*via subject/',
74 ],
75 ],
76 ];
77
78 $cs[] = [
79 '2015-02-01 00:00:00',
80 // 'addAliceDues addBobDonation scheduleForDues startOnTime useHelloFirstName',
81 'addAliceDues addBobDonation scheduleForDues startOnTime useHelloFirstNameStatus',
82 [
83 [
84 'time' => '2015-02-01 00:00:00',
85 'to' => ['alice@example.org'],
86 'subject' => '/Hello, Alice. @Completed.*via subject/',
87 ],
88 ],
89 ];
90
91 $cs[] = [
92 '2015-02-01 00:00:00',
93 'addAliceDues addBobDonation scheduleForAny startOnTime useHelloFirstName',
94 [
95 [
96 'time' => '2015-02-01 00:00:00',
97 'to' => ['alice@example.org'],
98 'subject' => '/Hello, Alice.*via subject/',
99 ],
100 [
101 'time' => '2015-02-01 00:00:00',
102 'to' => ['bob@example.org'],
103 'subject' => '/Hello, Bob.*via subject/',
104 ],
105 ],
106 ];
107
108 $cs[] = [
109 '2015-02-02 00:00:00',
110 'addAliceDues addBobDonation scheduleForDonation startWeekBefore repeatTwoWeeksAfter useHelloFirstName',
111 [
112 [
113 'time' => '2015-01-26 00:00:00',
114 'to' => ['bob@example.org'],
115 'subject' => '/Hello, Bob.*via subject/',
116 ],
117 [
118 'time' => '2015-02-02 00:00:00',
119 'to' => ['bob@example.org'],
120 'subject' => '/Hello, Bob.*via subject/',
121 ],
122 [
123 'time' => '2015-02-09 00:00:00',
124 'to' => ['bob@example.org'],
125 'subject' => '/Hello, Bob.*via subject/',
126 ],
127 [
128 'time' => '2015-02-16 00:00:00',
129 'to' => ['bob@example.org'],
130 'subject' => '/Hello, Bob.*via subject/',
131 ],
132 ],
133 ];
134
135 $cs[] = [
136 '2015-02-03 00:00:00',
137 'addAliceDues addBobDonation scheduleForSoftCreditor startWeekAfter useHelloFirstName',
138 [
139 [
140 'time' => '2015-02-10 00:00:00',
141 'to' => ['carol@example.org'],
142 'subject' => '/Hello, Carol.*via subject/',
143 ],
144 ],
145 ];
146
147 return $cs;
148 }
149
150 /**
151 * Create a contribution record for Alice with type "Member Dues".
152 */
153 public function addAliceDues() {
154 $this->callAPISuccess('Contribution', 'create', [
155 'contact_id' => $this->contacts['alice']['id'],
156 'receive_date' => date('Ymd', strtotime($this->targetDate)),
157 'total_amount' => '100',
158 'financial_type_id' => 1,
159 'non_deductible_amount' => '10',
160 'fee_amount' => '5',
161 'net_amount' => '95',
162 'source' => 'SSF',
163 'contribution_status_id' => 1,
164 'soft_credit' => [
165 '1' => [
166 'contact_id' => $this->contacts['carol']['id'],
167 'amount' => 50,
168 'soft_credit_type_id' => 3,
169 ],
170 ],
171 ]);
172 }
173
174 /**
175 * Create a contribution record for Bob with type "Donation".
176 */
177 public function addBobDonation() {
178 $this->callAPISuccess('Contribution', 'create', [
179 'contact_id' => $this->contacts['bob']['id'],
180 'receive_date' => date('Ymd', strtotime($this->targetDate)),
181 'total_amount' => '150',
182 'financial_type_id' => 2,
183 'non_deductible_amount' => '10',
184 'fee_amount' => '5',
185 'net_amount' => '145',
186 'source' => 'SSF',
187 'contribution_status_id' => 2,
188 ]);
189 }
190
191 /**
192 * Schedule message delivery for contributions of type "Member Dues".
193 */
194 public function scheduleForDues() {
195 $this->schedule->mapping_id = CRM_Contribute_ActionMapping_ByType::MAPPING_ID;
196 $this->schedule->start_action_date = 'receive_date';
197 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([1]);
198 $this->schedule->entity_status = CRM_Utils_Array::implodePadded([1]);
199 }
200
201 /**
202 * Schedule message delivery for contributions of type "Donation".
203 */
204 public function scheduleForDonation() {
205 $this->schedule->mapping_id = CRM_Contribute_ActionMapping_ByType::MAPPING_ID;
206 $this->schedule->start_action_date = 'receive_date';
207 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([2]);
208 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
209 }
210
211 /**
212 * Schedule message delivery for any contribution, regardless of type.
213 */
214 public function scheduleForAny() {
215 $this->schedule->mapping_id = CRM_Contribute_ActionMapping_ByType::MAPPING_ID;
216 $this->schedule->start_action_date = 'receive_date';
217 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(NULL);
218 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
219 }
220
221 /**
222 * Schedule message delivery to the 'soft credit' assignee.
223 */
224 public function scheduleForSoftCreditor() {
225 $this->schedule->mapping_id = CRM_Contribute_ActionMapping_ByType::MAPPING_ID;
226 $this->schedule->start_action_date = 'receive_date';
227 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(NULL);
228 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
229 $this->schedule->limit_to = 1;
230 $this->schedule->recipient = 'soft_credit_type';
231 $this->schedule->recipient_listing = CRM_Utils_Array::implodePadded([3]);
232 }
233
234 public function useHelloFirstNameStatus() {
235 $this->schedule->subject = 'Hello, {contact.first_name}. @{contribution.status}. (via subject)';
236 $this->schedule->body_html = '<p>Hello, {contact.first_name}. @{contribution.status}. (via body_html)</p>';
237 $this->schedule->body_text = 'Hello, {contact.first_name}. @{contribution.status}. (via body_text)';
238 }
239
240 }