Merge pull request #17236 from civicrm/5.25
[civicrm-core.git] / tests / phpunit / CRM / Activity / ActionMappingTest.php
CommitLineData
83aefb2e
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
83aefb2e 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 |
83aefb2e
TO
9 +--------------------------------------------------------------------+
10 */
11
12require_once 'CiviTest/CiviUnitTestCase.php';
13
14/**
15 * Class CRM_Activity_ActionMappingTest
16 * @group ActionSchedule
17 *
18 * This class tests various configurations of scheduled-reminders, with a focus on
19 * reminders for *activity types*. It follows a design/pattern described in
20 * AbstractMappingTest.
21 *
22 * NOTE: There are also pretty deep tests of activity-based reminders in
23 * CRM_Core_BAO_ActionScheduleTest.
24 *
25 * @see \Civi\ActionSchedule\AbstractMappingTest
26 * @see CRM_Core_BAO_ActionScheduleTest
27 */
28class CRM_Activity_ActionMappingTest extends \Civi\ActionSchedule\AbstractMappingTest {
29
30 /**
31 * Generate a list of test cases, where each is a distinct combination of
32 * data, schedule-rules, and schedule results.
33 *
34 * @return array
35 * - targetDate: string; eg "2015-02-01 00:00:01"
36 * - setupFuncs: string, space-separated list of setup functions
37 * - messages: array; each item is a message that's expected to be sent
38 * each message may include keys:
39 * - time: approximate time (give or take a few seconds)
40 * - recipients: array of emails
41 * - subject: regex
42 */
43 public function createTestCases() {
9099cab3 44 $cs = [];
83aefb2e 45
9099cab3 46 $cs[] = [
83aefb2e
TO
47 '2015-02-01 00:00:00',
48 'addAliceMeeting scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
9099cab3
CW
49 [
50 [
83aefb2e 51 'time' => '2015-02-01 00:00:00',
9099cab3 52 'to' => ['alice@example.org'],
83aefb2e 53 'subject' => '/Hello, Alice.*via subject/',
9099cab3
CW
54 ],
55 ],
56 ];
83aefb2e 57
cdc402cd
TO
58 // FIXME: CRM-19415: This test should pass...
59 // $cs[] = array(
60 // '2015-02-01 00:00:00',
61 // 'addAliceMeeting scheduleForAny startOnTime useHelloFirstName recipientIsBob',
62 // array(
63 // array(
64 // 'time' => '2015-02-01 00:00:00',
65 // 'to' => array('bob@example.org'),
66 // 'subject' => '/Hello, Bob.*via subject/',
67 // // It might make more sense to get Alice's details... but path of least resistance...
68 // ),
69 // ),
70 // );
83aefb2e 71
9099cab3 72 $cs[] = [
83aefb2e
TO
73 '2015-02-01 00:00:00',
74 'addAliceMeeting addBobPhoneCall scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
9099cab3
CW
75 [
76 [
83aefb2e 77 'time' => '2015-02-01 00:00:00',
9099cab3 78 'to' => ['alice@example.org'],
83aefb2e 79 'subject' => '/Hello, Alice.*via subject/',
9099cab3
CW
80 ],
81 ],
82 ];
83aefb2e 83
9099cab3 84 $cs[] = [
83aefb2e
TO
85 '2015-02-01 00:00:00',
86 'addAliceMeeting addBobPhoneCall scheduleForAny startOnTime useHelloFirstName recipientIsActivitySource',
9099cab3
CW
87 [
88 [
83aefb2e 89 'time' => '2015-02-01 00:00:00',
9099cab3 90 'to' => ['alice@example.org'],
83aefb2e 91 'subject' => '/Hello, Alice.*via subject/',
9099cab3
CW
92 ],
93 [
83aefb2e 94 'time' => '2015-02-01 00:00:00',
9099cab3 95 'to' => ['bob@example.org'],
83aefb2e 96 'subject' => '/Hello, Bob.*via subject/',
9099cab3
CW
97 ],
98 ],
99 ];
83aefb2e 100
9099cab3 101 $cs[] = [
83aefb2e
TO
102 '2015-02-02 00:00:00',
103 'addAliceMeeting addBobPhoneCall scheduleForPhoneCall startWeekBefore repeatTwoWeeksAfter useHelloFirstName recipientIsActivitySource',
9099cab3
CW
104 [
105 [
83aefb2e 106 'time' => '2015-01-26 00:00:00',
9099cab3 107 'to' => ['bob@example.org'],
83aefb2e 108 'subject' => '/Hello, Bob.*via subject/',
9099cab3
CW
109 ],
110 [
83aefb2e 111 'time' => '2015-02-02 00:00:00',
9099cab3 112 'to' => ['bob@example.org'],
83aefb2e 113 'subject' => '/Hello, Bob.*via subject/',
9099cab3
CW
114 ],
115 [
83aefb2e 116 'time' => '2015-02-09 00:00:00',
9099cab3 117 'to' => ['bob@example.org'],
83aefb2e 118 'subject' => '/Hello, Bob.*via subject/',
9099cab3
CW
119 ],
120 [
83aefb2e 121 'time' => '2015-02-16 00:00:00',
9099cab3 122 'to' => ['bob@example.org'],
83aefb2e 123 'subject' => '/Hello, Bob.*via subject/',
9099cab3
CW
124 ],
125 ],
126 ];
83aefb2e 127
deecfdef
AH
128 // No recipients: Dave has `do_not_email`, Edith is dead, and Francis' email
129 // is on hold.
206a21e8
AH
130 $cs[] = [
131 '2015-02-01 00:00:00',
deecfdef 132 'addDaveMeeting addEdithMeeting addFrancisMeeting scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
206a21e8
AH
133 [],
134 ];
135
83aa0e63
AH
136 // Gretchen has one email on hold, but her primary email is not on hold.
137 $cs[] = [
138 '2015-02-01 00:00:00',
139 'addGretchenMeeting scheduleForMeeting startOnTime useHelloFirstName recipientIsActivitySource',
140 [
141 [
142 'time' => '2015-02-01 00:00:00',
143 'to' => ['gretchen@example.org'],
144 'subject' => '/Hello, Gretchen.*via subject/',
145 ],
146 ],
147 ];
148
83aefb2e
TO
149 return $cs;
150 }
151
152 /**
153 * Create an activity record for Alice with type "Meeting".
154 */
155 public function addAliceMeeting() {
9099cab3 156 $this->callAPISuccess('Activity', 'create', [
83aefb2e
TO
157 'source_contact_id' => $this->contacts['alice']['id'],
158 'activity_type_id' => 'Meeting',
159 'subject' => 'Subject for Alice',
160 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
161 'status_id' => 2,
9099cab3
CW
162 'assignee_contact_id' => [$this->contacts['carol']['id']],
163 ]);
83aefb2e
TO
164 }
165
166 /**
167 * Create a contribution record for Bob with type "Donation".
168 */
169 public function addBobPhoneCall() {
9099cab3 170 $this->callAPISuccess('Activity', 'create', [
83aefb2e
TO
171 'source_contact_id' => $this->contacts['bob']['id'],
172 'activity_type_id' => 'Phone Call',
173 'subject' => 'Subject for Bob',
174 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
175 'status_id' => 2,
9099cab3
CW
176 'assignee_contact_id' => [$this->contacts['carol']['id']],
177 ]);
83aefb2e
TO
178 }
179
206a21e8
AH
180 /**
181 * Create an activity record for Dave with type "Meeting". Dave has
182 * "do_not_email" set, so he should never receive an email reminder.
183 */
184 public function addDaveMeeting() {
185 $this->callAPISuccess('Activity', 'create', [
186 'source_contact_id' => $this->contacts['dave']['id'],
187 'activity_type_id' => 'Meeting',
188 'subject' => 'Subject for Dave',
189 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
190 'status_id' => 2,
191 'assignee_contact_id' => [$this->contacts['carol']['id']],
192 ]);
193 }
194
195 /**
196 * Create an activity record for Edith with type "Meeting". Edith is dead, so
197 * she should never receive an email reminder.
198 */
199 public function addEdithMeeting() {
200 $this->callAPISuccess('Activity', 'create', [
201 'source_contact_id' => $this->contacts['edith']['id'],
202 'activity_type_id' => 'Meeting',
203 'subject' => 'Subject for Edith',
204 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
205 'status_id' => 2,
206 'assignee_contact_id' => [$this->contacts['carol']['id']],
207 ]);
208 }
209
deecfdef
AH
210 /**
211 * Create an activity record for Francis with type "Meeting". Francis' email
212 * is misspelled and has bounced, so he should never receive an email reminder.
213 */
214 public function addFrancisMeeting() {
215 $this->callAPISuccess('Activity', 'create', [
216 'source_contact_id' => $this->contacts['francis']['id'],
217 'activity_type_id' => 'Meeting',
218 'subject' => 'Subject for Francis',
219 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
220 'status_id' => 2,
221 'assignee_contact_id' => [$this->contacts['carol']['id']],
222 ]);
223 }
224
83aa0e63
AH
225 /**
226 * Create an activity record for Gretchen with type "Meeting".
227 */
228 public function addGretchenMeeting() {
229 $this->callAPISuccess('Activity', 'create', [
230 'source_contact_id' => $this->contacts['gretchen']['id'],
231 'activity_type_id' => 'Meeting',
232 'subject' => 'Subject for Gretchen',
233 'activity_date_time' => date('Y-m-d H:i:s', strtotime($this->targetDate)),
234 'status_id' => 2,
235 'assignee_contact_id' => [$this->contacts['carol']['id']],
236 ]);
237 }
238
83aefb2e
TO
239 /**
240 * Schedule message delivery for activities of type "Meeting".
241 */
242 public function scheduleForMeeting() {
243 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
244 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
245 $this->schedule->start_action_date = 'receive_date';
9099cab3
CW
246 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([array_search('Meeting', $actTypes)]);
247 $this->schedule->entity_status = CRM_Utils_Array::implodePadded([2]);
83aefb2e
TO
248 }
249
250 /**
251 * Schedule message delivery for activities of type "Phone Call".
252 */
253 public function scheduleForPhoneCall() {
254 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
255 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
256 $this->schedule->start_action_date = 'receive_date';
9099cab3 257 $this->schedule->entity_value = CRM_Utils_Array::implodePadded([array_search('Phone Call', $actTypes)]);
83aefb2e
TO
258 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
259 }
260
261 /**
262 * Schedule message delivery for any contribution, regardless of type.
263 */
264 public function scheduleForAny() {
265 $actTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id');
266 $this->schedule->mapping_id = CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID;
267 $this->schedule->start_action_date = 'receive_date';
268 $this->schedule->entity_value = CRM_Utils_Array::implodePadded(array_keys($actTypes));
269 $this->schedule->entity_status = CRM_Utils_Array::implodePadded(NULL);
270 }
271
272 /**
273 * Set the recipient to "Choose Recipient(s): Bob".
274 */
275 public function recipientIsBob() {
276 $this->schedule->limit_to = 1;
277 $this->schedule->recipient = NULL;
278 $this->schedule->recipient_listing = NULL;
279 $this->schedule->recipient_manual = $this->contacts['bob']['id'];
280 }
281
282 /**
283 * Set the recipient to "Activity Assignee".
284 */
285 public function recipientIsActivityAssignee() {
286 $this->schedule->limit_to = 1;
287 $this->schedule->recipient = 1;
288 $this->schedule->recipient_listing = NULL;
289 $this->schedule->recipient_manual = NULL;
290 }
291
292 /**
293 * Set the recipient to "Activity Source".
294 */
295 public function recipientIsActivitySource() {
296 $this->schedule->limit_to = 1;
297 $this->schedule->recipient = 2;
298 $this->schedule->recipient_listing = NULL;
299 $this->schedule->recipient_manual = NULL;
300 }
301
302}