Use new checkPermissions shorthand in api calls
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / ActionScheduleTest.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 use Civi\Api4\ActivityContact;
13
14 /**
15 * Class CRM_Core_BAO_ActionScheduleTest
16 * @group ActionSchedule
17 * @group headless
18 *
19 * There are additional tests for some specific entities in other classes:
20 * @see CRM_Activity_ActionMappingTest
21 * @see CRM_Contribute_ActionMapping_ByTypeTest
22 */
23 class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
24
25 /**
26 * @var CiviMailUtils
27 */
28 public $mut;
29
30 /**
31 * Entities set up for the test.
32 *
33 * @var array
34 */
35 private $fixtures = [];
36
37 /**
38 * Setup for tests.
39 *
40 * @throws CRM_Core_Exception
41 */
42 public function setUp() {
43 parent::setUp();
44
45 $this->mut = new CiviMailUtils($this, TRUE);
46
47 $this->fixtures['rolling_membership_type'] = [
48 'period_type' => 'rolling',
49 'duration_unit' => 'month',
50 'duration_interval' => '3',
51 'is_active' => 1,
52 'domain_id' => 1,
53 'financial_type_id' => 2,
54 ];
55
56 $this->fixtures['rolling_membership'] = [
57 'membership_type_id' => [
58 'period_type' => 'rolling',
59 'duration_unit' => 'month',
60 'duration_interval' => '3',
61 'is_active' => 1,
62 ],
63 'join_date' => '20120315',
64 'start_date' => '20120315',
65 'end_date' => '20120615',
66 'is_override' => 0,
67 ];
68
69 $this->fixtures['rolling_membership_past'] = [
70 'membership_type_id' => [
71 'period_type' => 'rolling',
72 'duration_unit' => 'month',
73 'duration_interval' => '3',
74 'is_active' => 1,
75 ],
76 'join_date' => '20100310',
77 'start_date' => '20100310',
78 'end_date' => '20100610',
79 'is_override' => 'NULL',
80 ];
81 $this->fixtures['participant'] = [
82 'event_id' => [
83 'is_active' => 1,
84 'is_template' => 0,
85 'title' => 'Example Event',
86 'start_date' => '20120315',
87 'end_date' => '20120615',
88 ],
89 // Attendee.
90 'role_id' => '1',
91 // No-show.
92 'status_id' => '8',
93 ];
94
95 $this->fixtures['phonecall'] = [
96 'status_id' => 1,
97 'activity_type_id' => 2,
98 'activity_date_time' => '20120615100000',
99 'is_current_revision' => 1,
100 'is_deleted' => 0,
101 ];
102 $this->fixtures['contact'] = [
103 'is_deceased' => 0,
104 'contact_type' => 'Individual',
105 'email' => 'test-member@example.com',
106 'gender_id' => 'Female',
107 'first_name' => 'Churmondleia',
108 'last_name' => 'Ōtākou',
109 ];
110 $this->fixtures['contact_2'] = [
111 'is_deceased' => 0,
112 'contact_type' => 'Individual',
113 'email' => 'test-contact-2@example.com',
114 'gender_id' => 'Male',
115 'first_name' => 'Fabble',
116 'last_name' => 'Fi',
117 ];
118 $this->fixtures['contact_birthdate'] = [
119 'is_deceased' => 0,
120 'contact_type' => 'Individual',
121 'email' => 'test-bday@example.com',
122 'birth_date' => '20050707',
123 ];
124 $this->fixtures['sched_activity_1day'] = [
125 'name' => 'One_Day_Phone_Call_Notice',
126 'title' => 'One Day Phone Call Notice',
127 'limit_to' => '1',
128 'absolute_date' => NULL,
129 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
130 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
131 'end_action' => NULL,
132 'end_date' => NULL,
133 'end_frequency_interval' => NULL,
134 'end_frequency_unit' => NULL,
135 'entity_status' => '1',
136 'entity_value' => '2',
137 'group_id' => NULL,
138 'is_active' => '1',
139 'is_repeat' => '0',
140 'mapping_id' => '1',
141 'msg_template_id' => NULL,
142 'recipient' => '2',
143 'recipient_listing' => NULL,
144 'recipient_manual' => NULL,
145 'record_activity' => 1,
146 'repetition_frequency_interval' => NULL,
147 'repetition_frequency_unit' => NULL,
148 'start_action_condition' => 'before',
149 'start_action_date' => 'activity_date_time',
150 'start_action_offset' => '1',
151 'start_action_unit' => 'day',
152 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
153 ];
154 $this->fixtures['sched_activity_1day_r'] = [
155 'name' => 'One_Day_Phone_Call_Notice_R',
156 'title' => 'One Day Phone Call Notice R',
157 'limit_to' => 1,
158 'absolute_date' => NULL,
159 'body_html' => '<p>1-Day (repeating)</p>',
160 'body_text' => '1-Day (repeating)',
161 'end_action' => 'after',
162 'end_date' => 'activity_date_time',
163 'end_frequency_interval' => '2',
164 'end_frequency_unit' => 'day',
165 'entity_status' => '1',
166 'entity_value' => '2',
167 'group_id' => NULL,
168 'is_active' => '1',
169 'is_repeat' => '1',
170 'mapping_id' => '1',
171 'msg_template_id' => NULL,
172 'recipient' => '2',
173 'recipient_listing' => NULL,
174 'recipient_manual' => NULL,
175 'record_activity' => NULL,
176 'repetition_frequency_interval' => '6',
177 'repetition_frequency_unit' => 'hour',
178 'start_action_condition' => 'before',
179 'start_action_date' => 'activity_date_time',
180 'start_action_offset' => '1',
181 'start_action_unit' => 'day',
182 'subject' => '1-Day (repeating) (about {activity.activity_type})',
183 ];
184 $this->fixtures['sched_activity_1day_r_on_abs_date'] = [
185 'name' => 'One_Day_Phone_Call_Notice_R',
186 'title' => 'One Day Phone Call Notice R',
187 'limit_to' => 1,
188 'absolute_date' => CRM_Utils_Date::processDate('20120614100000'),
189 'body_html' => '<p>1-Day (repeating)</p>',
190 'body_text' => '1-Day (repeating)',
191 'entity_status' => '1',
192 'entity_value' => '2',
193 'group_id' => NULL,
194 'is_active' => '1',
195 'is_repeat' => '1',
196 'mapping_id' => '1',
197 'msg_template_id' => NULL,
198 'recipient' => '2',
199 'recipient_listing' => NULL,
200 'recipient_manual' => NULL,
201 'record_activity' => NULL,
202 'repetition_frequency_interval' => '6',
203 'repetition_frequency_unit' => 'hour',
204 'end_action' => 'after',
205 'end_date' => 'activity_date_time',
206 'end_frequency_interval' => '2',
207 'end_frequency_unit' => 'day',
208 'start_action_condition' => '',
209 'start_action_date' => '',
210 'start_action_offset' => '',
211 'start_action_unit' => '',
212 'subject' => '1-Day (repeating) (about {activity.activity_type})',
213 ];
214 $this->fixtures['sched_eventname_1day_on_abs_date'] = [
215 'name' => 'sched_eventname_1day_on_abs_date',
216 'title' => 'sched_eventname_1day_on_abs_date',
217 'limit_to' => 1,
218 'absolute_date' => CRM_Utils_Date::processDate('20120614100000'),
219 'body_html' => '<p>sched_eventname_1day_on_abs_date</p>',
220 'body_text' => 'sched_eventname_1day_on_abs_date',
221 'entity_status' => '1',
222 'entity_value' => '2',
223 'group_id' => NULL,
224 'is_active' => '1',
225 'is_repeat' => '0',
226 'mapping_id' => '3',
227 'msg_template_id' => NULL,
228 'recipient' => '2',
229 'recipient_listing' => NULL,
230 'recipient_manual' => NULL,
231 'record_activity' => NULL,
232 'repetition_frequency_interval' => NULL,
233 'repetition_frequency_unit' => NULL,
234 'end_action' => NULL,
235 'end_date' => NULL,
236 'end_frequency_interval' => NULL,
237 'end_frequency_unit' => NULL,
238 'start_action_condition' => NULL,
239 'start_action_date' => NULL,
240 'start_action_offset' => NULL,
241 'start_action_unit' => NULL,
242 'subject' => 'sched_eventname_1day_on_abs_date',
243 ];
244 $this->fixtures['sched_membership_join_2week'] = [
245 'name' => 'sched_membership_join_2week',
246 'title' => 'sched_membership_join_2week',
247 'absolute_date' => '',
248 'body_html' => '<p>body sched_membership_join_2week</p>',
249 'body_text' => 'body sched_membership_join_2week',
250 'end_action' => '',
251 'end_date' => '',
252 'end_frequency_interval' => '',
253 'end_frequency_unit' => '',
254 'entity_status' => '',
255 'entity_value' => '',
256 'group_id' => '',
257 'is_active' => 1,
258 'is_repeat' => '0',
259 'mapping_id' => 4,
260 'msg_template_id' => '',
261 'recipient' => '',
262 'recipient_listing' => '',
263 'recipient_manual' => '',
264 'record_activity' => 1,
265 'repetition_frequency_interval' => '',
266 'repetition_frequency_unit' => '',
267 'start_action_condition' => 'after',
268 'start_action_date' => 'membership_join_date',
269 'start_action_offset' => '2',
270 'start_action_unit' => 'week',
271 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
272 ];
273 $this->fixtures['sched_membership_start_1week'] = [
274 'name' => 'sched_membership_start_1week',
275 'title' => 'sched_membership_start_1week',
276 'absolute_date' => '',
277 'body_html' => '<p>body sched_membership_start_1week</p>',
278 'body_text' => 'body sched_membership_start_1week',
279 'end_action' => '',
280 'end_date' => '',
281 'end_frequency_interval' => '',
282 'end_frequency_unit' => '',
283 'entity_status' => '',
284 'entity_value' => '',
285 'group_id' => '',
286 'is_active' => 1,
287 'is_repeat' => '0',
288 'mapping_id' => 4,
289 'msg_template_id' => '',
290 'recipient' => '',
291 'recipient_listing' => '',
292 'recipient_manual' => '',
293 'record_activity' => 1,
294 'repetition_frequency_interval' => '',
295 'repetition_frequency_unit' => '',
296 'start_action_condition' => 'after',
297 'start_action_date' => 'membership_start_date',
298 'start_action_offset' => '1',
299 'start_action_unit' => 'week',
300 'subject' => 'subject sched_membership_start_1week (joined {membership.start_date})',
301 ];
302 $this->fixtures['sched_membership_end_2week'] = [
303 'name' => 'sched_membership_end_2week',
304 'title' => 'sched_membership_end_2week',
305 'absolute_date' => '',
306 'body_html' => '<p>body sched_membership_end_2week</p>',
307 'body_text' => 'body sched_membership_end_2week',
308 'end_action' => '',
309 'end_date' => '',
310 'end_frequency_interval' => '',
311 'end_frequency_unit' => '',
312 'entity_status' => '',
313 'entity_value' => '',
314 'group_id' => '',
315 'is_active' => 1,
316 'is_repeat' => '0',
317 'mapping_id' => 4,
318 'msg_template_id' => '',
319 'recipient' => '',
320 'recipient_listing' => '',
321 'recipient_manual' => '',
322 'record_activity' => 1,
323 'repetition_frequency_interval' => '',
324 'repetition_frequency_unit' => '',
325 'start_action_condition' => 'before',
326 'start_action_date' => 'membership_end_date',
327 'start_action_offset' => '2',
328 'start_action_unit' => 'week',
329 'subject' => 'subject sched_membership_end_2week',
330 ];
331 $this->fixtures['sched_on_membership_end_date'] = [
332 'name' => 'sched_on_membership_end_date',
333 'title' => 'sched_on_membership_end_date',
334 'body_html' => '<p>Your membership expired today</p>',
335 'body_text' => 'Your membership expired today',
336 'is_active' => 1,
337 'mapping_id' => 4,
338 'record_activity' => 1,
339 'start_action_condition' => 'after',
340 'start_action_date' => 'membership_end_date',
341 'start_action_offset' => '0',
342 'start_action_unit' => 'hour',
343 'subject' => 'subject send reminder on membership_end_date',
344 ];
345 $this->fixtures['sched_after_1day_membership_end_date'] = [
346 'name' => 'sched_after_1day_membership_end_date',
347 'title' => 'sched_after_1day_membership_end_date',
348 'body_html' => '<p>Your membership expired yesterday</p>',
349 'body_text' => 'Your membership expired yesterday',
350 'is_active' => 1,
351 'mapping_id' => 4,
352 'record_activity' => 1,
353 'start_action_condition' => 'after',
354 'start_action_date' => 'membership_end_date',
355 'start_action_offset' => '1',
356 'start_action_unit' => 'day',
357 'subject' => 'subject send reminder on membership_end_date',
358 ];
359
360 $this->fixtures['sched_membership_end_2month'] = [
361 'name' => 'sched_membership_end_2month',
362 'title' => 'sched_membership_end_2month',
363 'absolute_date' => '',
364 'body_html' => '<p>body sched_membership_end_2month</p>',
365 'body_text' => 'body sched_membership_end_2month',
366 'end_action' => '',
367 'end_date' => '',
368 'end_frequency_interval' => '',
369 'end_frequency_unit' => '',
370 'entity_status' => '',
371 'entity_value' => '',
372 'group_id' => '',
373 'is_active' => 1,
374 'is_repeat' => '0',
375 'mapping_id' => 4,
376 'msg_template_id' => '',
377 'recipient' => '',
378 'recipient_listing' => '',
379 'recipient_manual' => '',
380 'record_activity' => 1,
381 'repetition_frequency_interval' => '',
382 'repetition_frequency_unit' => '',
383 'start_action_condition' => 'after',
384 'start_action_date' => 'membership_end_date',
385 'start_action_offset' => '2',
386 'start_action_unit' => 'month',
387 'subject' => 'subject sched_membership_end_2month',
388 ];
389
390 $this->fixtures['sched_membership_absolute_date'] = [
391 'name' => 'sched_membership_absolute_date',
392 'title' => 'sched_membership_absolute_date',
393 'absolute_date' => CRM_Utils_Date::processDate('20120614100000'),
394 'body_html' => '<p>body sched_membership_absolute_date</p>',
395 'body_text' => 'body sched_membership_absolute_date',
396 'end_action' => '',
397 'end_date' => '',
398 'end_frequency_interval' => '',
399 'end_frequency_unit' => '',
400 'entity_status' => '',
401 'entity_value' => '',
402 'group_id' => '',
403 'is_active' => 1,
404 'is_repeat' => '0',
405 'mapping_id' => 4,
406 'msg_template_id' => '',
407 'recipient' => '',
408 'recipient_listing' => '',
409 'recipient_manual' => '',
410 'record_activity' => 1,
411 'repetition_frequency_interval' => '',
412 'repetition_frequency_unit' => '',
413 'start_action_condition' => '',
414 'start_action_date' => '',
415 'start_action_offset' => '',
416 'start_action_unit' => '',
417 'subject' => 'subject sched_membership_absolute_date',
418 ];
419
420 $this->fixtures['sched_contact_bday_yesterday'] = [
421 'name' => 'sched_contact_bday_yesterday',
422 'title' => 'sched_contact_bday_yesterday',
423 'absolute_date' => '',
424 'body_html' => '<p>you look like you were born yesterday!</p>',
425 'body_text' => 'you look like you were born yesterday!',
426 'end_action' => '',
427 'end_date' => '',
428 'end_frequency_interval' => '',
429 'end_frequency_unit' => '',
430 'entity_status' => 1,
431 'entity_value' => 'birth_date',
432 'group_id' => '',
433 'is_active' => 1,
434 'is_repeat' => '0',
435 'mapping_id' => 6,
436 'msg_template_id' => '',
437 'recipient' => '',
438 'recipient_listing' => '',
439 'recipient_manual' => '',
440 'record_activity' => 1,
441 'repetition_frequency_interval' => '',
442 'repetition_frequency_unit' => '',
443 'start_action_condition' => 'after',
444 'start_action_date' => 'date_field',
445 'start_action_offset' => '1',
446 'start_action_unit' => 'day',
447 'subject' => 'subject sched_contact_bday_yesterday',
448 ];
449
450 $this->fixtures['sched_contact_bday_anniv'] = [
451 'name' => 'sched_contact_bday_anniv',
452 'title' => 'sched_contact_bday_anniv',
453 'absolute_date' => '',
454 'body_html' => '<p>happy birthday!</p>',
455 'body_text' => 'happy birthday!',
456 'end_action' => '',
457 'end_date' => '',
458 'end_frequency_interval' => '',
459 'end_frequency_unit' => '',
460 'entity_status' => 2,
461 'entity_value' => 'birth_date',
462 'group_id' => '',
463 'is_active' => 1,
464 'is_repeat' => '0',
465 'mapping_id' => 6,
466 'msg_template_id' => '',
467 'recipient' => '',
468 'recipient_listing' => '',
469 'recipient_manual' => '',
470 'record_activity' => 1,
471 'repetition_frequency_interval' => '',
472 'repetition_frequency_unit' => '',
473 'start_action_condition' => 'before',
474 'start_action_date' => 'date_field',
475 'start_action_offset' => '1',
476 'start_action_unit' => 'day',
477 'subject' => 'subject sched_contact_bday_anniv',
478 ];
479
480 $this->fixtures['sched_contact_grad_tomorrow'] = [
481 'name' => 'sched_contact_grad_tomorrow',
482 'title' => 'sched_contact_grad_tomorrow',
483 'absolute_date' => '',
484 'body_html' => '<p>congratulations on your graduation!</p>',
485 'body_text' => 'congratulations on your graduation!',
486 'end_action' => '',
487 'end_date' => '',
488 'end_frequency_interval' => '',
489 'end_frequency_unit' => '',
490 'entity_status' => 1,
491 'group_id' => '',
492 'is_active' => 1,
493 'is_repeat' => '0',
494 'mapping_id' => 6,
495 'msg_template_id' => '',
496 'recipient' => '',
497 'recipient_listing' => '',
498 'recipient_manual' => '',
499 'record_activity' => 1,
500 'repetition_frequency_interval' => '',
501 'repetition_frequency_unit' => '',
502 'start_action_condition' => 'before',
503 'start_action_date' => 'date_field',
504 'start_action_offset' => '1',
505 'start_action_unit' => 'day',
506 'subject' => 'subject sched_contact_grad_tomorrow',
507 ];
508
509 $this->fixtures['sched_contact_grad_anniv'] = [
510 'name' => 'sched_contact_grad_anniv',
511 'title' => 'sched_contact_grad_anniv',
512 'absolute_date' => '',
513 'body_html' => '<p>dear alum, please send us money.</p>',
514 'body_text' => 'dear alum, please send us money.',
515 'end_action' => '',
516 'end_date' => '',
517 'end_frequency_interval' => '',
518 'end_frequency_unit' => '',
519 'entity_status' => 2,
520 'group_id' => '',
521 'is_active' => 1,
522 'is_repeat' => '0',
523 'mapping_id' => 6,
524 'msg_template_id' => '',
525 'recipient' => '',
526 'recipient_listing' => '',
527 'recipient_manual' => '',
528 'record_activity' => 1,
529 'repetition_frequency_interval' => '',
530 'repetition_frequency_unit' => '',
531 'start_action_condition' => 'after',
532 'start_action_date' => 'date_field',
533 'start_action_offset' => '1',
534 'start_action_unit' => 'week',
535 'subject' => 'subject sched_contact_grad_anniv',
536 ];
537
538 $this->fixtures['sched_contact_created_yesterday'] = [
539 'name' => 'sched_contact_created_yesterday',
540 'title' => 'sched_contact_created_yesterday',
541 'absolute_date' => '',
542 'body_html' => '<p>Your contact was created yesterday</p>',
543 'body_text' => 'Your contact was created yesterday!',
544 'end_action' => '',
545 'end_date' => '',
546 'end_frequency_interval' => '',
547 'end_frequency_unit' => '',
548 'entity_status' => 1,
549 'entity_value' => 'created_date',
550 'group_id' => '',
551 'is_active' => 1,
552 'is_repeat' => '0',
553 'mapping_id' => 6,
554 'msg_template_id' => '',
555 'recipient' => '',
556 'recipient_listing' => '',
557 'recipient_manual' => '',
558 'record_activity' => 1,
559 'repetition_frequency_interval' => '',
560 'repetition_frequency_unit' => '',
561 'start_action_condition' => 'after',
562 'start_action_date' => 'date_field',
563 'start_action_offset' => '1',
564 'start_action_unit' => 'day',
565 'subject' => 'subject sched_contact_created_yesterday',
566 ];
567
568 $this->fixtures['sched_contact_mod_anniv'] = [
569 'name' => 'sched_contact_mod_anniv',
570 'title' => 'sched_contact_mod_anniv',
571 'absolute_date' => '',
572 'body_html' => '<p>You last updated your data last year</p>',
573 'body_text' => 'Go update your stuff!',
574 'end_action' => '',
575 'end_date' => '',
576 'end_frequency_interval' => '',
577 'end_frequency_unit' => '',
578 'entity_status' => 2,
579 'entity_value' => 'modified_date',
580 'group_id' => '',
581 'is_active' => 1,
582 'is_repeat' => '0',
583 'mapping_id' => 6,
584 'msg_template_id' => '',
585 'recipient' => '',
586 'recipient_listing' => '',
587 'recipient_manual' => '',
588 'record_activity' => 1,
589 'repetition_frequency_interval' => '',
590 'repetition_frequency_unit' => '',
591 'start_action_condition' => 'before',
592 'start_action_date' => 'date_field',
593 'start_action_offset' => '1',
594 'start_action_unit' => 'day',
595 'subject' => 'subject sched_contact_mod_anniv',
596 ];
597
598 $this->fixtures['sched_eventtype_start_1week_before'] = [
599 'name' => 'sched_eventtype_start_1week_before',
600 'title' => 'sched_eventtype_start_1week_before',
601 'absolute_date' => '',
602 'body_html' => '<p>body sched_eventtype_start_1week_before ({event.title})</p>',
603 'body_text' => 'body sched_eventtype_start_1week_before ({event.title})',
604 'end_action' => '',
605 'end_date' => '',
606 'end_frequency_interval' => '',
607 'end_frequency_unit' => '',
608 // participant status id
609 'entity_status' => '',
610 // event type id
611 'entity_value' => '',
612 'group_id' => '',
613 'is_active' => 1,
614 'is_repeat' => '0',
615 // event type
616 'mapping_id' => 2,
617 'msg_template_id' => '',
618 'recipient' => '',
619 'recipient_listing' => '',
620 'recipient_manual' => '',
621 'record_activity' => 1,
622 'repetition_frequency_interval' => '',
623 'repetition_frequency_unit' => '',
624 'start_action_condition' => 'before',
625 'start_action_date' => 'event_start_date',
626 'start_action_offset' => '1',
627 'start_action_unit' => 'week',
628 'subject' => 'subject sched_eventtype_start_1week_before ({event.title})',
629 ];
630 $this->fixtures['sched_eventtype_end_2month_repeat_twice_2_weeks'] = [
631 'name' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
632 'title' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
633 'absolute_date' => '',
634 'body_html' => '<p>body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}</p>',
635 'body_text' => 'body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
636 'end_action' => 'after',
637 'end_date' => 'event_end_date',
638 'end_frequency_interval' => '3',
639 'end_frequency_unit' => 'month',
640 // participant status id
641 'entity_status' => '',
642 // event type id
643 'entity_value' => '',
644 'group_id' => '',
645 'is_active' => 1,
646 'is_repeat' => '1',
647 // event type
648 'mapping_id' => 2,
649 'msg_template_id' => '',
650 'recipient' => '',
651 'recipient_listing' => '',
652 'recipient_manual' => '',
653 'record_activity' => 1,
654 'repetition_frequency_interval' => '2',
655 'repetition_frequency_unit' => 'week',
656 'start_action_condition' => 'after',
657 'start_action_date' => 'event_end_date',
658 'start_action_offset' => '2',
659 'start_action_unit' => 'month',
660 'subject' => 'subject sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
661 ];
662
663 $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'] = [
664 'name' => 'sched_membership_end_2month',
665 'title' => 'sched_membership_end_2month',
666 'absolute_date' => '',
667 'body_html' => '<p>body sched_membership_end_2month</p>',
668 'body_text' => 'body sched_membership_end_2month',
669 'end_action' => '',
670 'end_date' => 'membership_end_date',
671 'end_frequency_interval' => '4',
672 'end_frequency_unit' => 'month',
673 'entity_status' => '',
674 'entity_value' => '',
675 'group_id' => '',
676 'is_active' => 1,
677 'is_repeat' => '1',
678 'mapping_id' => 4,
679 'msg_template_id' => '',
680 'recipient' => '',
681 'recipient_listing' => '',
682 'recipient_manual' => '',
683 'record_activity' => 1,
684 'repetition_frequency_interval' => '4',
685 'repetition_frequency_unit' => 'week',
686 'start_action_condition' => 'after',
687 'start_action_date' => 'membership_end_date',
688 'start_action_offset' => '2',
689 'start_action_unit' => 'month',
690 'subject' => 'subject sched_membership_end_2month',
691 ];
692 $this->fixtures['sched_membership_end_limit_to_none'] = [
693 'name' => 'limit to none',
694 'title' => 'limit to none',
695 'absolute_date' => '',
696 'body_html' => '<p>body sched_membership_end_2month</p>',
697 'body_text' => 'body sched_membership_end_2month',
698 'end_action' => '',
699 'end_date' => '',
700 'end_frequency_interval' => '4',
701 'end_frequency_unit' => 'month',
702 'entity_status' => '',
703 'entity_value' => '',
704 'limit_to' => 0,
705 'group_id' => '',
706 'is_active' => 1,
707 'is_repeat' => '1',
708 'mapping_id' => 4,
709 'msg_template_id' => '',
710 'recipient' => '',
711 'recipient_listing' => '',
712 'recipient_manual' => '',
713 'record_activity' => 1,
714 'repetition_frequency_interval' => '4',
715 'repetition_frequency_unit' => 'week',
716 'start_action_condition' => 'after',
717 'start_action_date' => 'membership_end_date',
718 'start_action_offset' => '2',
719 'start_action_unit' => 'month',
720 'subject' => 'limit to none',
721 ];
722 $this->fixtures['sched_on_membership_end_date_repeat_interval'] = [
723 'name' => 'sched_on_membership_end_date',
724 'title' => 'sched_on_membership_end_date',
725 'body_html' => '<p>Your membership expired 1 unit ago</p>',
726 'body_text' => 'Your membership expired 1 unit ago',
727 'end_frequency_interval' => 10,
728 'end_frequency_unit' => 'year',
729 'is_active' => 1,
730 'is_repeat' => TRUE,
731 'mapping_id' => 4,
732 'record_activity' => 1,
733 'start_action_condition' => 'after',
734 'start_action_date' => 'membership_end_date',
735 'start_action_offset' => '0',
736 'start_action_unit' => 'hour',
737 'subject' => 'subject send reminder every unit after membership_end_date',
738 ];
739
740 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
741 'title' => ts('Test Contact Custom group'),
742 'name' => 'test_contact_cg',
743 'extends' => 'Contact',
744 'domain_id' => CRM_Core_Config::domainID(),
745 'is_active' => 1,
746 'collapse_adv_display' => 0,
747 'collapse_display' => 0,
748 ]);
749 $customField = $this->callAPISuccess('CustomField', 'create', [
750 'label' => 'Test Text',
751 'data_type' => 'String',
752 'html_type' => 'Text',
753 'custom_group_id' => $customGroup['id'],
754 ]);
755 $this->fixtures['contact_custom_token'] = [
756 'id' => $customField['id'],
757 'token' => sprintf('{contact.custom_%s}', $customField['id']),
758 'name' => sprintf('custom_%s', $customField['id']),
759 'value' => 'text ' . substr(sha1(rand()), 0, 7),
760 ];
761
762 $this->_setUp();
763 }
764
765 /**
766 * Tears down the fixture, for example, closes a network connection.
767 *
768 * This method is called after a test is executed.
769 *
770 * @throws \CRM_Core_Exception
771 */
772 public function tearDown() {
773 parent::tearDown();
774 $this->mut->clearMessages();
775 $this->mut->stop();
776 unset($this->mut);
777 $this->quickCleanup([
778 'civicrm_action_schedule',
779 'civicrm_action_log',
780 'civicrm_membership',
781 'civicrm_participant',
782 'civicrm_event',
783 'civicrm_email',
784 ]);
785 $this->callAPISuccess('CustomField', 'delete', ['id' => $this->fixtures['contact_custom_token']['id']]);
786 $this->callAPISuccess('CustomGroup', 'delete', [
787 'id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', 'test_contact_cg', 'id', 'name'),
788 ]);
789 $this->_tearDown();
790 }
791
792 /**
793 * Get mailer examples.
794 *
795 * @return array
796 */
797 public function mailerExamples() {
798 $cases = [];
799
800 // Some tokens - short as subject has 128char limit in DB.
801 $someTokensTmpl = implode(';;', [
802 // basic contact token
803 '{contact.display_name}',
804 // funny legacy contact token
805 '{contact.gender}',
806 // funny legacy contact token
807 '{contact.gender_id}',
808 // domain token
809 '{domain.name}',
810 // action-scheduler token
811 '{activity.activity_type}',
812 ]);
813 // Further tokens can be tested in the body text/html.
814 $manyTokensTmpl = implode(';;', [
815 $someTokensTmpl,
816 '{contact.email_greeting}',
817 $this->fixtures['contact_custom_token']['token'],
818 ]);
819 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
820 // can really do is check that it has something.
821 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
822 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, $this->fixtures['contact_custom_token']['value']);
823
824 // In this example, we use a lot of tokens cutting across multiple components.
825 $cases[0] = [
826 // Schedule definition.
827 [
828 'subject' => "subj $someTokensTmpl",
829 'body_html' => "html $manyTokensTmpl",
830 'body_text' => "text $manyTokensTmpl",
831 ],
832 // Assertions (regex).
833 [
834 'from_name' => '/^FIXME$/',
835 'from_email' => '/^info@EXAMPLE.ORG$/',
836 'subject' => "/^subj $someTokensExpected\$/",
837 'body_html' => "/^html $manyTokensExpected\$/",
838 'body_text' => "/^text $manyTokensExpected\$/",
839 ],
840 ];
841
842 // In this example, we customize the from address.
843 $cases[1] = [
844 // Schedule definition.
845 [
846 'from_name' => 'Bob',
847 'from_email' => 'bob@example.org',
848 ],
849 // Assertions (regex).
850 [
851 'from_name' => '/^Bob$/',
852 'from_email' => '/^bob@example.org$/',
853 ],
854 ];
855
856 // In this example, we autoconvert HTML to text
857 $cases[2] = [
858 // Schedule definition.
859 [
860 'body_html' => '<p>Hello &amp; stuff.</p>',
861 'body_text' => '',
862 ],
863 // Assertions (regex).
864 [
865 'body_html' => '/^' . preg_quote('<p>Hello &amp; stuff.</p>', '/') . '/',
866 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
867 ],
868 ];
869
870 // In this example, we autoconvert HTML to text
871 $cases[3] = [
872 // Schedule definition.
873 [
874 'body_html' => '',
875 'body_text' => 'Hello world',
876 ],
877 // Assertions (regex).
878 [
879 'body_html' => '/^--UNDEFINED--$/',
880 'body_text' => '/^Hello world$/',
881 ],
882 ];
883
884 return $cases;
885 }
886
887 /**
888 * This generates a single mailing through the scheduled-reminder
889 * system (using an activity-reminder as a baseline) and
890 * checks that the resulting message satisfies various
891 * regular expressions.
892 *
893 * @param array $schedule
894 * Values to set/override in the schedule.
895 * Ex: array('subject' => 'Hello, {contact.first_name}!').
896 * @param array $patterns
897 * A list of regexes to compare with the actual email.
898 * Ex: array('subject' => '/^Hello, Alice!/').
899 * Keys: subject, body_text, body_html, from_name, from_email.
900 *
901 * @throws \API_Exception
902 * @throws \CRM_Core_Exception
903 * @throws \Civi\API\Exception\UnauthorizedException
904 *
905 * @dataProvider mailerExamples
906 *
907 */
908 public function testMailer($schedule, $patterns) {
909 $actionSchedule = array_merge($this->fixtures['sched_activity_1day'], $schedule);
910 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
911 $this->assertInternalType('numeric', $actionScheduleDao->id);
912
913 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
914 $this->assertInternalType('numeric', $activity->id);
915 $contact = $this->callAPISuccess('contact', 'create', array_merge(
916 $this->fixtures['contact'],
917 [
918 $this->fixtures['contact_custom_token']['name'] => $this->fixtures['contact_custom_token']['value'],
919 ]
920 ));
921 $activity->save();
922
923 ActivityContact::create(FALSE)->setValues([
924 'contact_id' => $contact['id'],
925 'activity_id' => $activity->id,
926 'record_type_id:name' => 'Activity Source',
927 ])->execute();
928
929 CRM_Utils_Time::setTime('2012-06-14 15:00:00');
930 $this->callAPISuccess('job', 'send_reminder');
931 $this->mut->assertRecipients([['test-member@example.com']]);
932 foreach ($this->mut->getAllMessages('ezc') as $message) {
933 /** @var ezcMail $message */
934
935 $messageArray = [];
936 $messageArray['subject'] = $message->subject;
937 $messageArray['from_name'] = $message->from->name;
938 $messageArray['from_email'] = $message->from->email;
939 $messageArray['body_text'] = '--UNDEFINED--';
940 $messageArray['body_html'] = '--UNDEFINED--';
941
942 foreach ($message->fetchParts() as $part) {
943 /** @var ezcMailText ezcMailText */
944 if ($part instanceof ezcMailText && $part->subType === 'html') {
945 $messageArray['body_html'] = $part->text;
946 }
947 if ($part instanceof ezcMailText && $part->subType === 'plain') {
948 $messageArray['body_text'] = $part->text;
949 }
950 }
951
952 foreach ($patterns as $field => $pattern) {
953 $this->assertRegExp($pattern, $messageArray[$field],
954 "Check that '$field'' matches regex. " . print_r(['expected' => $patterns, 'actual' => $messageArray], 1));
955 }
956 }
957 $this->mut->clearMessages();
958 }
959
960 /**
961 * Test calculated activity schedule.
962 *
963 * @throws \CRM_Core_Exception
964 */
965 public function testActivityDateTimeMatchNonRepeatableSchedule() {
966 $this->createScheduleFromFixtures('sched_activity_1day');
967
968 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
969 $this->assertInternalType('numeric', $activity->id);
970 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
971 $activity->subject = 'Test subject for Phonecall';
972 $activity->save();
973
974 $source['contact_id'] = $contact['id'];
975 $source['activity_id'] = $activity->id;
976 $source['record_type_id'] = 2;
977 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
978 $activityContact->save();
979
980 $this->assertCronRuns([
981 [
982 // Before the 24-hour mark, no email
983 'time' => '2012-06-14 04:00:00',
984 'recipients' => [],
985 'subjects' => [],
986 ],
987 [
988 // After the 24-hour mark, an email
989 'time' => '2012-06-14 15:00:00',
990 'recipients' => [['test-member@example.com']],
991 'subjects' => ['1-Day (non-repeating) (about Phone Call)'],
992 ],
993 [
994 // Run cron again; message already sent
995 'time' => '',
996 'recipients' => [],
997 ],
998 ]);
999 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
1000 $activityDAO = new CRM_Activity_DAO_Activity();
1001 $activityDAO->source_record_id = $activity->id;
1002 $activityDAO->activity_type_id = array_search('Reminder Sent', $activityTypes);
1003 $activityDAO->find();
1004 while ($activityDAO->fetch()) {
1005 $this->assertContains($activity->subject, $activityDAO->details);
1006 }
1007 }
1008
1009 /**
1010 * Test schedule creation on repeatable schedule.
1011 *
1012 * @throws \CRM_Core_Exception
1013 */
1014 public function testActivityDateTimeMatchRepeatableSchedule() {
1015 $this->createScheduleFromFixtures('sched_activity_1day_r');
1016
1017 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
1018 $this->assertTrue(is_numeric($activity->id));
1019 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
1020 $activity->save();
1021
1022 $source['contact_id'] = $contact['id'];
1023 $source['activity_id'] = $activity->id;
1024 $source['record_type_id'] = 2;
1025 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1026 $activityContact->save();
1027
1028 $this->assertCronRuns([
1029 [
1030 // Before the 24-hour mark, no email
1031 'time' => '012-06-14 04:00:00',
1032 'recipients' => [],
1033 'subjects' => [],
1034 ],
1035 [
1036 // After the 24-hour mark, an email
1037 'time' => '2012-06-14 15:00:00',
1038 'recipients' => [['test-member@example.com']],
1039 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1040 ],
1041 [
1042 // Run cron 4 hours later; first message already sent
1043 'time' => '2012-06-14 20:00:00',
1044 'recipients' => [],
1045 'subjects' => [],
1046 ],
1047 [
1048 // Run cron 6 hours later; send second message.
1049 'time' => '2012-06-14 21:00:01',
1050 'recipients' => [['test-member@example.com']],
1051 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1052 ],
1053 ]);
1054 }
1055
1056 /**
1057 * @throws \CRM_Core_Exception
1058 */
1059 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate() {
1060 $this->createScheduleFromFixtures('sched_activity_1day_r_on_abs_date');
1061
1062 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
1063 $this->assertInternalType('numeric', $activity->id);
1064 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
1065 $activity->save();
1066
1067 $source['contact_id'] = $contact['id'];
1068 $source['activity_id'] = $activity->id;
1069 $source['record_type_id'] = 2;
1070 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1071 $activityContact->save();
1072
1073 $this->assertCronRuns([
1074 [
1075 // Before the 24-hour mark, no email
1076 'time' => '2012-06-13 04:00:00',
1077 'recipients' => [],
1078 'subjects' => [],
1079 ],
1080 [
1081 // On absolute date set on 2012-06-14
1082 'time' => '2012-06-14 00:00:00',
1083 'recipients' => [['test-member@example.com']],
1084 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1085 ],
1086 [
1087 // Run cron 4 hours later; first message already sent
1088 'time' => '2012-06-14 04:00:00',
1089 'recipients' => [],
1090 'subjects' => [],
1091 ],
1092 [
1093 // Run cron 6 hours later; send second message.
1094 'time' => '2012-06-14 06:00:01',
1095 'recipients' => [['test-member@example.com']],
1096 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1097 ],
1098 ]);
1099 }
1100
1101 /**
1102 * Test event with only an absolute date.
1103 *
1104 * @throws \CRM_Core_Exception
1105 */
1106 public function testEventNameWithAbsoluteDateAndNothingElse() {
1107 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], ['status_id' => 1]));
1108 $this->callAPISuccess('Email', 'create', [
1109 'contact_id' => $participant->contact_id,
1110 'email' => 'test-event@example.com',
1111 ]);
1112 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $participant->contact_id]));
1113
1114 $actionSchedule = $this->fixtures['sched_eventname_1day_on_abs_date'];
1115 $actionSchedule['entity_value'] = $participant->event_id;
1116 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1117
1118 $this->assertCronRuns([
1119 [
1120 // Before the 24-hour mark, no email
1121 'time' => '2012-06-13 04:00:00',
1122 'recipients' => [],
1123 'subjects' => [],
1124 ],
1125 [
1126 // On absolute date set on 2012-06-14
1127 'time' => '2012-06-14 00:00:00',
1128 'recipients' => [['test-event@example.com']],
1129 'subjects' => ['sched_eventname_1day_on_abs_date'],
1130 ],
1131 [
1132 // Run cron 4 hours later; first message already sent
1133 'time' => '2012-06-14 04:00:00',
1134 'recipients' => [],
1135 'subjects' => [],
1136 ],
1137 ]);
1138 }
1139
1140 /**
1141 * For contacts/activities which don't match the schedule filter,
1142 * an email should *not* be sent.
1143 */
1144 // TODO // function testActivityDateTime_NonMatch() { }
1145
1146 /**
1147 * For contacts/members which match schedule based on join/start date,
1148 * an email should be sent.
1149 *
1150 * @throws \CRM_Core_Exception
1151 */
1152 public function testMembershipDateMatch() {
1153 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], ['status_id' => 1]));
1154 $this->assertInternalType('numeric', $membership->id);
1155 $this->callAPISuccess('Email', 'create', [
1156 'contact_id' => $membership->contact_id,
1157 'email' => 'test-member@example.com',
1158 'location_type_id' => 1,
1159 'is_primary' => 1,
1160 ]);
1161
1162 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1163 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
1164 $actionSchedule['entity_value'] = $membership->membership_type_id;
1165 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1166 $this->assertInternalType('numeric', $actionScheduleDao->id);
1167
1168 // start_date=2012-03-15 ; schedule is 2 weeks after join_date
1169 $this->assertCronRuns([
1170 [
1171 // Before the 2-week mark, no email.
1172 'time' => '2012-03-28 01:00:00',
1173 'recipients' => [],
1174 'subjects' => [],
1175 ],
1176 [
1177 // After the 2-week mark, send an email.
1178 'time' => '2012-03-29 01:00:00',
1179 'recipients' => [['test-member@example.com']],
1180 'subjects' => ['subject sched_membership_join_2week (joined March 15th, 2012)'],
1181 ],
1182 ]);
1183
1184 $actionSchedule = $this->fixtures['sched_membership_start_1week'];
1185 $actionSchedule['entity_value'] = $membership->membership_type_id;
1186 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1187 $this->assertInternalType('numeric', $actionScheduleDao->id);
1188
1189 // start_date=2012-03-15 ; schedule is 1 weeks after start_date
1190 $this->assertCronRuns([
1191 [
1192 // Before the 2-week mark, no email.
1193 'time' => '2012-03-21 01:00:00',
1194 'recipients' => [],
1195 'subjects' => [],
1196 ],
1197 [
1198 // After the 2-week mark, send an email.
1199 'time' => '2012-03-22 01:00:00',
1200 'recipients' => [['test-member@example.com']],
1201 'subjects' => ['subject sched_membership_start_1week (joined March 15th, 2012)'],
1202 ],
1203 ]);
1204 }
1205
1206 /**
1207 * CRM-21675: Support parent and smart group in 'Limit to' field
1208 *
1209 * @throws \CRM_Core_Exception
1210 */
1211 public function testScheduleReminderWithParentGroup() {
1212 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1213 $this->individualCreate(['birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com']);
1214 // Contact B with birth-date at '07-07-2005', later got added in regular group
1215 $contactID2 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'def@test.com'], 1);
1216 // Contact C with birth-date at '07-07-2005', but not included in any group
1217 $this->individualCreate(['birth_date' => '20050707', 'email' => 'ghi@test.com'], 2);
1218
1219 // create regular group and add Contact B to it
1220 $groupID = $this->groupCreate();
1221 $this->callAPISuccess('GroupContact', 'Create', [
1222 'group_id' => $groupID,
1223 'contact_id' => $contactID2,
1224 ]);
1225
1226 // create smart group which will contain all Male contacts
1227 $smartGroupParams = ['form_values' => ['gender_id' => 1]];
1228 $smartGroupID = $this->smartGroupCreate(
1229 $smartGroupParams,
1230 [
1231 'name' => 'new_smart_group',
1232 'title' => 'New Smart Group',
1233 'parents' => [$groupID => 1],
1234 ]
1235 );
1236
1237 $actionScheduleParams = [
1238 'name' => 'sched_contact_bday_yesterday',
1239 'title' => 'sched_contact_bday_yesterday',
1240 'absolute_date' => '',
1241 'body_html' => '<p>you look like you were born yesterday!</p>',
1242 'body_text' => 'you look like you were born yesterday!',
1243 'end_action' => '',
1244 'end_date' => '',
1245 'end_frequency_interval' => '',
1246 'end_frequency_unit' => '',
1247 'entity_status' => 1,
1248 'entity_value' => 'birth_date',
1249 'limit_to' => 1,
1250 'group_id' => $groupID,
1251 'is_active' => 1,
1252 'is_repeat' => '0',
1253 'mapping_id' => 6,
1254 'msg_template_id' => '',
1255 'recipient' => '2',
1256 'recipient_listing' => '',
1257 'recipient_manual' => '',
1258 'record_activity' => 1,
1259 'repetition_frequency_interval' => '',
1260 'repetition_frequency_unit' => '',
1261 'start_action_condition' => 'after',
1262 'start_action_date' => 'date_field',
1263 'start_action_offset' => '1',
1264 'start_action_unit' => 'day',
1265 'subject' => 'subject sched_contact_bday_yesterday',
1266 ];
1267
1268 // Create schedule reminder where parent group ($groupID) is selectd to limit recipients,
1269 // which contain a individual contact - $contactID2 and is parent to smart group.
1270 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionScheduleParams);
1271 $this->assertTrue(is_numeric($actionScheduleDao->id));
1272 $this->assertCronRuns([
1273 [
1274 // On the birthday, no email.
1275 'time' => '2005-07-07 01:00:00',
1276 'recipients' => [],
1277 ],
1278 [
1279 // The next day, send an email.
1280 'time' => '2005-07-08 20:00:00',
1281 'recipients' => [
1282 [
1283 'def@test.com',
1284 ],
1285 [
1286 'abc@test.com',
1287 ],
1288 ],
1289 ],
1290 ]);
1291 $this->groupDelete($smartGroupID);
1292 $this->groupDelete($groupID);
1293 }
1294
1295 /**
1296 * Test end date email sent.
1297 *
1298 * For contacts/members which match schedule based on join date,
1299 * an email should be sent.
1300 *
1301 * @throws \API_Exception
1302 * @throws \Civi\API\Exception\UnauthorizedException
1303 */
1304 public function testMembershipJoinDateNonMatch() {
1305 $this->createMembershipFromFixture('rolling_membership', '', ['email' => 'test-member@example.com']);
1306 // Add an alternative membership type, and only send messages for that type
1307 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', []);
1308 $this->assertInternalType('numeric', $extraMembershipType->id);
1309 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_membership_join_2week']);
1310 $this->assertInternalType('numeric', $actionScheduleDao->id);
1311 $actionScheduleDao->entity_value = $extraMembershipType->id;
1312 $actionScheduleDao->save();
1313
1314 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1315 $this->assertCronRuns([
1316 [
1317 // After the 2-week mark, don't send email because we have different membership type.
1318 'time' => '2012-03-29 01:00:00',
1319 'recipients' => [],
1320 ],
1321 ]);
1322 }
1323
1324 /**
1325 * Test that the first and SECOND notifications are sent out.
1326 *
1327 * @throws \CRM_Core_Exception
1328 */
1329 public function testMembershipEndDateRepeat() {
1330 // creates membership with end_date = 20120615
1331 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1332 $this->callAPISuccess('Email', 'create', [
1333 'contact_id' => $membership->contact_id,
1334 'email' => 'test-member@example.com',
1335 ]);
1336 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1337
1338 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
1339 $actionSchedule['entity_value'] = $membership->membership_type_id;
1340 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1341
1342 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1343 $this->assertCronRuns([
1344 [
1345 // After the 1-month mark, no email
1346 'time' => '2012-07-15 01:00:00',
1347 'recipients' => [],
1348 ],
1349 [
1350 // After the 2-month mark, send an email.
1351 'time' => '2012-08-15 01:00:00',
1352 'recipients' => [['test-member@example.com']],
1353 ],
1354 [
1355 // 4 weeks after first email send first repeat
1356 'time' => '2012-09-12 01:00:00',
1357 'recipients' => [['test-member@example.com']],
1358 ],
1359 [
1360 // 1 week after first repeat send nothing
1361 // There was a bug where the first repeat went out and then
1362 // it would keep going out every cron run. This is to check that's
1363 // not happening.
1364 'time' => '2012-09-19 01:00:00',
1365 'recipients' => [],
1366 ],
1367 [
1368 // 4 weeks after first repeat send second repeat
1369 'time' => '2012-10-10 01:00:00',
1370 'recipients' => [['test-member@example.com']],
1371 ],
1372 [
1373 // 4 months after membership end, send nothing
1374 'time' => '2012-10-15 01:00:00',
1375 'recipients' => [],
1376 ],
1377 [
1378 // 5 months after membership end, send nothing
1379 'time' => '2012-11-15 01:00:00',
1380 'recipients' => [],
1381 ],
1382 ]);
1383 }
1384
1385 /**
1386 * Test behaviour when date changes.
1387 *
1388 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1389 * between
1390 * see CRM-15376
1391 *
1392 * @throws \CRM_Core_Exception
1393 */
1394 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
1395 // creates membership with end_date = 20120615
1396 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1397 $this->callAPISuccess('Email', 'create', [
1398 'contact_id' => $membership->contact_id,
1399 'email' => 'test-member@example.com',
1400 ]);
1401 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1402
1403 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
1404 $actionSchedule['entity_value'] = $membership->membership_type_id;
1405 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1406 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1407 $this->assertCronRuns([
1408 [
1409 // After the 2-week mark, send an email.
1410 'time' => '2012-08-15 01:00:00',
1411 'recipients' => [['test-member@example.com']],
1412 ],
1413 ]);
1414
1415 // Extend membership - reminder should NOT go out.
1416 $this->callAPISuccess('membership', 'create', ['id' => $membership->id, 'end_date' => '2014-01-01']);
1417 $this->assertCronRuns([
1418 [
1419 // After the 2-week mark, send an email.
1420 'time' => '2012-09-12 01:00:00',
1421 'recipients' => [],
1422 ],
1423 ]);
1424 }
1425
1426 /**
1427 * Test membership end date email sends.
1428 *
1429 * For contacts/members which match schedule based on end date,
1430 * an email should be sent.
1431 *
1432 * @throws \API_Exception
1433 * @throws \CRM_Core_Exception
1434 * @throws \Civi\API\Exception\UnauthorizedException
1435 */
1436 public function testMembershipEndDateMatch() {
1437 // creates membership with end_date = 20120615
1438 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1439 $this->callAPISuccess('Email', 'create', [
1440 'contact_id' => $membership->contact_id,
1441 'email' => 'test-member@example.com',
1442 ]);
1443 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1444
1445 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
1446 $actionSchedule['entity_value'] = $membership->membership_type_id;
1447 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1448 $this->assertTrue(is_numeric($actionScheduleDao->id));
1449
1450 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1451 $this->assertCronRuns([
1452 [
1453 // Before the 2-week mark, no email.
1454 'time' => '2012-05-31 01:00:00',
1455 'recipients' => [],
1456 ],
1457 [
1458 // After the 2-week mark, send an email.
1459 'time' => '2012-06-01 01:00:00',
1460 'recipients' => [['test-member@example.com']],
1461 ],
1462 [
1463 // After the email is sent, another one is not sent
1464 'time' => '2012-06-01 02:00:00',
1465 'recipients' => [],
1466 ],
1467 ]);
1468
1469 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1470 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1471 $membership->end_date = '2012-09-15';
1472 $membership->save();
1473
1474 //change the email id of chosen membership contact to assert
1475 //recipient of not the previously sent mail but the new one
1476 $result = $this->callAPISuccess('Email', 'create', [
1477 'is_primary' => 1,
1478 'contact_id' => $membership->contact_id,
1479 'email' => 'member2@example.com',
1480 ]);
1481 $this->assertAPISuccess($result);
1482
1483 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1484 $this->assertCronRuns([
1485 [
1486 // Before the 2-week mark, no email
1487 'time' => '2012-08-31 01:00:00',
1488 'recipients' => [],
1489 ],
1490 [
1491 // After the 2-week mark, send an email
1492 'time' => '2012-09-01 01:00:00',
1493 'recipients' => [['member2@example.com']],
1494 ],
1495 [
1496 // After the email is sent, another one is not sent
1497 'time' => '2012-09-01 02:00:00',
1498 'recipients' => [],
1499 ],
1500 ]);
1501
1502 $membership->end_date = '2012-12-15';
1503 $membership->save();
1504 // end_date=2012-12-15 ; schedule is 2 weeks before end_date
1505 $this->assertCronRuns([
1506 [
1507 // Before the 2-week mark, no email
1508 'time' => '2012-11-30 01:00:00',
1509 'recipients' => [],
1510 ],
1511 [
1512 // After the 2-week mark, send an email
1513 'time' => '2012-12-01 01:00:00',
1514 'recipients' => [['member2@example.com']],
1515 ],
1516 [
1517 // After the email is sent, another one is not sent
1518 'time' => '2012-12-01 02:00:00',
1519 'recipients' => [],
1520 ],
1521 ]);
1522
1523 }
1524
1525 /**
1526 * @param array $contactFixture
1527 * @param int $membershipTypeId
1528 *
1529 * @return array|NULL|object
1530 * @throws \CRM_Core_Exception
1531 */
1532 public function createMembershipAndContact($contactFixture, $membershipTypeId) {
1533 $result = $this->callAPISuccess('contact', 'create', $contactFixture);
1534 $contact = $result['values'][$result['id']];
1535 $params = [
1536 'status_id' => 2,
1537 'contact_id' => $contact['id'],
1538 'membership_type_id' => $membershipTypeId,
1539 'owner_membership_id' => 'NULL',
1540 ];
1541 $params = array_merge($this->fixtures['rolling_membership'], $params);
1542 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $params);
1543 $this->assertInternalType('numeric', $membership->id);
1544 return $membership;
1545 }
1546
1547 /**
1548 * This test is very similar to testMembershipEndDateMatch, but it adds
1549 * another contact because there was a bug in
1550 * RecipientBuilder::buildRelFirstPass where it was only sending the
1551 * reminder for the first contact returned in a query for renewed
1552 * memberships. Other contacts wouldn't get the mail.
1553 *
1554 * @throws \CRM_Core_Exception
1555 */
1556 public function testMultipleMembershipEndDateMatch() {
1557 $membershipTypeId = $this->membershipTypeCreate($this->fixtures['rolling_membership']['membership_type_id']);
1558 $membershipOne = $this->createMembershipAndContact($this->fixtures['contact'], $membershipTypeId);
1559 $membershipTwo = $this->createMembershipAndContact($this->fixtures['contact_2'], $membershipTypeId);
1560 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
1561 $actionSchedule['entity_value'] = $membershipTypeId;
1562 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1563 $this->assertTrue(is_numeric($actionScheduleDao->id));
1564
1565 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1566 $this->assertCronRuns([
1567 [
1568 // Before the 2-week mark, no email.
1569 'time' => '2012-05-31 01:00:00',
1570 'recipients' => [],
1571 ],
1572 [
1573 // After the 2-week mark, send emails.
1574 'time' => '2012-06-01 01:00:00',
1575 'recipients' => [
1576 ['test-member@example.com'],
1577 ['test-contact-2@example.com'],
1578 ],
1579 ],
1580 [
1581 // After the email is sent, another one is not sent
1582 'time' => '2012-06-01 02:00:00',
1583 'recipients' => [],
1584 ],
1585 ]);
1586
1587 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1588 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1589 $membershipOne->end_date = '2012-09-15';
1590 $membershipOne->save();
1591 $membershipTwo->end_date = '2012-09-15';
1592 $membershipTwo->save();
1593
1594 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1595 $this->assertCronRuns([
1596 [
1597 // Before the 2-week mark, no email
1598 'time' => '2012-08-31 01:00:00',
1599 'recipients' => [],
1600 ],
1601 [
1602 // After the 2-week mark, send an email
1603 'time' => '2012-09-01 01:00:00',
1604 'recipients' => [
1605 ['test-member@example.com'],
1606 ['test-contact-2@example.com'],
1607 ],
1608 ],
1609 [
1610 // After the email is sent, another one is not sent
1611 'time' => '2012-06-01 02:00:00',
1612 'recipients' => [],
1613 ],
1614 ]);
1615 }
1616
1617 /**
1618 * Test membership end date email.
1619 *
1620 * For contacts/members which match schedule based on end date,
1621 * an email should be sent.
1622 *
1623 * @throws \CRM_Core_Exception
1624 */
1625 public function testMembershipEndDateNoMatch() {
1626 // creates membership with end_date = 20120615
1627 $membership = $this->createMembershipFromFixture('rolling_membership', 'Grace');
1628 $this->callAPISuccess('Email', 'create', [
1629 'contact_id' => $membership->contact_id,
1630 'email' => 'test-member@example.com',
1631 ]);
1632 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1633
1634 $actionSchedule = $this->fixtures['sched_membership_end_2month'];
1635 $actionSchedule['entity_value'] = $membership->membership_type_id;
1636 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1637 $this->assertTrue(is_numeric($actionScheduleDao->id));
1638
1639 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1640 $this->assertCronRuns([
1641 [
1642 // Before the 2-week mark, no email.
1643 'time' => '2012-05-31 01:00:00',
1644 'recipients' => [],
1645 ],
1646 [
1647 // After the 2-week mark, no email
1648 'time' => '2013-05-01 01:00:00',
1649 'recipients' => [],
1650 ],
1651 ]);
1652 }
1653
1654 /**
1655 * @throws \CRM_Core_Exception
1656 */
1657 public function testContactBirthDateNoAnniv() {
1658 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1659 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1660 $actionSchedule = $this->fixtures['sched_contact_bday_yesterday'];
1661 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1662 $this->assertInternalType('numeric', $actionScheduleDao->id);
1663 $this->assertCronRuns([
1664 [
1665 // On the birthday, no email.
1666 'time' => '2005-07-07 01:00:00',
1667 'recipients' => [],
1668 ],
1669 [
1670 // The next day, send an email.
1671 'time' => '2005-07-08 20:00:00',
1672 'recipients' => [['test-bday@example.com']],
1673 ],
1674 ]);
1675 }
1676
1677 /**
1678 * @throws \CRM_Core_Exception
1679 */
1680 public function testContactBirthDateAnniversary() {
1681 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1682 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1683 $actionSchedule = $this->fixtures['sched_contact_bday_anniv'];
1684 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1685 $this->assertTrue(is_numeric($actionScheduleDao->id));
1686 $this->assertCronRuns([
1687 [
1688 // On some random day, no email.
1689 'time' => '2014-03-07 01:00:00',
1690 'recipients' => [],
1691 ],
1692 [
1693 // On the eve of their 9th birthday, send an email.
1694 'time' => '2014-07-06 20:00:00',
1695 'recipients' => [['test-bday@example.com']],
1696 ],
1697 ]);
1698 }
1699
1700 /**
1701 * @throws \CRM_Core_Exception
1702 */
1703 public function testContactCustomDateNoAnniversary() {
1704 $group = [
1705 'title' => 'Test_Group',
1706 'name' => 'test_group',
1707 'extends' => ['Individual'],
1708 'style' => 'Inline',
1709 'is_multiple' => FALSE,
1710 'is_active' => 1,
1711 ];
1712 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1713 $field = [
1714 'label' => 'Graduation',
1715 'data_type' => 'Date',
1716 'html_type' => 'Select Date',
1717 'custom_group_id' => $createGroup['id'],
1718 ];
1719 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1720 $contactParams = $this->fixtures['contact'];
1721 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1722 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1723 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1724 $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow'];
1725 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1726 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1727 $this->assertTrue(is_numeric($actionScheduleDao->id));
1728 $this->assertCronRuns([
1729 [
1730 // On some random day, no email.
1731 'time' => '2014-03-07 01:00:00',
1732 'recipients' => [],
1733 ],
1734 [
1735 // On the eve of their graduation, send an email.
1736 'time' => '2013-12-15 20:00:00',
1737 'recipients' => [['test-member@example.com']],
1738 ],
1739 ]);
1740 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1741 }
1742
1743 /**
1744 * @throws \CRM_Core_Exception
1745 */
1746 public function testContactCreatedNoAnniversary() {
1747 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1748 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1749 $this->createScheduleFromFixtures('sched_contact_created_yesterday');
1750 $this->assertCronRuns([
1751 [
1752 // On the date created, no email.
1753 'time' => $contact['values'][$contact['id']]['created_date'],
1754 'recipients' => [],
1755 ],
1756 [
1757 // The next day, send an email.
1758 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1759 'recipients' => [['test-bday@example.com']],
1760 ],
1761 ]);
1762 }
1763
1764 /**
1765 * Test the impact of changing the anniversary.
1766 *
1767 * @throws \CRM_Core_Exception
1768 */
1769 public function testContactModifiedAnniversary() {
1770 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1771 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1772 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', ['id' => $contact['id'], 'return' => 'modified_date']);
1773 $actionSchedule = $this->fixtures['sched_contact_mod_anniv'];
1774 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1775 $this->assertInternalType('numeric', $actionScheduleDao->id);
1776 $this->assertCronRuns([
1777 [
1778 // On some random day, no email.
1779 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1780 'recipients' => [],
1781 ],
1782 [
1783 // On the eve of 3 years after they were modified, send an email.
1784 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1785 'recipients' => [['test-bday@example.com']],
1786 ],
1787 ]);
1788 }
1789
1790 /**
1791 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
1792 *
1793 * @throws \API_Exception
1794 * @throws \CRM_Core_Exception
1795 * @throws \Civi\API\Exception\UnauthorizedException
1796 */
1797 public function testMembershipLimitToNone() {
1798 // creates membership with end_date = 20120615
1799 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1800 $result = $this->callAPISuccess('Email', 'create', [
1801 'contact_id' => $membership->contact_id,
1802 'email' => 'member@example.com',
1803 ]);
1804 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1805 $this->callAPISuccess('contact', 'create', ['email' => 'b@c.com', 'contact_type' => 'Individual']);
1806
1807 $this->assertAPISuccess($result);
1808
1809 $actionSchedule = $this->fixtures['sched_membership_end_limit_to_none'];
1810 $actionSchedule['entity_value'] = $membership->membership_type_id;
1811 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1812 $this->assertTrue(is_numeric($actionScheduleDao->id));
1813
1814 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1815 $this->assertCronRuns([
1816 [
1817 // Before the 2-week mark, no email.
1818 'time' => '2012-05-31 01:00:00',
1819 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1820 'recipients' => [],
1821 ],
1822 ]);
1823 }
1824
1825 /**
1826 * Test handling of reference date for memberships.
1827 *
1828 * @throws \API_Exception
1829 * @throws \CRM_Core_Exception
1830 * @throws \Civi\API\Exception\UnauthorizedException
1831 */
1832 public function testMembershipWithReferenceDate() {
1833 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1834 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1835
1836 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
1837 $actionSchedule['entity_value'] = $membership->membership_type_id;
1838 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1839 $this->assertInternalType('numeric', $actionScheduleDao->id);
1840
1841 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1842 $this->assertCronRuns([
1843 [
1844 // After the 2-week mark, send an email
1845 'time' => '2012-03-29 01:00:00',
1846 'recipients' => [['member@example.com']],
1847 ],
1848 [
1849 // After the 2-week 1day mark, don't send an email
1850 'time' => '2012-03-30 01:00:00',
1851 'recipients' => [],
1852 ],
1853 ]);
1854
1855 //check if reference date is set to membership's join date
1856 //as per the action_start_date chosen for current schedule reminder
1857 $this->assertEquals('2012-03-15 00:00:00',
1858 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id')
1859 );
1860
1861 //change current membership join date that may signifies as membership renewal activity
1862 $membership->join_date = '2012-03-29';
1863 $membership->save();
1864
1865 $this->assertCronRuns([
1866 [
1867 // After the 13 days of the changed join date 2012-03-29, don't send an email
1868 'time' => '2012-04-11 01:00:00',
1869 'recipients' => [],
1870 ],
1871 [
1872 // After the 2-week of the changed join date 2012-03-29, send an email
1873 'time' => '2012-04-12 01:00:00',
1874 'recipients' => [['member@example.com']],
1875 ],
1876 ]);
1877 $this->assertCronRuns([
1878 [
1879 // It should not re-send on the same day
1880 'time' => '2012-04-12 01:00:00',
1881 'recipients' => [],
1882 ],
1883 ]);
1884 }
1885
1886 /**
1887 * Test multiple membership reminder.
1888 *
1889 * @throws \API_Exception
1890 * @throws \CRM_Core_Exception
1891 * @throws \Civi\API\Exception\UnauthorizedException
1892 */
1893 public function testMembershipOnMultipleReminder() {
1894 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1895 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
1896
1897 // Send email 2 weeks before end_date
1898 $actionScheduleBefore = $this->fixtures['sched_membership_end_2week'];
1899 // Send email on end_date/expiry date
1900 $actionScheduleOn = $this->fixtures['sched_on_membership_end_date'];
1901 // Send email 1 day after end_date/grace period
1902 $actionScheduleAfter = $this->fixtures['sched_after_1day_membership_end_date'];
1903 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id;
1904 foreach (['actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter'] as $value) {
1905 $$value = CRM_Core_BAO_ActionSchedule::add($$value);
1906 $this->assertInternalType('numeric', $$value->id);
1907 }
1908
1909 $this->assertCronRuns(
1910 [
1911 [
1912 // 1day 2weeks before membership end date(MED), don't send mail
1913 'time' => '2012-05-31 01:00:00',
1914 'recipients' => [],
1915 ],
1916 [
1917 // 2 weeks before MED, send an email
1918 'time' => '2012-06-01 01:00:00',
1919 'recipients' => [['member@example.com']],
1920 ],
1921 [
1922 // 1day before MED, don't send mail
1923 'time' => '2012-06-14 01:00:00',
1924 'recipients' => [],
1925 ],
1926 [
1927 // On MED, send an email
1928 'time' => '2012-06-15 00:00:00',
1929 'recipients' => [['member@example.com']],
1930 ],
1931 [
1932 // After 1day of MED, send an email
1933 'time' => '2012-06-16 01:00:00',
1934 'recipients' => [['member@example.com']],
1935 ],
1936 [
1937 // After 1day 1min of MED, don't send an email
1938 'time' => '2012-06-17 00:01:00',
1939 'recipients' => [],
1940 ],
1941 ]
1942 );
1943
1944 // Assert the timestamp as of when the emails of respective three reminders as configured
1945 // 2 weeks before, on and 1 day after MED, are sent
1946 $this->assertApproxEquals(
1947 strtotime('2012-06-01 01:00:00'),
1948 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id, 'action_date_time', 'action_schedule_id', TRUE)),
1949 // Variation in test execution time.
1950 3
1951 );
1952 $this->assertApproxEquals(
1953 strtotime('2012-06-15 00:00:00'),
1954 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id, 'action_date_time', 'action_schedule_id', TRUE)),
1955 // Variation in test execution time.
1956 3
1957 );
1958 $this->assertApproxEquals(
1959 strtotime('2012-06-16 01:00:00'),
1960 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id, 'action_date_time', 'action_schedule_id', TRUE)),
1961 // Variation in test execution time.
1962 3
1963 );
1964
1965 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1966 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1967 $membership->end_date = '2012-06-20';
1968 $membership->save();
1969
1970 $this->callAPISuccess('Contact', 'get', ['id' => $membership->contact_id]);
1971 $this->assertCronRuns(
1972 [
1973 [
1974 // 1day 2weeks before membership end date(MED), don't send mail
1975 'time' => '2012-06-05 01:00:00',
1976 'recipients' => [],
1977 ],
1978 [
1979 // 2 weeks before MED, send an email
1980 'time' => '2012-06-06 01:00:00',
1981 'recipients' => [['member@example.com']],
1982 ],
1983 [
1984 // 1day before MED, don't send mail
1985 'time' => '2012-06-19 01:00:00',
1986 'recipients' => [],
1987 ],
1988 [
1989 // On MED, send an email
1990 'time' => '2012-06-20 00:00:00',
1991 'recipients' => [['member@example.com']],
1992 ],
1993 [
1994 // After 1day of MED, send an email
1995 'time' => '2012-06-21 01:00:00',
1996 'recipients' => [['member@example.com']],
1997 ],
1998 [
1999 // After 1day 1min of MED, don't send an email
2000 'time' => '2012-07-21 00:01:00',
2001 'recipients' => [],
2002 ],
2003 ]);
2004 }
2005
2006 /**
2007 * Test reminders sent on custom data anniversary.
2008 *
2009 * @throws \CRM_Core_Exception
2010 */
2011 public function testContactCustomDate_Anniversary() {
2012 $group = [
2013 'title' => 'Test_Group now',
2014 'name' => 'test_group_now',
2015 'extends' => ['Individual'],
2016 'style' => 'Inline',
2017 'is_multiple' => FALSE,
2018 'is_active' => 1,
2019 ];
2020 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
2021 $field = [
2022 'label' => 'Graduation',
2023 'data_type' => 'Date',
2024 'html_type' => 'Select Date',
2025 'custom_group_id' => $createGroup['id'],
2026 ];
2027 $createField = $this->callAPISuccess('custom_field', 'create', $field);
2028
2029 $contactParams = $this->fixtures['contact'];
2030 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
2031 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
2032 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
2033 $actionSchedule = $this->fixtures['sched_contact_grad_anniv'];
2034 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
2035 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
2036 $this->assertTrue(is_numeric($actionScheduleDao->id));
2037 $this->assertCronRuns([
2038 [
2039 // On some random day, no email.
2040 'time' => '2014-03-07 01:00:00',
2041 'recipients' => [],
2042 ],
2043 [
2044 // A week after their 5th anniversary of graduation, send an email.
2045 'time' => '2018-12-23 20:00:00',
2046 'recipients' => [['test-member@example.com']],
2047 ],
2048 ]);
2049 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
2050 }
2051
2052 /**
2053 * Test sched reminder set via registration date.
2054 *
2055 * @throws \CRM_Core_Exception
2056 */
2057 public function testEventTypeRegistrationDate() {
2058 $contact = $this->individualCreate(['email' => 'test-event@example.com']);
2059 //Add it as a participant to an event ending registration - 7 days from now.
2060 $params = [
2061 'start_date' => date('Ymd', strtotime('-5 day')),
2062 'end_date' => date('Ymd', strtotime('+7 day')),
2063 'registration_start_date' => date('Ymd', strtotime('-5 day')),
2064 'registration_end_date' => date('Ymd', strtotime('+7 day')),
2065 ];
2066 $event = $this->eventCreate($params);
2067 $this->participantCreate(['contact_id' => $contact, 'event_id' => $event['id']]);
2068
2069 //Create a scheduled reminder to send email 7 days before registration date.
2070 $actionSchedule = $this->fixtures['sched_eventtype_start_1week_before'];
2071 $actionSchedule['start_action_offset'] = 7;
2072 $actionSchedule['start_action_unit'] = 'day';
2073 $actionSchedule['start_action_date'] = 'registration_end_date';
2074 $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id'];
2075 $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', [
2076 'return' => 'id',
2077 'name' => 'Attended',
2078 ]);
2079 $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2080 //Run the cron and verify if an email was sent.
2081 $this->assertCronRuns([
2082 [
2083 'time' => date('Y-m-d'),
2084 'recipients' => [['test-event@example.com']],
2085 ],
2086 ]);
2087
2088 //Create contact 2
2089 $contactParams = [
2090 'email' => 'test-event2@example.com',
2091 ];
2092 $contact2 = $this->individualCreate($contactParams);
2093 //Create an event with registration end date = 2 week from now.
2094 $params['end_date'] = date('Ymd', strtotime('+2 week'));
2095 $params['registration_end_date'] = date('Ymd', strtotime('+2 week'));
2096 $event2 = $this->eventCreate($params);
2097 $this->participantCreate(['contact_id' => $contact2, 'event_id' => $event2['id']]);
2098
2099 //Assert there is no reminder sent to the contact.
2100 $this->assertCronRuns([
2101 [
2102 'time' => date('Y-m-d'),
2103 'recipients' => [],
2104 ],
2105 ]);
2106
2107 //Modify the sched reminder to be sent 2 week from registration end date.
2108 $this->callAPISuccess('action_schedule', 'create', [
2109 'id' => $actionSched['id'],
2110 'start_action_offset' => 2,
2111 'start_action_unit' => 'week',
2112 ]);
2113
2114 //Contact should receive the reminder now.
2115 $this->assertCronRuns([
2116 [
2117 'time' => date('Y-m-d'),
2118 'recipients' => [['test-event2@example.com']],
2119 ],
2120 ]);
2121 }
2122
2123 /**
2124 * Test sched reminder set via start date.
2125 *
2126 * @throws \CRM_Core_Exception
2127 */
2128 public function testEventTypeStartDate() {
2129 // Create event+participant with start_date = 20120315, end_date = 20120615.
2130 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], ['status_id' => 2]));
2131 $this->callAPISuccess('Email', 'create', [
2132 'contact_id' => $participant->contact_id,
2133 'email' => 'test-event@example.com',
2134 ]);
2135 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $participant->contact_id]));
2136
2137 $actionSchedule = $this->fixtures['sched_eventtype_start_1week_before'];
2138 $actionSchedule['entity_value'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'event_type_id');
2139 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2140
2141 //echo "CREATED\n"; ob_flush(); sleep(20);
2142
2143 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2144 $this->assertCronRuns([
2145 [
2146 // 2 weeks before
2147 'time' => '2012-03-02 01:00:00',
2148 'recipients' => [],
2149 ],
2150 [
2151 // 1 week before
2152 'time' => '2012-03-08 01:00:00',
2153 'recipients' => [['test-event@example.com']],
2154 ],
2155 [
2156 // And then nothing else
2157 'time' => '2012-03-16 01:00:00',
2158 'recipients' => [],
2159 ],
2160 ]);
2161 }
2162
2163 /**
2164 * Test schedule on event end date.
2165 *
2166 * @throws \CRM_Core_Exception
2167 */
2168 public function testEventTypeEndDateRepeat() {
2169 // Create event+participant with start_date = 20120315, end_date = 20120615.
2170 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], ['status_id' => 2]));
2171 $this->callAPISuccess('Email', 'create', [
2172 'contact_id' => $participant->contact_id,
2173 'email' => 'test-event@example.com',
2174 ]);
2175 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $participant->contact_id]));
2176
2177 $actionSchedule = $this->fixtures['sched_eventtype_end_2month_repeat_twice_2_weeks'];
2178 $actionSchedule['entity_value'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'event_type_id');
2179 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2180
2181 $this->assertCronRuns([
2182 [
2183 // Almost 2 months.
2184 'time' => '2012-08-13 01:00:00',
2185 'recipients' => [],
2186 ],
2187 [
2188 // After the 2-month mark, send an email.
2189 'time' => '2012-08-16 01:00:00',
2190 'recipients' => [['test-event@example.com']],
2191 ],
2192 [
2193 // After 2 months and 1 week, don't repeat yet.
2194 'time' => '2012-08-23 02:00:00',
2195 'recipients' => [],
2196 ],
2197 [
2198 // After 2 months and 2 weeks
2199 'time' => '2012-08-30 02:00:00',
2200 'recipients' => [['test-event@example.com']],
2201 ],
2202 [
2203 // After 2 months and 4 week
2204 'time' => '2012-09-13 02:00:00',
2205 'recipients' => [['test-event@example.com']],
2206 ],
2207 [
2208 // After 2 months and 6 weeks
2209 'time' => '2012-09-27 01:00:00',
2210 'recipients' => [],
2211 ],
2212 ]);
2213 }
2214
2215 // TODO // function testMembershipEndDate_NonMatch() { }
2216 // TODO // function testEventTypeStartDate_Match() { }
2217 // TODO // function testEventTypeEndDate_Match() { }
2218 // TODO // function testEventNameStartDate_Match() { }
2219 // TODO // function testEventNameEndDate_Match() { }
2220
2221 /**
2222 * Run a series of cron jobs and make an assertion about email deliveries.
2223 *
2224 * @param array $cronRuns
2225 * array specifying when to run cron and what messages to expect; each item is an array with keys:
2226 * - time: string, e.g. '2012-06-15 21:00:01'
2227 * - recipients: array(array(string)), list of email addresses which should receive messages
2228 *
2229 * @throws \CRM_Core_Exception
2230 */
2231 public function assertCronRuns($cronRuns) {
2232 foreach ($cronRuns as $cronRun) {
2233 CRM_Utils_Time::setTime($cronRun['time']);
2234 $this->callAPISuccess('job', 'send_reminder', []);
2235 $this->mut->assertRecipients($cronRun['recipients']);
2236 if (array_key_exists('subjects', $cronRun)) {
2237 $this->mut->assertSubjects($cronRun['subjects']);
2238 }
2239 $this->mut->clearMessages();
2240 }
2241 }
2242
2243 /**
2244 * @var array
2245 * (DAO_Name => array(int)) List of items to garbage-collect during tearDown
2246 */
2247 private $_testObjects;
2248
2249 /**
2250 * Sets up the fixture, for example, opens a network connection.
2251 *
2252 * This method is called before a test is executed.
2253 */
2254 protected function _setUp() {
2255 $this->_testObjects = [];
2256 }
2257
2258 /**
2259 * Tears down the fixture, for example, closes a network connection.
2260 *
2261 * This method is called after a test is executed.
2262 */
2263 protected function _tearDown() {
2264 parent::tearDown();
2265 $this->deleteTestObjects();
2266 }
2267
2268 /**
2269 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
2270 * created entities and provides for brainless cleanup.
2271 *
2272 * @see CRM_Core_DAO::createTestObject
2273 *
2274 * @param $daoName
2275 * @param array $params
2276 * @param int $numObjects
2277 * @param bool $createOnly
2278 *
2279 * @return array|NULL|object
2280 */
2281 public function createTestObject($daoName, $params = [], $numObjects = 1, $createOnly = FALSE) {
2282 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
2283 if (is_array($objects)) {
2284 $this->registerTestObjects($objects);
2285 }
2286 else {
2287 $this->registerTestObjects([$objects]);
2288 }
2289 return $objects;
2290 }
2291
2292 /**
2293 * @param array $objects
2294 * DAO or BAO objects.
2295 */
2296 public function registerTestObjects($objects) {
2297 //if (is_object($objects)) {
2298 // $objects = array($objects);
2299 //}
2300 foreach ($objects as $object) {
2301 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
2302 $this->_testObjects[$daoName][] = $object->id;
2303 }
2304 }
2305
2306 public function deleteTestObjects() {
2307 // Note: You might argue that the FK relations between test
2308 // objects could make this problematic; however, it should
2309 // behave intuitively as long as we mentally split our
2310 // test-objects between the "manual/primary records"
2311 // and the "automatic/secondary records"
2312 foreach ($this->_testObjects as $daoName => $daoIds) {
2313 foreach ($daoIds as $daoId) {
2314 CRM_Core_DAO::deleteTestObjects($daoName, ['id' => $daoId]);
2315 }
2316 }
2317 $this->_testObjects = [];
2318 }
2319
2320 /**
2321 * Test that the various repetition units work correctly.
2322 * CRM-17028
2323 */
2324 public function testRepetitionFrequencyUnit() {
2325 $membershipTypeParams = [
2326 'duration_interval' => '1',
2327 'duration_unit' => 'year',
2328 'is_active' => 1,
2329 'period_type' => 'rolling',
2330 ];
2331 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
2332 $interval_units = ['hour', 'day', 'week', 'month', 'year'];
2333 foreach ($interval_units as $interval_unit) {
2334 $membershipEndDate = DateTime::createFromFormat('Y-m-d H:i:s', "2013-03-15 00:00:00");
2335 $contactParams = [
2336 'contact_type' => 'Individual',
2337 'first_name' => 'Test',
2338 'last_name' => "Interval $interval_unit",
2339 'is_deceased' => 0,
2340 ];
2341 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2342 $this->assertTrue(is_numeric($contact->id));
2343 $emailParams = [
2344 'contact_id' => $contact->id,
2345 'is_primary' => 1,
2346 'email' => "test-member-{$interval_unit}@example.com",
2347 'location_type_id' => 1,
2348 ];
2349 $email = $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2350 $this->assertTrue(is_numeric($email->id));
2351 $membershipParams = [
2352 'membership_type_id' => $membershipType->id,
2353 'contact_id' => $contact->id,
2354 'join_date' => '20120315',
2355 'start_date' => '20120315',
2356 'end_date' => '20130315',
2357 'is_override' => 0,
2358 'status_id' => 2,
2359 ];
2360 $membershipParams['status-id'] = 1;
2361 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2362 $actionScheduleParams = $this->fixtures['sched_on_membership_end_date_repeat_interval'];
2363 $actionScheduleParams['entity_value'] = $membershipType->id;
2364 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
2365 $actionScheduleParams['repetition_frequency_interval'] = 2;
2366 $actionSchedule = CRM_Core_BAO_ActionSchedule::add($actionScheduleParams);
2367 $this->assertTrue(is_numeric($actionSchedule->id));
2368 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
2369 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
2370 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
2371 $cronRuns = [
2372 [
2373 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
2374 'recipients' => [],
2375 ],
2376 [
2377 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
2378 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2379 ],
2380 [
2381 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
2382 'recipients' => [],
2383 ],
2384 [
2385 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
2386 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2387 ],
2388 ];
2389 $this->assertCronRuns($cronRuns);
2390 $actionSchedule->delete();
2391 $membership->delete();
2392 }
2393 }
2394
2395 /**
2396 * Inherited members without permission to edit the main member contact should
2397 * not get reminders.
2398 *
2399 * However, just because a contact inherits one membership doesn't mean
2400 * reminders for other memberships should be suppressed.
2401 *
2402 * See CRM-14098
2403 *
2404 * @throws \CRM_Core_Exception
2405 */
2406 public function testInheritedMembershipPermissions() {
2407 // Set up common parameters for memberships.
2408 $membershipParams = $this->fixtures['rolling_membership'];
2409 $membershipParams['status_id'] = 1;
2410
2411 $membershipParams['membership_type_id']['relationship_type_id'] = 1;
2412 $membershipParams['membership_type_id']['relationship_direction'] = 'b_a';
2413 $membershipType1 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2414
2415 // We'll create a new membership type that can be held at the same time as
2416 // the first one.
2417 $membershipParams['membership_type_id']['relationship_type_id'] = 'NULL';
2418 $membershipParams['membership_type_id']['relationship_direction'] = 'NULL';
2419 $membershipType2 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2420
2421 // Create the parent membership and contact
2422 $membershipParams['membership_type_id'] = $membershipType1->id;
2423 $mainMembership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2424
2425 $contactParams = [
2426 'contact_type' => 'Individual',
2427 'first_name' => 'Mom',
2428 'last_name' => 'Rel',
2429 'is_deceased' => 0,
2430 ];
2431 $this->createTestObject('CRM_Contact_DAO_Contact', array_merge($contactParams, ['id' => $mainMembership->contact_id]));
2432
2433 $emailParams = [
2434 'contact_id' => $mainMembership->contact_id,
2435 'email' => 'test-member@example.com',
2436 'location_type_id' => 1,
2437 'is_primary' => 1,
2438 ];
2439 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2440
2441 // Set up contacts and emails for the two children
2442 $contactParams['first_name'] = 'Favorite';
2443 $permChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2444 $emailParams['email'] = 'favorite@example.com';
2445 $emailParams['contact_id'] = $permChild->id;
2446 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2447
2448 $contactParams['first_name'] = 'Black Sheep';
2449 $nonPermChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2450 $emailParams['email'] = 'black.sheep@example.com';
2451 $emailParams['contact_id'] = $nonPermChild->id;
2452 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2453
2454 // Each child gets a relationship, one with permission to edit the parent. This
2455 // will trigger inherited memberships for the first membership type
2456 $relParams = [
2457 'relationship_type_id' => 1,
2458 'contact_id_a' => $nonPermChild->id,
2459 'contact_id_b' => $mainMembership->contact_id,
2460 'is_active' => 1,
2461 ];
2462 $this->callAPISuccess('relationship', 'create', $relParams);
2463
2464 $relParams['contact_id_a'] = $permChild->id;
2465 $relParams['is_permission_a_b'] = CRM_Contact_BAO_Relationship::EDIT;
2466 $this->callAPISuccess('relationship', 'create', $relParams);
2467
2468 // Mom and Black Sheep get their own memberships of the second type.
2469 $membershipParams['membership_type_id'] = $membershipType2->id;
2470 $membershipParams['owner_membership_id'] = 'NULL';
2471 $membershipParams['contact_id'] = $mainMembership->contact_id;
2472 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2473
2474 $membershipParams['contact_id'] = $nonPermChild->id;
2475 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2476
2477 // Test a reminder for the first membership type - that should exclude Black
2478 // Sheep.
2479 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
2480 $actionSchedule['entity_value'] = $membershipType1->id;
2481 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
2482 $this->assertTrue(is_numeric($actionScheduleDao->id));
2483
2484 $this->assertCronRuns([
2485 [
2486 'time' => '2012-03-29 01:00:00',
2487 'recipients' => [['test-member@example.com'], ['favorite@example.com']],
2488 'subjects' => [
2489 'subject sched_membership_join_2week (joined March 15th, 2012)',
2490 'subject sched_membership_join_2week (joined March 15th, 2012)',
2491 ],
2492 ],
2493 ]);
2494
2495 // Test a reminder for the second membership type - that should include
2496 // Black Sheep.
2497 $actionSchedule = $this->fixtures['sched_membership_start_1week'];
2498 $actionSchedule['entity_value'] = $membershipType2->id;
2499 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
2500 $this->assertInternalType('numeric', $actionScheduleDao->id);
2501
2502 $this->assertCronRuns([
2503 [
2504 'time' => '2012-03-22 01:00:00',
2505 'recipients' => [['test-member@example.com'], ['black.sheep@example.com']],
2506 'subjects' => [
2507 'subject sched_membership_start_1week (joined March 15th, 2012)',
2508 'subject sched_membership_start_1week (joined March 15th, 2012)',
2509 ],
2510 ],
2511 ]);
2512 }
2513
2514 /**
2515 * Modify the date time by the modify rule.
2516 *
2517 * @param DateTime $origDateTime
2518 * @param string $modifyRule
2519 *
2520 * @return DateTime
2521 */
2522 public function createModifiedDateTime($origDateTime, $modifyRule) {
2523 $newDateTime = clone($origDateTime);
2524 $newDateTime->modify($modifyRule);
2525 return $newDateTime;
2526 }
2527
2528 /**
2529 * Test absolute date handling for membership.
2530 *
2531 * @throws \CRM_Core_Exception
2532 */
2533 public function testMembershipScheduleWithAbsoluteDate() {
2534 $membership = $this->createMembershipFromFixture('rolling_membership', 'New', [
2535 'email' => 'test-member@example.com',
2536 'location_type_id' => 1,
2537 ]);
2538
2539 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], ['contact_id' => $membership->contact_id]));
2540 $actionSchedule = $this->fixtures['sched_membership_absolute_date'];
2541 $actionSchedule['entity_value'] = $membership->membership_type_id;
2542 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
2543 $this->assertInternalType('numeric', $actionScheduleDao->id);
2544
2545 $this->assertCronRuns([
2546 [
2547 // Before the 24-hour mark, no email
2548 'time' => '2012-06-13 04:00:00',
2549 'recipients' => [],
2550 'subjects' => [],
2551 ],
2552 [
2553 // On absolute date set on 2012-06-14
2554 'time' => '2012-06-14 00:00:00',
2555 'recipients' => [['test-member@example.com']],
2556 'subjects' => ['subject sched_membership_absolute_date'],
2557 ],
2558 [
2559 // Run cron 4 hours later; first message already sent
2560 'time' => '2012-06-14 04:00:00',
2561 'recipients' => [],
2562 'subjects' => [],
2563 ],
2564 ]);
2565 }
2566
2567 /**
2568 * @param string $fixture
2569 * Key from $this->fixtures
2570 * @param string $status
2571 * Membership status
2572 * @param array $emailParams
2573 *
2574 * @return \CRM_Member_DAO_Membership
2575 * @throws \API_Exception
2576 * @throws \Civi\API\Exception\UnauthorizedException
2577 */
2578 protected function createMembershipFromFixture($fixture, $status, $emailParams = []) {
2579 /* @var CRM_Member_DAO_Membership $membership */
2580 $membership = $this->createTestObject(
2581 'CRM_Member_DAO_Membership',
2582 array_merge($this->fixtures[$fixture], ['status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', $status)])
2583 );
2584 $this->assertInternalType('numeric', $membership->id);
2585 if ($emailParams) {
2586 Civi\Api4\Email::create(FALSE)->setValues(array_merge([
2587 'contact_id' => $membership->contact_id,
2588 'location_type_id' => 1,
2589 ], $emailParams))->execute();
2590 }
2591 return $membership;
2592 }
2593
2594 /**
2595 * Create action schedule from defined fixtures.
2596 *
2597 * @param string $fixture
2598 */
2599 protected function createScheduleFromFixtures($fixture) {
2600 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures[$fixture]);
2601 $this->assertInternalType('numeric', $actionScheduleDao->id);
2602 }
2603
2604 }