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