3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 use Civi\Api4\Activity
;
13 use Civi\Api4\ActivityContact
;
14 use Civi\Api4\MembershipType
;
17 * Class CRM_Core_BAO_ActionScheduleTest.
19 * @group ActionSchedule
22 * There are additional tests for some specific entities in other classes:
23 * @see CRM_Activity_ActionMappingTest
24 * @see CRM_Contribute_ActionMapping_ByTypeTest
26 class CRM_Core_BAO_ActionScheduleTest
extends CiviUnitTestCase
{
28 use CRMTraits_Custom_CustomDataTrait
;
36 * Entities set up for the test.
40 private $fixtures = [];
43 * Generic usable membership type id.
45 * These should pre-exist but something is deleting them.
49 protected $membershipTypeID;
54 * @throws CRM_Core_Exception
56 public function setUp(): void
{
59 $this->mut
= new CiviMailUtils($this, TRUE);
61 $this->fixtures
['rolling_membership_type'] = [
62 'period_type' => 'rolling',
63 'duration_unit' => 'month',
64 'duration_interval' => '3',
67 'financial_type_id' => 2,
70 $this->fixtures
['rolling_membership'] = [
71 'membership_type_id' => [
72 'period_type' => 'rolling',
73 'duration_unit' => 'month',
74 'duration_interval' => '3',
77 'join_date' => '20120315',
78 'start_date' => '20120315',
79 'end_date' => '20120615',
83 $this->fixtures
['rolling_membership_past'] = [
84 'membership_type_id' => [
85 'period_type' => 'rolling',
86 'duration_unit' => 'month',
87 'duration_interval' => '3',
90 'join_date' => '20100310',
91 'start_date' => '20100310',
92 'end_date' => '20100610',
93 'is_override' => 'NULL',
95 $this->fixtures
['participant'] = [
99 'title' => 'Example Event',
100 'start_date' => '20120315',
101 'end_date' => '20120615',
109 $this->fixtures
['phone_call'] = [
111 'activity_type_id' => 2,
112 'activity_date_time' => '20120615100000',
113 'is_current_revision' => 1,
115 'subject' => 'Phone call',
116 'details' => 'A phone call about a bear',
118 $this->fixtures
['contact'] = [
120 'contact_type' => 'Individual',
121 'email' => 'test-member@example.com',
122 'gender_id' => 'Female',
123 'first_name' => 'Churmondleia',
124 'last_name' => 'Ōtākou',
126 $this->fixtures
['contact_2'] = [
128 'contact_type' => 'Individual',
129 'email' => 'test-contact-2@example.com',
130 'gender_id' => 'Male',
131 'first_name' => 'Fabio',
134 $this->fixtures
['contact_birthdate'] = [
136 'contact_type' => 'Individual',
137 'email' => 'test-birth_day@example.com',
138 'birth_date' => '20050707',
140 $this->fixtures
['sched_activity_1day'] = [
141 'name' => 'One_Day_Phone_Call_Notice',
142 'title' => 'One Day Phone Call Notice',
144 'absolute_date' => NULL,
145 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
146 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
147 'end_action' => NULL,
149 'end_frequency_interval' => NULL,
150 'end_frequency_unit' => NULL,
151 'entity_status' => '1',
152 'entity_value' => '2',
157 'msg_template_id' => NULL,
159 'recipient_listing' => NULL,
160 'recipient_manual' => NULL,
161 'record_activity' => 1,
162 'repetition_frequency_interval' => NULL,
163 'repetition_frequency_unit' => NULL,
164 'start_action_condition' => 'before',
165 'start_action_date' => 'activity_date_time',
166 'start_action_offset' => '1',
167 'start_action_unit' => 'day',
168 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
169 'effective_start_date' => '2012-06-14 00:00:00',
170 'effective_end_date' => '2012-06-15 00:00:00',
172 $this->fixtures
['sched_activity_5minute'] = [
173 'name' => 'Five_Minute_Phone_Call_Notice',
174 'title' => 'Five Minute Phone Call Notice',
176 'absolute_date' => NULL,
177 'body_html' => '<p>5 minutes (for {activity.subject})</p>',
178 'body_text' => '5 minutes (non-repeating) (for {activity.subject})',
179 'end_action' => NULL,
181 'end_frequency_interval' => NULL,
182 'end_frequency_unit' => NULL,
183 'entity_status' => '1',
184 'entity_value' => '2',
189 'msg_template_id' => NULL,
191 'recipient_listing' => NULL,
192 'recipient_manual' => NULL,
193 'record_activity' => 1,
194 'repetition_frequency_interval' => NULL,
195 'repetition_frequency_unit' => NULL,
196 'start_action_condition' => 'before',
197 'start_action_date' => 'activity_date_time',
198 'start_action_offset' => '5',
199 'start_action_unit' => 'minute',
200 'subject' => '5 minutes (about {activity.activity_type})',
202 $this->fixtures
['sched_activity_1day_r'] = [
203 'name' => 'One_Day_Phone_Call_Notice_R',
204 'title' => 'One Day Phone Call Notice R',
206 'absolute_date' => NULL,
207 'body_html' => '<p>1-Day (repeating)</p>',
208 'body_text' => '1-Day (repeating)',
209 'end_action' => 'after',
210 'end_date' => 'activity_date_time',
211 'end_frequency_interval' => '2',
212 'end_frequency_unit' => 'day',
213 'entity_status' => '1',
214 'entity_value' => '2',
219 'msg_template_id' => NULL,
221 'recipient_listing' => NULL,
222 'recipient_manual' => NULL,
223 'record_activity' => NULL,
224 'repetition_frequency_interval' => '6',
225 'repetition_frequency_unit' => 'hour',
226 'start_action_condition' => 'before',
227 'start_action_date' => 'activity_date_time',
228 'start_action_offset' => '1',
229 'start_action_unit' => 'day',
230 'subject' => '1-Day (repeating) (about {activity.activity_type})',
231 'effective_end_date' => '2012-06-14 16:00:00',
233 $this->fixtures
['sched_activity_1day_r_on_abs_date'] = [
234 'name' => 'One_Day_Phone_Call_Notice_R',
235 'title' => 'One Day Phone Call Notice R',
237 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
238 'body_html' => '<p>1-Day (repeating)</p>',
239 'body_text' => '1-Day (repeating)',
240 'entity_status' => '1',
241 'entity_value' => '2',
246 'msg_template_id' => NULL,
248 'recipient_listing' => NULL,
249 'recipient_manual' => NULL,
250 'record_activity' => NULL,
251 'repetition_frequency_interval' => '6',
252 'repetition_frequency_unit' => 'hour',
253 'end_action' => 'after',
254 'end_date' => 'activity_date_time',
255 'end_frequency_interval' => '2',
256 'end_frequency_unit' => 'day',
257 'start_action_condition' => '',
258 'start_action_date' => '',
259 'start_action_offset' => '',
260 'start_action_unit' => '',
261 'subject' => '1-Day (repeating) (about {activity.activity_type})',
263 $this->fixtures
['sched_event_name_1day_on_abs_date'] = [
264 'name' => 'sched_event_name_1day_on_abs_date',
265 'title' => 'sched_event_name_1day_on_abs_date',
267 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
268 'body_html' => '<p>sched_event_name_1day_on_abs_date</p>',
269 'body_text' => 'sched_event_name_1day_on_abs_date',
270 'entity_status' => '1',
271 'entity_value' => '2',
276 'msg_template_id' => NULL,
278 'recipient_listing' => NULL,
279 'recipient_manual' => NULL,
280 'record_activity' => NULL,
281 'repetition_frequency_interval' => NULL,
282 'repetition_frequency_unit' => NULL,
283 'end_action' => NULL,
285 'end_frequency_interval' => NULL,
286 'end_frequency_unit' => NULL,
287 'start_action_condition' => NULL,
288 'start_action_date' => NULL,
289 'start_action_offset' => NULL,
290 'start_action_unit' => NULL,
291 'subject' => 'sched_event_name_1day_on_abs_date',
293 $this->fixtures
['sched_membership_join_2week'] = [
294 'name' => 'sched_membership_join_2week',
295 'title' => 'sched_membership_join_2week',
296 'absolute_date' => '',
297 'body_html' => '<p>body sched_membership_join_2week</p>',
298 'body_text' => 'body sched_membership_join_2week',
301 'end_frequency_interval' => '',
302 'end_frequency_unit' => '',
303 'entity_status' => '',
304 'entity_value' => '',
309 'msg_template_id' => '',
311 'recipient_listing' => '',
312 'recipient_manual' => '',
313 'record_activity' => 1,
314 'repetition_frequency_interval' => '',
315 'repetition_frequency_unit' => '',
316 'start_action_condition' => 'after',
317 'start_action_date' => 'membership_join_date',
318 'start_action_offset' => '2',
319 'start_action_unit' => 'week',
320 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
322 $this->fixtures
['sched_membership_start_1week'] = [
323 'name' => 'sched_membership_start_1week',
324 'title' => 'sched_membership_start_1week',
325 'absolute_date' => '',
326 'body_html' => '<p>body sched_membership_start_1week</p>',
327 'body_text' => 'body sched_membership_start_1week',
330 'end_frequency_interval' => '',
331 'end_frequency_unit' => '',
332 'entity_status' => '',
333 'entity_value' => '',
338 'msg_template_id' => '',
340 'recipient_listing' => '',
341 'recipient_manual' => '',
342 'record_activity' => 1,
343 'repetition_frequency_interval' => '',
344 'repetition_frequency_unit' => '',
345 'start_action_condition' => 'after',
346 'start_action_date' => 'membership_start_date',
347 'start_action_offset' => '1',
348 'start_action_unit' => 'week',
349 'subject' => 'subject sched_membership_start_1week (joined {membership.start_date})',
351 $this->fixtures
['sched_membership_end_2week'] = [
352 'name' => 'sched_membership_end_2week',
353 'title' => 'sched_membership_end_2week',
354 'absolute_date' => '',
355 'body_html' => '<p>body sched_membership_end_2week</p>',
356 'body_text' => 'body sched_membership_end_2week',
359 'end_frequency_interval' => '',
360 'end_frequency_unit' => '',
361 'entity_status' => '',
362 'entity_value' => '',
367 'msg_template_id' => '',
369 'recipient_listing' => '',
370 'recipient_manual' => '',
371 'record_activity' => 1,
372 'repetition_frequency_interval' => '',
373 'repetition_frequency_unit' => '',
374 'start_action_condition' => 'before',
375 'start_action_date' => 'membership_end_date',
376 'start_action_offset' => '2',
377 'start_action_unit' => 'week',
378 'subject' => 'subject sched_membership_end_2week',
379 'effective_start_date' => '2012-05-01 01:00:00',
381 $this->fixtures
['sched_on_membership_end_date'] = [
382 'name' => 'sched_on_membership_end_date',
383 'title' => 'sched_on_membership_end_date',
384 'body_html' => '<p>Your membership expired today</p>',
385 'body_text' => 'Your membership expired today',
388 'record_activity' => 1,
389 'start_action_condition' => 'after',
390 'start_action_date' => 'membership_end_date',
391 'start_action_offset' => '0',
392 'start_action_unit' => 'hour',
393 'subject' => 'subject send reminder on membership_end_date',
395 $this->fixtures
['sched_after_1day_membership_end_date'] = [
396 'name' => 'sched_after_1day_membership_end_date',
397 'title' => 'sched_after_1day_membership_end_date',
398 'body_html' => '<p>Your membership expired yesterday</p>',
399 'body_text' => 'Your membership expired yesterday',
402 'record_activity' => 1,
403 'start_action_condition' => 'after',
404 'start_action_date' => 'membership_end_date',
405 'start_action_offset' => '1',
406 'start_action_unit' => 'day',
407 'subject' => 'subject send reminder on membership_end_date',
410 $this->fixtures
['sched_membership_end_2month'] = [
411 'name' => 'sched_membership_end_2month',
412 'title' => 'sched_membership_end_2month',
413 'absolute_date' => '',
414 'body_html' => '<p>body sched_membership_end_2month</p>',
415 'body_text' => 'body sched_membership_end_2month',
418 'end_frequency_interval' => '',
419 'end_frequency_unit' => '',
420 'entity_status' => '',
421 'entity_value' => '',
426 'msg_template_id' => '',
428 'recipient_listing' => '',
429 'recipient_manual' => '',
430 'record_activity' => 1,
431 'repetition_frequency_interval' => '',
432 'repetition_frequency_unit' => '',
433 'start_action_condition' => 'after',
434 'start_action_date' => 'membership_end_date',
435 'start_action_offset' => '2',
436 'start_action_unit' => 'month',
437 'subject' => 'subject sched_membership_end_2month',
440 $this->fixtures
['sched_membership_absolute_date'] = [
441 'name' => 'sched_membership_absolute_date',
442 'title' => 'sched_membership_absolute_date',
443 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
444 'body_html' => '<p>body sched_membership_absolute_date</p>',
445 'body_text' => 'body sched_membership_absolute_date',
448 'end_frequency_interval' => '',
449 'end_frequency_unit' => '',
450 'entity_status' => '',
451 'entity_value' => '',
456 'msg_template_id' => '',
458 'recipient_listing' => '',
459 'recipient_manual' => '',
460 'record_activity' => 1,
461 'repetition_frequency_interval' => '',
462 'repetition_frequency_unit' => '',
463 'start_action_condition' => '',
464 'start_action_date' => '',
465 'start_action_offset' => '',
466 'start_action_unit' => '',
467 'subject' => 'subject sched_membership_absolute_date',
470 $this->fixtures
['sched_contact_birth_day_yesterday'] = [
471 'name' => 'sched_contact_birth_day_yesterday',
472 'title' => 'sched_contact_birth_day_yesterday',
473 'absolute_date' => '',
474 'body_html' => '<p>you look like you were born yesterday!</p>',
475 'body_text' => 'you look like you were born yesterday!',
478 'end_frequency_interval' => '',
479 'end_frequency_unit' => '',
480 'entity_status' => 1,
481 'entity_value' => 'birth_date',
486 'msg_template_id' => '',
488 'recipient_listing' => '',
489 'recipient_manual' => '',
490 'record_activity' => 1,
491 'repetition_frequency_interval' => '',
492 'repetition_frequency_unit' => '',
493 'start_action_condition' => 'after',
494 'start_action_date' => 'date_field',
495 'start_action_offset' => '1',
496 'start_action_unit' => 'day',
497 'subject' => 'subject sched_contact_birth_day_yesterday',
500 $this->fixtures
['sched_contact_birth_day_anniversary'] = [
501 'name' => 'sched_contact_birth_day_anniversary',
502 'title' => 'sched_contact_birth_day_anniversary',
503 'absolute_date' => '',
504 'body_html' => '<p>happy birthday!</p>',
505 'body_text' => 'happy birthday!',
508 'end_frequency_interval' => '',
509 'end_frequency_unit' => '',
510 'entity_status' => 2,
511 'entity_value' => 'birth_date',
516 'msg_template_id' => '',
518 'recipient_listing' => '',
519 'recipient_manual' => '',
520 'record_activity' => 1,
521 'repetition_frequency_interval' => '',
522 'repetition_frequency_unit' => '',
523 'start_action_condition' => 'before',
524 'start_action_date' => 'date_field',
525 'start_action_offset' => '1',
526 'start_action_unit' => 'day',
527 'subject' => 'subject sched_contact_birth_day_anniversary',
530 $this->fixtures
['sched_contact_grad_tomorrow'] = [
531 'name' => 'sched_contact_grad_tomorrow',
532 'title' => 'sched_contact_grad_tomorrow',
533 'absolute_date' => '',
534 'body_html' => '<p>congratulations on your graduation!</p>',
535 'body_text' => 'congratulations on your graduation!',
538 'end_frequency_interval' => '',
539 'end_frequency_unit' => '',
540 'entity_status' => 1,
545 'msg_template_id' => '',
547 'recipient_listing' => '',
548 'recipient_manual' => '',
549 'record_activity' => 1,
550 'repetition_frequency_interval' => '',
551 'repetition_frequency_unit' => '',
552 'start_action_condition' => 'before',
553 'start_action_date' => 'date_field',
554 'start_action_offset' => '1',
555 'start_action_unit' => 'day',
556 'subject' => 'subject sched_contact_grad_tomorrow',
557 'effective_start_date' => '2013-10-15 20:00:00',
560 $this->fixtures
['sched_contact_grad_anniversary'] = [
561 'name' => 'sched_contact_grad_anniversary',
562 'title' => 'sched_contact_grad_anniversary',
563 'absolute_date' => '',
564 'body_html' => '<p>dear alum, please send us money.</p>',
565 'body_text' => 'dear alum, please send us money.',
568 'end_frequency_interval' => '',
569 'end_frequency_unit' => '',
570 'entity_status' => 2,
575 'msg_template_id' => '',
577 'recipient_listing' => '',
578 'recipient_manual' => '',
579 'record_activity' => 1,
580 'repetition_frequency_interval' => '',
581 'repetition_frequency_unit' => '',
582 'start_action_condition' => 'after',
583 'start_action_date' => 'date_field',
584 'start_action_offset' => '1',
585 'start_action_unit' => 'week',
586 'subject' => 'subject sched_contact_grad_anniversary',
589 $this->fixtures
['sched_contact_created_yesterday'] = [
590 'name' => 'sched_contact_created_yesterday',
591 'title' => 'sched_contact_created_yesterday',
592 'absolute_date' => '',
593 'body_html' => '<p>Your contact was created yesterday</p>',
594 'body_text' => 'Your contact was created yesterday!',
597 'end_frequency_interval' => '',
598 'end_frequency_unit' => '',
599 'entity_status' => 1,
600 'entity_value' => 'created_date',
605 'msg_template_id' => '',
607 'recipient_listing' => '',
608 'recipient_manual' => '',
609 'record_activity' => 1,
610 'repetition_frequency_interval' => '',
611 'repetition_frequency_unit' => '',
612 'start_action_condition' => 'after',
613 'start_action_date' => 'date_field',
614 'start_action_offset' => '1',
615 'start_action_unit' => 'day',
616 'subject' => 'subject sched_contact_created_yesterday',
619 $this->fixtures
['sched_contact_mod_anniversary'] = [
620 'name' => 'sched_contact_mod_anniversary',
621 'title' => 'sched_contact_mod_anniversary',
622 'absolute_date' => '',
623 'body_html' => '<p>You last updated your data last year</p>',
624 'body_text' => 'Go update your stuff!',
627 'end_frequency_interval' => '',
628 'end_frequency_unit' => '',
629 'entity_status' => 2,
630 'entity_value' => 'modified_date',
635 'msg_template_id' => '',
637 'recipient_listing' => '',
638 'recipient_manual' => '',
639 'record_activity' => 1,
640 'repetition_frequency_interval' => '',
641 'repetition_frequency_unit' => '',
642 'start_action_condition' => 'before',
643 'start_action_date' => 'date_field',
644 'start_action_offset' => '1',
645 'start_action_unit' => 'day',
646 'subject' => 'subject sched_contact_mod_anniversary',
649 $this->fixtures
['sched_event_type_start_1week_before'] = [
650 'name' => 'sched_event_type_start_1week_before',
651 'title' => 'sched_event_type_start_1week_before',
652 'absolute_date' => '',
653 'body_html' => '<p>body sched_event_type_start_1week_before ({event.title})</p>',
654 'body_text' => 'body sched_event_type_start_1week_before ({event.title})',
657 'end_frequency_interval' => '',
658 'end_frequency_unit' => '',
659 // participant status id
660 'entity_status' => '',
662 'entity_value' => '',
668 'msg_template_id' => '',
670 'recipient_listing' => '',
671 'recipient_manual' => '',
672 'record_activity' => 1,
673 'repetition_frequency_interval' => '',
674 'repetition_frequency_unit' => '',
675 'start_action_condition' => 'before',
676 'start_action_date' => 'event_start_date',
677 'start_action_offset' => '1',
678 'start_action_unit' => 'week',
679 'subject' => 'subject sched_event_type_start_1week_before ({event.title})',
681 $this->fixtures
['sched_event_type_end_2month_repeat_twice_2_weeks'] = [
682 'name' => 'sched_event_type_end_2month_repeat_twice_2_weeks',
683 'title' => 'sched_event_type_end_2month_repeat_twice_2_weeks',
684 'absolute_date' => '',
685 'body_html' => '<p>body sched_event_type_end_2month_repeat_twice_2_weeks {event.title}</p>',
686 'body_text' => 'body sched_event_type_end_2month_repeat_twice_2_weeks {event.title}',
687 'end_action' => 'after',
688 'end_date' => 'event_end_date',
689 'end_frequency_interval' => '3',
690 'end_frequency_unit' => 'month',
691 // participant status id
692 'entity_status' => '',
694 'entity_value' => '',
700 'msg_template_id' => '',
702 'recipient_listing' => '',
703 'recipient_manual' => '',
704 'record_activity' => 1,
705 'repetition_frequency_interval' => '2',
706 'repetition_frequency_unit' => 'week',
707 'start_action_condition' => 'after',
708 'start_action_date' => 'event_end_date',
709 'start_action_offset' => '2',
710 'start_action_unit' => 'month',
711 'subject' => 'subject sched_event_type_end_2month_repeat_twice_2_weeks {event.title}',
714 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = [
715 'name' => 'sched_membership_end_2month',
716 'title' => 'sched_membership_end_2month',
717 'absolute_date' => '',
718 'body_html' => '<p>body sched_membership_end_2month</p>',
719 'body_text' => 'body sched_membership_end_2month',
721 'end_date' => 'membership_end_date',
722 'end_frequency_interval' => '4',
723 'end_frequency_unit' => 'month',
724 'entity_status' => '',
725 'entity_value' => '',
730 'msg_template_id' => '',
732 'recipient_listing' => '',
733 'recipient_manual' => '',
734 'record_activity' => 1,
735 'repetition_frequency_interval' => '4',
736 'repetition_frequency_unit' => 'week',
737 'start_action_condition' => 'after',
738 'start_action_date' => 'membership_end_date',
739 'start_action_offset' => '2',
740 'start_action_unit' => 'month',
741 'subject' => 'subject sched_membership_end_2month',
743 $this->fixtures
['sched_membership_end_limit_to_none'] = [
744 'name' => 'limit to none',
745 'title' => 'limit to none',
746 'absolute_date' => '',
747 'body_html' => '<p>body sched_membership_end_2month</p>',
748 'body_text' => 'body sched_membership_end_2month',
751 'end_frequency_interval' => '4',
752 'end_frequency_unit' => 'month',
753 'entity_status' => '',
754 'entity_value' => '',
760 'msg_template_id' => '',
762 'recipient_listing' => '',
763 'recipient_manual' => '',
764 'record_activity' => 1,
765 'repetition_frequency_interval' => '4',
766 'repetition_frequency_unit' => 'week',
767 'start_action_condition' => 'after',
768 'start_action_date' => 'membership_end_date',
769 'start_action_offset' => '2',
770 'start_action_unit' => 'month',
771 'subject' => 'limit to none',
773 $this->fixtures
['sched_on_membership_end_date_repeat_interval'] = [
774 'name' => 'sched_on_membership_end_date',
775 'title' => 'sched_on_membership_end_date',
776 'body_html' => '<p>Your membership expired 1 unit ago</p>',
777 'body_text' => 'Your membership expired 1 unit ago',
778 'end_frequency_interval' => 10,
779 'end_frequency_unit' => 'year',
783 'record_activity' => 1,
784 'start_action_condition' => 'after',
785 'start_action_date' => 'membership_end_date',
786 'start_action_offset' => '0',
787 'start_action_unit' => 'hour',
788 'subject' => 'subject send reminder every unit after membership_end_date',
791 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
792 'title' => ts('Test Contact Custom group'),
793 'name' => 'test_contact_cg',
794 'extends' => 'Contact',
795 'domain_id' => CRM_Core_Config
::domainID(),
797 'collapse_adv_display' => 0,
798 'collapse_display' => 0,
800 $customField = $this->callAPISuccess('CustomField', 'create', [
801 'label' => 'Test Text',
802 'data_type' => 'String',
803 'html_type' => 'Text',
804 'custom_group_id' => $customGroup['id'],
806 $customDateField = $this->callAPISuccess('CustomField', 'create', [
807 'label' => 'Test Date Field',
808 'data_type' => 'Date',
809 'html_type' => 'Select Date',
810 'date_format' => 'mm/dd/yy',
811 'custom_group_id' => $customGroup['id'],
814 $this->fixtures
['contact_custom_token'] = [
815 'id' => $customField['id'],
816 'token' => sprintf('{contact.custom_%s}', $customField['id']),
817 'name' => sprintf('custom_%s', $customField['id']),
818 'value' => 'text ' . substr(sha1(mt_rand()), 0, 7),
821 $this->fixtures
['sched_on_custom_date'] = [
822 'name' => 'sched_on_custom_date',
823 'title' => 'sched_on_custom_date',
824 'body_html' => '<p>Send reminder before 1 hour of custom date field</p>',
825 'body_text' => 'Send reminder on custom date field',
826 'subject' => 'Send reminder on custom date field',
828 'entity_value' => 'custom_' . $customDateField['id'],
829 'entity_status' => 2,
832 ['custom_' . $customDateField['id']],
835 'start_action_offset' => 1,
836 'start_action_unit' => 'hour',
837 'start_action_condition' => 'before',
838 'start_action_date' => 'date_field',
839 'record_activity' => 1,
840 'repetition_frequency_unit' => 'hour',
841 'end_frequency_unit' => 'hour',
842 'end_action' => 'before',
843 'end_date' => 'date_field',
844 'custom_field_name' => 'custom_' . $customDateField['id'],
849 * Tears down the fixture, for example, closes a network connection.
851 * This method is called after a test is executed.
853 * @throws \CRM_Core_Exception
854 * @throws \CiviCRM_API3_Exception
855 * @throws \API_Exception
857 public function tearDown(): void
{
858 $this->deleteTestObjects();
859 MembershipType
::delete()->addWhere('name', 'NOT IN', ['General', 'Student', 'Lifetime'])->execute();
860 $this->quickCleanup([
861 'civicrm_action_schedule',
862 'civicrm_action_log',
863 'civicrm_membership',
865 'civicrm_participant',
869 $this->quickCleanUpFinancialEntities();
874 * Get a usable membership type id - creating one if none exists.
876 * It should exist but this class over-deletes in not-fully-diagnosed places.
878 * @throws \API_Exception
880 protected function getMembershipTypeID(): int {
881 $generalTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'membership_type_id', 'General');
882 if ($generalTypeID) {
883 $this->membershipTypeID
= $generalTypeID;
886 $this->membershipTypeID
= (int) MembershipType
::create()
889 'period_type' => 'rolling',
890 'member_of_contact_id' => 1,
891 'financial_type_id:name' => 'Member Dues',
892 'duration_unit' => 'month',
894 )->execute()->first()['id'];
896 return $this->membershipTypeID
;
900 * Get mailer examples.
904 public function mailerExamples(): array {
907 // Some tokens - short as subject has 128char limit in DB.
908 $someTokensTmpl = implode(';;', [
909 // basic contact token
910 '{contact.display_name}',
911 // funny legacy contact token
915 // action-scheduler token
916 '{activity.activity_type}',
918 // Further tokens can be tested in the body text/html.
919 // We use a dummy string to represent the custom token as this is done in setUp which is run after this function is called.
920 $manyTokensTmpl = implode(';;', [
922 '{contact.email_greeting}',
923 '{contactCustomToken}',
925 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
926 // can really do is check that it has something.
927 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
928 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, '{contactCustomTokenValue}');
930 // In this example, we use a lot of tokens cutting across multiple components.
932 // Schedule definition.
934 'subject' => "subj $someTokensTmpl",
935 'body_html' => "html $manyTokensTmpl",
936 'body_text' => "text $manyTokensTmpl",
938 // Assertions (regex).
940 'from_name' => '/^FIXME$/',
941 'from_email' => '/^info@EXAMPLE.ORG$/',
942 'subject' => "/^subj $someTokensExpected\$/",
943 'body_html' => "/^html $manyTokensExpected\$/",
944 'body_text' => "/^text $manyTokensExpected\$/",
948 // In this example, we customize the from address.
950 // Schedule definition.
952 'from_name' => 'Bob',
953 'from_email' => 'bob@example.org',
955 // Assertions (regex).
957 'from_name' => '/^Bob$/',
958 'from_email' => '/^bob@example.org$/',
962 // In this example, we auto-convert HTML to text
964 // Schedule definition.
966 'body_html' => '<p>Hello & stuff.</p>',
969 // Assertions (regex).
971 'body_html' => '/^' . preg_quote('<p>Hello & stuff.</p>', '/') . '/',
972 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
976 // In this example, we autoconvert HTML to text
978 // Schedule definition.
981 'body_text' => 'Hello world',
983 // Assertions (regex).
985 'body_html' => '/^--UNDEFINED--$/',
986 'body_text' => '/^Hello world$/',
990 // In this example, we test activity tokens
991 $activityTokens = '{activity.subject};;{activity.details};;{activity.activity_date_time}';
994 'activity_type_id' => 2,
995 'activity_date_time' => '20120615100000',
996 'is_current_revision' => 1,
998 'subject' => 'Phone call',
999 'details' => 'A phone call about a bear',
1001 $activityTokensExpected = 'Phone call;;A phone call about a bear;;June 15th, 2012 10:00 AM';
1003 // Schedule definition.
1005 'subject' => "subj $someTokensTmpl",
1006 'body_html' => "html {$activityTokens}",
1007 'body_text' => "text {$activityTokens}",
1009 // Assertions (regex).
1011 'from_name' => '/^FIXME$/',
1012 'from_email' => '/^info@EXAMPLE.ORG$/',
1013 'subject' => "/^subj $someTokensExpected\$/",
1014 'body_html' => "/^html $activityTokensExpected\$/",
1015 'body_text' => "/^text $activityTokensExpected\$/",
1023 * This generates a single mailing through the scheduled-reminder
1024 * system (using an activity-reminder as a baseline) and
1025 * checks that the resulting message satisfies various
1026 * regular expressions.
1028 * @param array $schedule
1029 * Values to set/override in the schedule.
1030 * Ex: array('subject' => 'Hello, {contact.first_name}!').
1031 * @param array $patterns
1032 * A list of regexes to compare with the actual email.
1033 * Ex: array('subject' => '/^Hello, Alice!/').
1034 * Keys: subject, body_text, body_html, from_name, from_email.
1036 * @throws \API_Exception
1037 * @throws \CRM_Core_Exception
1038 * @throws \Civi\API\Exception\UnauthorizedException
1039 * @dataProvider mailerExamples
1041 public function testMailer(array $schedule, array $patterns): void
{
1042 // Replace the dummy custom contact token referecnes in schedule and patterns that we had to insert because phpunit
1043 // evaluates dataProviders before running setUp
1044 foreach ($schedule as $type => $content) {
1045 $schedule[$type] = str_replace('{contactCustomToken}', $this->fixtures
['contact_custom_token']['token'], $content);
1047 foreach ($patterns as $type => $content) {
1048 $patterns[$type] = str_replace('{contactCustomTokenValue}', $this->fixtures
['contact_custom_token']['value'], $content);
1050 $this->createScheduleFromFixtures('sched_activity_1day', $schedule);
1051 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phone_call']);
1052 $contact = $this->callAPISuccess('contact', 'create', array_merge(
1053 $this->fixtures
['contact'],
1055 $this->fixtures
['contact_custom_token']['name'] => $this->fixtures
['contact_custom_token']['value'],
1060 ActivityContact
::create(FALSE)->setValues([
1061 'contact_id' => $contact['id'],
1062 'activity_id' => $activity->id
,
1063 'record_type_id:name' => 'Activity Source',
1066 CRM_Utils_Time
::setTime('2012-06-14 15:00:00');
1067 $this->callAPISuccess('job', 'send_reminder');
1068 $this->mut
->assertRecipients([['test-member@example.com']]);
1069 foreach ($this->mut
->getAllMessages('ezc') as $message) {
1070 /** @var ezcMail $message */
1073 $messageArray['subject'] = $message->subject
;
1074 $messageArray['from_name'] = $message->from
->name
;
1075 $messageArray['from_email'] = $message->from
->email
;
1076 $messageArray['body_text'] = '--UNDEFINED--';
1077 $messageArray['body_html'] = '--UNDEFINED--';
1079 foreach ($message->fetchParts() as $part) {
1080 /** @var ezcMailText ezcMailText */
1081 if ($part instanceof ezcMailText
&& $part->subType
=== 'html') {
1082 $messageArray['body_html'] = $part->text
;
1084 if ($part instanceof ezcMailText
&& $part->subType
=== 'plain') {
1085 $messageArray['body_text'] = $part->text
;
1089 foreach ($patterns as $field => $pattern) {
1090 $this->assertRegExp($pattern, $messageArray[$field],
1091 "Check that '$field'' matches regex. " . print_r(['expected' => $patterns, 'actual' => $messageArray], 1));
1094 $this->mut
->clearMessages();
1098 * Send reminder 1 hour before custom date field
1100 * @throws \CRM_Core_Exception
1102 public function testReminderWithCustomDateField(): void
{
1103 $this->createScheduleFromFixtures('sched_on_custom_date');
1104 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], [$this->fixtures
['sched_on_custom_date']['custom_field_name'] => '04/06/2021']));
1105 $this->assertCronRuns([
1107 // Before the 24-hour mark, no email
1108 'time' => '2021-04-02 04:00:00',
1113 // After the 24-hour mark, an email
1114 'time' => '2021-04-05 23:00:00',
1115 'recipients' => [['test-member@example.com']],
1116 'subjects' => ['Send reminder on custom date field'],
1119 // Run cron again; message already sent
1127 * Test calculated activity schedule.
1129 * @throws \API_Exception
1130 * @throws \CRM_Core_Exception
1132 public function testActivityDateTimeMatchNonRepeatableSchedule(): void
{
1133 $this->createScheduleFromFixtures('sched_activity_1day');
1135 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phone_call']);
1136 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1137 $activity->subject
= 'Test subject for phone_call';
1140 $source['contact_id'] = $contact['id'];
1141 $source['activity_id'] = $activity->id
;
1142 $source['record_type_id'] = 2;
1143 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1144 $activityContact->save();
1146 $this->assertCronRuns([
1148 // Before the 24-hour mark, no email
1149 'time' => '2012-06-14 04:00:00',
1154 // After the 24-hour mark, an email
1155 'time' => '2012-06-14 15:00:00',
1156 'recipients' => [['test-member@example.com']],
1157 'subjects' => ['1-Day (non-repeating) (about Phone Call)'],
1160 // Run cron again; message already sent
1165 $activities = Activity
::get(FALSE)
1166 ->setSelect(['details'])
1167 ->addWhere('activity_type_id:name', '=', 'Reminder Sent')
1168 ->addWhere('source_record_id', '=', $activity->id
)
1170 foreach ($activities as $activityDetails) {
1171 $this->assertStringContainsString($activity->subject
, $activityDetails['details']);
1176 * Test "minute" as a valid unit.
1178 * @throws \API_Exception
1179 * @throws \CRM_Core_Exception
1181 public function testActivityDateTimeMinuteUnit(): void
{
1182 $this->createScheduleFromFixtures('sched_activity_5minute');
1184 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phone_call']);
1185 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1186 $activity->subject
= 'Test subject for phone_call';
1189 $source['contact_id'] = $contact['id'];
1190 $source['activity_id'] = $activity->id
;
1191 $source['record_type_id'] = 2;
1192 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1193 $activityContact->save();
1195 $this->assertCronRuns([
1197 // 15 minutes before phone call time, no email
1198 'time' => '2012-06-15 9:45:00',
1203 // 3 minutes before, an email
1204 'time' => '2012-06-15 9:57:00',
1205 'recipients' => [['test-member@example.com']],
1208 // Run cron again; message already sent
1216 * Test schedule creation on repeatable schedule.
1218 * @throws \CRM_Core_Exception
1220 public function testActivityDateTimeMatchRepeatableSchedule(): void
{
1221 $this->createScheduleFromFixtures('sched_activity_1day_r');
1222 $this->createActivityAndContactFromFixtures();
1224 $this->assertCronRuns([
1226 // Before the 24-hour mark, no email
1227 'time' => '2012-06-14 04:00:00',
1232 // After the 24-hour mark, an email
1233 'time' => '2012-06-14 15:00:00',
1234 'recipients' => [['test-member@example.com']],
1235 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1238 // Run cron 4 hours later; first message already sent
1239 'time' => '2012-06-14 20:00:00',
1244 // Run cron 6 hours later; send second message.
1245 'time' => '2012-06-14 21:00:01',
1246 'recipients' => [['test-member@example.com']],
1247 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1253 * @throws \CRM_Core_Exception
1255 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate(): void
{
1256 $this->createScheduleFromFixtures('sched_activity_1day_r_on_abs_date');
1257 $this->createActivityAndContactFromFixtures();
1259 $this->assertCronRuns([
1261 // Before the 24-hour mark, no email
1262 'time' => '2012-06-13 04:00:00',
1267 // On absolute date set on 2012-06-14
1268 'time' => '2012-06-14 00:00:00',
1269 'recipients' => [['test-member@example.com']],
1270 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1273 // Run cron 4 hours later; first message already sent
1274 'time' => '2012-06-14 04:00:00',
1279 // Run cron 6 hours later; send second message.
1280 'time' => '2012-06-14 06:00:01',
1281 'recipients' => [['test-member@example.com']],
1282 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1288 * Test event with only an absolute date.
1290 * @throws \CRM_Core_Exception
1292 public function testEventNameWithAbsoluteDateAndNothingElse(): void
{
1293 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 1]));
1294 $this->callAPISuccess('Email', 'create', [
1295 'contact_id' => $participant->contact_id
,
1296 'email' => 'test-event@example.com',
1298 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
1300 $actionSchedule = $this->fixtures
['sched_event_name_1day_on_abs_date'];
1301 $actionSchedule['entity_value'] = $participant->event_id
;
1302 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1304 $this->assertCronRuns([
1306 // Before the 24-hour mark, no email
1307 'time' => '2012-06-13 04:00:00',
1312 // On absolute date set on 2012-06-14
1313 'time' => '2012-06-14 00:00:00',
1314 'recipients' => [['test-event@example.com']],
1315 'subjects' => ['sched_event_name_1day_on_abs_date'],
1318 // Run cron 4 hours later; first message already sent
1319 'time' => '2012-06-14 04:00:00',
1327 * For contacts/members which match schedule based on join/start date,
1328 * an email should be sent.
1330 * @throws \API_Exception
1331 * @throws \CRM_Core_Exception
1332 * @throws \CiviCRM_API3_Exception
1334 public function testMembershipDateMatch(): void
{
1335 $contactID = $this->individualCreate(array_merge($this->fixtures
['contact'], ['email' => 'test-member@example.com']));
1336 $membershipTypeID = $this->getMembershipTypeID();
1337 $membership = (array) $this->callAPISuccess('Membership', 'create', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 1, 'contact_id' => $contactID, 'sequential' => 1, 'membership_type_id' => $membershipTypeID]))['values'][0];
1338 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $membershipTypeID]);
1340 // start_date=2012-03-15 ; schedule is 2 weeks after join_date
1341 $this->assertCronRuns([
1343 // Before the 2-week mark, no email.
1344 'time' => '2012-03-28 01:00:00',
1349 // After the 2-week mark, send an email.
1350 'time' => '2012-03-29 01:00:00',
1351 'recipients' => [['test-member@example.com']],
1352 'subjects' => ['subject sched_membership_join_2week (joined March 15th, 2012)'],
1356 $this->createScheduleFromFixtures('sched_membership_start_1week', ['entity_value' => $membership['membership_type_id']]);
1358 // start_date=2012-03-15 ; schedule is 1 weeks after start_date
1359 $this->assertCronRuns([
1361 // Before the 2-week mark, no email.
1362 'time' => '2012-03-21 01:00:00',
1367 // After the 2-week mark, send an email.
1368 'time' => '2012-03-22 01:00:00',
1369 'recipients' => [['test-member@example.com']],
1370 'subjects' => ['subject sched_membership_start_1week (joined March 15th, 2012)'],
1376 * CRM-21675: Support parent and smart group in 'Limit to' field
1378 * @throws \CRM_Core_Exception
1379 * @throws \CiviCRM_API3_Exception
1381 public function testScheduleReminderWithParentGroup(): void
{
1382 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1383 $this->individualCreate(['birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com']);
1384 // Contact B with birth-date at '07-07-2005', later got added in regular group
1385 $contactID2 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'def@test.com'], 1);
1386 // Contact C with birth-date at '07-07-2005', but not included in any group
1387 $this->individualCreate(['birth_date' => '20050707', 'email' => 'ghi@test.com'], 2);
1389 // create regular group and add Contact B to it
1390 $groupID = $this->groupCreate();
1391 $this->callAPISuccess('GroupContact', 'Create', [
1392 'group_id' => $groupID,
1393 'contact_id' => $contactID2,
1396 // create smart group which will contain all Male contacts
1397 $smartGroupParams = ['form_values' => ['gender_id' => 1]];
1398 $smartGroupID = $this->smartGroupCreate(
1401 'name' => 'new_smart_group',
1402 'title' => 'New Smart Group',
1403 'parents' => [$groupID => 1],
1407 $actionScheduleParams = [
1408 'name' => 'sched_contact_birth_day_yesterday',
1409 'title' => 'sched_contact_birth_day_yesterday',
1410 'absolute_date' => '',
1411 'body_html' => '<p>you look like you were born yesterday!</p>',
1412 'body_text' => 'you look like you were born yesterday!',
1415 'end_frequency_interval' => '',
1416 'end_frequency_unit' => '',
1417 'entity_status' => 1,
1418 'entity_value' => 'birth_date',
1420 'group_id' => $groupID,
1424 'msg_template_id' => '',
1426 'recipient_listing' => '',
1427 'recipient_manual' => '',
1428 'record_activity' => 1,
1429 'repetition_frequency_interval' => '',
1430 'repetition_frequency_unit' => '',
1431 'start_action_condition' => 'after',
1432 'start_action_date' => 'date_field',
1433 'start_action_offset' => '1',
1434 'start_action_unit' => 'day',
1435 'subject' => 'subject sched_contact_birth_day_yesterday',
1438 // Create schedule reminder where parent group ($groupID) is selected to limit recipients,
1439 // which contain a individual contact - $contactID2 and is parent to smart group.
1440 $this->callAPISuccess('ActionSchedule', 'create', $actionScheduleParams);
1441 $this->assertCronRuns([
1443 // On the birthday, no email.
1444 'time' => '2005-07-07 01:00:00',
1448 // The next day, send an email.
1449 'time' => '2005-07-08 20:00:00',
1460 $this->groupDelete($smartGroupID);
1461 $this->groupDelete($groupID);
1465 * Test end date email sent.
1467 * For contacts/members which match schedule based on join date,
1468 * an email should be sent.
1470 * @throws \API_Exception
1471 * @throws \CRM_Core_Exception
1472 * @throws \CiviCRM_API3_Exception
1474 public function testMembershipJoinDateNonMatch(): void
{
1475 $this->createMembershipFromFixture('rolling_membership', '', ['email' => 'test-member@example.com']);
1476 // Add an alternative membership type, and only send messages for that type
1477 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', []);
1478 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $extraMembershipType->id
]);
1480 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1481 $this->assertCronRuns([
1483 // After the 2-week mark, don't send email because we have different membership type.
1484 'time' => '2012-03-29 01:00:00',
1491 * Test that the first and SECOND notifications are sent out.
1493 * @throws \API_Exception
1494 * @throws \CRM_Core_Exception
1495 * @throws \CiviCRM_API3_Exception
1497 public function testMembershipEndDateRepeat(): void
{
1498 // creates membership with end_date = 20120615
1499 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1500 $this->callAPISuccess('Email', 'create', [
1501 'contact_id' => $membership['contact_id'],
1502 'email' => 'test-member@example.com',
1504 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1506 $this->createScheduleFromFixtures('sched_membership_end_2month_repeat_twice_4_weeks', ['entity_value' => $membership['membership_type_id']]);
1508 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1509 $this->assertCronRuns([
1511 // After the 1-month mark, no email
1512 'time' => '2012-07-15 01:00:00',
1516 // After the 2-month mark, send an email.
1517 'time' => '2012-08-15 01:00:00',
1518 'recipients' => [['test-member@example.com']],
1521 // 4 weeks after first email send first repeat
1522 'time' => '2012-09-12 01:00:00',
1523 'recipients' => [['test-member@example.com']],
1526 // 1 week after first repeat send nothing
1527 // There was a bug where the first repeat went out and then
1528 // it would keep going out every cron run. This is to check that's
1530 'time' => '2012-09-19 01:00:00',
1534 // 4 weeks after first repeat send second repeat
1535 'time' => '2012-10-10 01:00:00',
1536 'recipients' => [['test-member@example.com']],
1539 // 4 months after membership end, send nothing
1540 'time' => '2012-10-15 01:00:00',
1544 // 5 months after membership end, send nothing
1545 'time' => '2012-11-15 01:00:00',
1552 * Test behaviour when date changes.
1554 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1558 * @throws \API_Exception
1559 * @throws \CRM_Core_Exception
1560 * @throws \CiviCRM_API3_Exception
1562 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376(): void
{
1563 // creates membership with end_date = 20120615
1564 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1565 $this->callAPISuccess('Email', 'create', [
1566 'contact_id' => $membership['contact_id'],
1567 'email' => 'test-member@example.com',
1569 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1571 $this->createScheduleFromFixtures('sched_membership_end_2month_repeat_twice_4_weeks', ['entity_value' => $membership['membership_type_id']]);
1572 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1573 $this->assertCronRuns([
1575 // After the 2-week mark, send an email.
1576 'time' => '2012-08-15 01:00:00',
1577 'recipients' => [['test-member@example.com']],
1581 // Extend membership - reminder should NOT go out.
1582 $this->callAPISuccess('membership', 'create', ['id' => $membership['id'], 'end_date' => '2014-01-01']);
1583 $this->assertCronRuns([
1585 // After the 2-week mark, send an email.
1586 'time' => '2012-09-12 01:00:00',
1593 * Test membership end date email sends.
1595 * For contacts/members which match schedule based on end date,
1596 * an email should be sent.
1598 * @throws \API_Exception
1599 * @throws \CRM_Core_Exception
1600 * @throws \CiviCRM_API3_Exception
1602 public function testMembershipEndDateMatch(): void
{
1603 // creates membership with end_date = 20120615
1604 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1605 $this->callAPISuccess('Email', 'create', [
1606 'contact_id' => $membership['contact_id'],
1607 'email' => 'test-member@example.com',
1609 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1611 $this->createScheduleFromFixtures('sched_membership_end_2week', [
1612 'entity_value' => $membership['membership_type_id'],
1613 'effective_start_date' => '2012-06-01 00:00:00',
1616 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1617 $this->assertCronRuns([
1619 // Before the 2-week mark, no email.
1620 'time' => '2012-05-31 01:00:00',
1624 // After the 2-week mark, send an email.
1625 'time' => '2012-06-01 01:00:00',
1626 'recipients' => [['test-member@example.com']],
1629 // After the email is sent, another one is not sent
1630 'time' => '2012-06-01 02:00:00',
1635 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1636 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1637 $membershipBAO = new CRM_Member_BAO_Membership();
1638 $membershipBAO->id
= $membership['id'];
1639 $membershipBAO->end_date
= '2012-09-15';
1640 $membershipBAO->save();
1642 //change the email id of chosen membership contact to assert
1643 //recipient of not the previously sent mail but the new one
1644 $result = $this->callAPISuccess('Email', 'create', [
1646 'contact_id' => $membership['contact_id'],
1647 'email' => 'member2@example.com',
1649 $this->assertAPISuccess($result);
1651 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1652 $this->assertCronRuns([
1654 // Before the 2-week mark, no email
1655 'time' => '2012-08-31 01:00:00',
1659 // After the 2-week mark, send an email
1660 'time' => '2012-09-01 01:00:00',
1661 'recipients' => [['member2@example.com']],
1664 // After the email is sent, another one is not sent
1665 'time' => '2012-09-01 02:00:00',
1669 $membershipBAO = new CRM_Member_BAO_Membership();
1670 $membershipBAO->id
= $membership['id'];
1671 $membershipBAO->end_date
= '2012-12-15';
1672 $membershipBAO->save();
1673 // end_date=2012-12-15 ; schedule is 2 weeks before end_date
1674 $this->assertCronRuns([
1676 // Before the 2-week mark, no email
1677 'time' => '2012-11-30 01:00:00',
1681 // After the 2-week mark, send an email
1682 'time' => '2012-12-01 01:00:00',
1683 'recipients' => [['member2@example.com']],
1686 // After the email is sent, another one is not sent
1687 'time' => '2012-12-01 02:00:00',
1695 * This test is very similar to testMembershipEndDateMatch, but it adds
1696 * another contact because there was a bug in
1697 * RecipientBuilder::buildRelFirstPass where it was only sending the
1698 * reminder for the first contact returned in a query for renewed
1699 * memberships. Other contacts wouldn't get the mail.
1701 * @throws \API_Exception
1702 * @throws \CRM_Core_Exception
1703 * @throws \CiviCRM_API3_Exception
1705 public function testMultipleMembershipEndDateMatch(): void
{
1706 $contactID = $this->callAPISuccess('Contact', 'create', array_merge($this->fixtures
['contact'], ['email' => 'test-member@example.com']))['id'];
1707 $contactID2 = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_2'])['id'];
1708 $membershipOne = $this->createMembershipFromFixture('rolling_membership', 2, [], ['contact_id' => $contactID]);
1709 $membershipTypeId = $membershipOne['membership_type_id'];
1710 $membershipTwo = $this->createMembershipFromFixture('rolling_membership', 2, [], ['contact_id' => $contactID2, 'membership_type_id' => $membershipTypeId]);
1711 // We are using dates that 'should' be expired but the test expects them not to be
1712 CRM_Core_DAO
::executeQuery('UPDATE civicrm_membership SET status_id = 2 WHERE 1');
1713 $this->createScheduleFromFixtures('sched_membership_end_2week', ['entity_value' => $membershipTypeId]);
1715 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1716 $this->assertCronRuns([
1718 // Before the 2-week mark, no email.
1719 'time' => '2012-05-31 01:00:00',
1723 // After the 2-week mark, send emails.
1724 'time' => '2012-06-01 01:00:00',
1726 ['test-member@example.com'],
1727 ['test-contact-2@example.com'],
1731 // After the email is sent, another one is not sent
1732 'time' => '2012-06-01 02:00:00',
1737 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1738 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1739 $membershipOneBAO = new CRM_Member_BAO_Membership();
1740 $membershipOneBAO->id
= $membershipOne['id'];
1741 $membershipOneBAO->end_date
= '2012-09-15';
1742 $membershipOneBAO->save();
1743 $membershipTwoBAO = new CRM_Member_BAO_Membership();
1744 $membershipTwoBAO->id
= $membershipTwo['id'];
1745 $membershipTwoBAO->end_date
= '2012-09-15';
1746 $membershipTwoBAO->save();
1748 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1749 $this->assertCronRuns([
1751 // Before the 2-week mark, no email
1752 'time' => '2012-08-31 01:00:00',
1756 // After the 2-week mark, send an email
1757 'time' => '2012-09-01 01:00:00',
1759 ['test-member@example.com'],
1760 ['test-contact-2@example.com'],
1764 // After the email is sent, another one is not sent
1765 'time' => '2012-06-01 02:00:00',
1772 * Test membership end date email.
1774 * For contacts/members which match schedule based on end date,
1775 * an email should be sent.
1777 * @throws \API_Exception
1778 * @throws \CRM_Core_Exception
1779 * @throws \CiviCRM_API3_Exception
1781 public function testMembershipEndDateNoMatch(): void
{
1782 // creates membership with end_date = 20120615
1783 $membership = $this->createMembershipFromFixture('rolling_membership', 'Grace');
1784 $this->callAPISuccess('Email', 'create', [
1785 'contact_id' => $membership['contact_id'],
1786 'email' => 'test-member@example.com',
1788 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1789 $this->createScheduleFromFixtures('sched_membership_end_2month', ['entity_value' => $membership['membership_type_id']]);
1791 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1792 $this->assertCronRuns([
1794 // Before the 2-week mark, no email.
1795 'time' => '2012-05-31 01:00:00',
1799 // After the 2-week mark, no email
1800 'time' => '2013-05-01 01:00:00',
1807 * @throws \CRM_Core_Exception
1809 public function testContactBirthDateNoAnniversary(): void
{
1810 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1811 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1812 $this->createScheduleFromFixtures('sched_contact_birth_day_yesterday');
1813 $this->assertCronRuns([
1815 // On the birthday, no email.
1816 'time' => '2005-07-07 01:00:00',
1820 // The next day, send an email.
1821 'time' => '2005-07-08 20:00:00',
1822 'recipients' => [['test-birth_day@example.com']],
1828 * @throws \CRM_Core_Exception
1830 public function testContactBirthDateAnniversary(): void
{
1831 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1832 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1833 $this->createScheduleFromFixtures('sched_contact_birth_day_anniversary');
1834 $this->assertCronRuns([
1836 // On some random day, no email.
1837 'time' => '2014-03-07 01:00:00',
1841 // On the eve of their 9th birthday, send an email.
1842 'time' => '2014-07-06 20:00:00',
1843 'recipients' => [['test-birth_day@example.com']],
1849 * @throws \CRM_Core_Exception
1851 public function testContactCustomDateNoAnniversary(): void
{
1853 'title' => 'Test_Group',
1854 'name' => 'test_group',
1855 'extends' => ['Individual'],
1856 'style' => 'Inline',
1857 'is_multiple' => FALSE,
1860 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1862 'label' => 'Graduation',
1863 'data_type' => 'Date',
1864 'html_type' => 'Select Date',
1865 'custom_group_id' => $createGroup['id'],
1867 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1868 $contactParams = $this->fixtures
['contact'];
1869 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1870 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1871 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1872 $this->createScheduleFromFixtures('sched_contact_grad_tomorrow', ['entity_value' => "custom_{$createField['id']}"]);
1873 $this->assertCronRuns([
1875 // On some random day, no email.
1876 'time' => '2014-03-07 01:00:00',
1880 // On the eve of their graduation, send an email.
1881 'time' => '2013-12-15 20:00:00',
1882 'recipients' => [['test-member@example.com']],
1885 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1889 * @throws \CRM_Core_Exception
1891 public function testContactCreatedNoAnniversary(): void
{
1892 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1893 $this->createScheduleFromFixtures('sched_contact_created_yesterday');
1894 $this->assertCronRuns([
1896 // On the date created, no email.
1897 'time' => $contact['values'][$contact['id']]['created_date'],
1901 // The next day, send an email.
1902 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1903 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']],
1909 * Test the impact of changing the anniversary.
1911 * @throws \CRM_Core_Exception
1913 public function testContactModifiedAnniversary(): void
{
1914 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1915 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1916 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', ['id' => $contact['id'], 'return' => 'modified_date']);
1917 $actionSchedule = $this->createScheduleFromFixtures('sched_contact_mod_anniversary');
1918 $actionSchedule['effective_start_date'] = date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date']));
1919 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1920 $this->assertCronRuns([
1922 // On some random day, no email.
1923 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1927 // On the eve of 3 years after they were modified, send an email.
1928 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1929 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']],
1935 * Check that limit_to + an empty recipients doesn't sent to multiple
1938 * @throws \API_Exception
1939 * @throws \CRM_Core_Exception
1940 * @throws \CiviCRM_API3_Exception
1942 public function testMembershipLimitToNone(): void
{
1943 // creates membership with end_date = 20120615
1944 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1945 $result = $this->callAPISuccess('Email', 'create', [
1946 'contact_id' => $membership['contact_id'],
1947 'email' => 'member@example.com',
1949 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1950 $this->callAPISuccess('contact', 'create', ['email' => 'b@c.com', 'contact_type' => 'Individual']);
1952 $this->assertAPISuccess($result);
1954 $this->createScheduleFromFixtures('sched_membership_end_limit_to_none', ['entity_value' => $membership['membership_type_id']]);
1956 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1957 $this->assertCronRuns([
1959 // Before the 2-week mark, no email.
1960 'time' => '2012-05-31 01:00:00',
1967 * Test handling of reference date for memberships.
1969 * @throws \API_Exception
1970 * @throws \CRM_Core_Exception
1971 * @throws \CiviCRM_API3_Exception
1973 public function testMembershipWithReferenceDate(): void
{
1974 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1975 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1977 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $membership['membership_type_id']]);
1979 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1980 $this->assertCronRuns([
1982 // After the 2-week mark, send an email
1983 'time' => '2012-03-29 01:00:00',
1984 'recipients' => [['member@example.com']],
1987 // After the 2-week 1day mark, don't send an email
1988 'time' => '2012-03-30 01:00:00',
1993 //check if reference date is set to membership's join date
1994 //as per the action_start_date chosen for current schedule reminder
1995 $this->assertEquals('2012-03-15 00:00:00',
1996 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership['contact_id'], 'reference_date', 'contact_id')
1999 //change current membership join date that may signifies as membership renewal activity
2000 $membershipBAO = new CRM_Member_BAO_Membership();
2001 $membershipBAO->id
= $membership['id'];
2002 $membershipBAO->join_date
= '2012-03-29';
2003 $membershipBAO->save();
2005 $this->assertCronRuns([
2007 // After the 13 days of the changed join date 2012-03-29, don't send an email
2008 'time' => '2012-04-11 01:00:00',
2012 // After the 2-week of the changed join date 2012-03-29, send an email
2013 'time' => '2012-04-12 01:00:00',
2014 'recipients' => [['member@example.com']],
2017 $this->assertCronRuns([
2019 // It should not re-send on the same day
2020 'time' => '2012-04-12 01:00:00',
2027 * Test multiple membership reminder.
2029 * @throws \API_Exception
2030 * @throws \CRM_Core_Exception
2031 * @throws \CiviCRM_API3_Exception
2033 public function testMembershipOnMultipleReminder(): void
{
2034 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
2035 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
2037 // Send email 2 weeks before end_date
2038 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week'];
2039 // Send email on end_date/expiry date
2040 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date'];
2041 $actionScheduleOn['effective_start_date'] = '2012-06-14 00:00:00';
2042 $actionScheduleAfter['effective_end_date'] = '2012-06-15 01:00:00';
2043 // Send email 1 day after end_date/grace period
2044 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date'];
2045 $actionScheduleAfter['effective_start_date'] = '2012-06-15 01:00:00';
2046 $actionScheduleAfter['effective_end_date'] = '2012-06-16 02:00:00';
2047 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership['membership_type_id'];
2048 foreach (['actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter'] as $value) {
2049 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
2052 $this->assertCronRuns(
2055 // 1day 2weeks before membership end date(MED), don't send mail
2056 'time' => '2012-05-31 01:00:00',
2060 // 2 weeks before MED, send an email
2061 'time' => '2012-06-01 01:00:00',
2062 'recipients' => [['member@example.com']],
2065 // 1day before MED, don't send mail
2066 'time' => '2012-06-14 01:00:00',
2070 // On MED, send an email
2071 'time' => '2012-06-15 00:00:00',
2072 'recipients' => [['member@example.com']],
2075 // After 1day of MED, send an email
2076 'time' => '2012-06-16 01:00:00',
2077 'recipients' => [['member@example.com']],
2080 // After 1day 1min of MED, don't send an email
2081 'time' => '2012-06-17 00:01:00',
2087 // Assert the timestamp as of when the emails of respective three reminders as configured
2088 // 2 weeks before, on and 1 day after MED, are sent
2089 $this->assertApproxEquals(
2090 strtotime('2012-06-01 01:00:00'),
2091 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE)),
2092 // Variation in test execution time.
2095 $this->assertApproxEquals(
2096 strtotime('2012-06-15 00:00:00'),
2097 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE)),
2098 // Variation in test execution time.
2101 $this->assertApproxEquals(
2102 strtotime('2012-06-16 01:00:00'),
2103 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE)),
2104 // Variation in test execution time.
2108 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
2109 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
2110 $membershipBAO = new CRM_Member_BAO_Membership();
2111 $membershipBAO->id
= $membership['id'];
2112 $membershipBAO->end_date
= '2012-06-20';
2113 $membershipBAO->save();
2115 // increase the effective end date to future
2116 $actionScheduleAfter->effective_end_date
= '2012-07-22 00:00:00';
2117 $actionScheduleAfter->save();
2119 $this->callAPISuccess('Contact', 'get', ['id' => $membership['contact_id']]);
2120 $this->assertCronRuns(
2123 // 1day 2weeks before membership end date(MED), don't send mail
2124 'time' => '2012-06-05 01:00:00',
2128 // 2 weeks before MED, send an email
2129 'time' => '2012-06-06 01:00:00',
2130 'recipients' => [['member@example.com']],
2133 // 1day before MED, don't send mail
2134 'time' => '2012-06-19 01:00:00',
2138 // On MED, send an email
2139 'time' => '2012-06-20 00:00:00',
2140 'recipients' => [['member@example.com']],
2143 // After 1day of MED, send an email
2144 'time' => '2012-06-21 01:00:00',
2145 'recipients' => [['member@example.com']],
2148 // After 1day 1min of MED, don't send an email
2149 'time' => '2012-07-21 00:01:00',
2156 * Test reminders sent on custom data anniversary.
2158 * @throws \API_Exception
2159 * @throws \CRM_Core_Exception
2161 public function testContactCustomDate_Anniversary(): void
{
2162 $this->createCustomGroupWithFieldOfType([], 'date');
2163 $contactParams = $this->fixtures
['contact'];
2164 $contactParams[$this->getCustomFieldName('date')] = '2013-12-16';
2165 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
2166 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
2167 $this->fixtures
['sched_contact_grad_anniversary']['entity_value'] = $this->getCustomFieldName('date');
2168 $this->createScheduleFromFixtures('sched_contact_grad_anniversary');
2170 $this->assertCronRuns([
2172 // On some random day, no email.
2173 'time' => '2014-03-07 01:00:00',
2177 // A week after their 5th anniversary of graduation, send an email.
2178 'time' => '2018-12-23 20:00:00',
2179 'recipients' => [['test-member@example.com']],
2185 * Test sched reminder set via registration date.
2187 * @throws \CRM_Core_Exception
2188 * @throws \CiviCRM_API3_Exception
2190 public function testEventTypeRegistrationDate(): void
{
2191 $contact = $this->individualCreate(['email' => 'test-event@example.com']);
2192 //Add it as a participant to an event ending registration - 7 days from now.
2194 'start_date' => date('Ymd', strtotime('-5 day')),
2195 'end_date' => date('Ymd', strtotime('+7 day')),
2196 'registration_start_date' => date('Ymd', strtotime('-5 day')),
2197 'registration_end_date' => date('Ymd', strtotime('+7 day')),
2199 $event = $this->eventCreate($params);
2200 $this->participantCreate(['contact_id' => $contact, 'event_id' => $event['id']]);
2202 //Create a scheduled reminder to send email 7 days before registration date.
2203 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2204 $actionSchedule['start_action_offset'] = 7;
2205 $actionSchedule['start_action_unit'] = 'day';
2206 $actionSchedule['start_action_date'] = 'registration_end_date';
2207 $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id'];
2208 $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', [
2210 'name' => 'Attended',
2212 $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2213 //Run the cron and verify if an email was sent.
2214 $this->assertCronRuns([
2216 'time' => date('Y-m-d'),
2217 'recipients' => [['test-event@example.com']],
2223 'email' => 'test-event2@example.com',
2225 $contact2 = $this->individualCreate($contactParams);
2226 //Create an event with registration end date = 2 week from now.
2227 $params['end_date'] = date('Ymd', strtotime('+2 week'));
2228 $params['registration_end_date'] = date('Ymd', strtotime('+2 week'));
2229 $event2 = $this->eventCreate($params);
2230 $this->participantCreate(['contact_id' => $contact2, 'event_id' => $event2['id']]);
2232 //Assert there is no reminder sent to the contact.
2233 $this->assertCronRuns([
2235 'time' => date('Y-m-d'),
2240 //Modify the sched reminder to be sent 2 week from registration end date.
2241 $this->callAPISuccess('action_schedule', 'create', [
2242 'id' => $actionSched['id'],
2243 'start_action_offset' => 2,
2244 'start_action_unit' => 'week',
2247 //Contact should receive the reminder now.
2248 $this->assertCronRuns([
2250 'time' => date('Y-m-d'),
2251 'recipients' => [['test-event2@example.com']],
2257 * Test sched reminder set via start date.
2259 * @throws \CRM_Core_Exception
2260 * @throws \CiviCRM_API3_Exception
2262 public function testEventTypeStartDate(): void
{
2263 // Create event+participant with start_date = 20120315, end_date = 20120615.
2264 $params = $this->fixtures
['participant'];
2265 $params['event_id'] = $this->callAPISuccess('Event', 'create', array_merge($this->fixtures
['participant']['event_id'], ['event_type_id' => 1]))['id'];
2266 $params['status_id'] = 2;
2267 $params['contact_id'] = $this->individualCreate(array_merge($this->fixtures
['contact'], ['email' => 'test-event@example.com']));
2268 $this->callAPISuccess('Participant', 'create', $params);
2270 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2271 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2272 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2274 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2275 $this->assertCronRuns([
2278 'time' => '2012-03-02 01:00:00',
2283 'time' => '2012-03-08 01:00:00',
2284 'recipients' => [['test-event@example.com']],
2287 // And then nothing else
2288 'time' => '2012-03-16 01:00:00',
2293 // CASE 2: Create a schedule reminder which was created 1 day after the schdule day,
2294 // so it shouldn't deliver reminders schedule to send 1 week before the event start date
2295 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2296 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2297 $actionSchedule['effective_start_date'] = '20120309000000';
2298 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2299 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2300 $this->assertCronRuns([
2303 'time' => '2012-03-02 01:00:00',
2308 'time' => '2012-03-08 01:00:00',
2312 // And then nothing else
2313 'time' => '2012-03-16 01:00:00',
2318 // CASE 3: Create a schedule reminder which is created less then a week before the event start date,
2319 // so it should deliver reminders schedule to send 1 week before the event start date, set the effective end date just an hour later the reminder delivery date
2320 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2321 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2322 $actionSchedule['effective_end_date'] = '20120309010000';
2323 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2324 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2325 $this->assertCronRuns([
2328 'time' => '2012-03-02 01:00:00',
2333 'time' => '2012-03-08 01:00:00',
2334 'recipients' => [['test-event@example.com']],
2337 // And then nothing else
2338 'time' => '2012-03-16 01:00:00',
2345 * Test schedule on event end date.
2347 * @throws \CRM_Core_Exception
2349 public function testEventTypeEndDateRepeat(): void
{
2350 // Create event+participant with start_date = 20120315, end_date = 20120615.
2351 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2352 $this->callAPISuccess('Email', 'create', [
2353 'contact_id' => $participant->contact_id
,
2354 'email' => 'test-event@example.com',
2356 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2358 $actionSchedule = $this->fixtures
['sched_event_type_end_2month_repeat_twice_2_weeks'];
2359 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2360 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2362 $this->assertCronRuns([
2365 'time' => '2012-08-13 01:00:00',
2369 // After the 2-month mark, send an email.
2370 'time' => '2012-08-16 01:00:00',
2371 'recipients' => [['test-event@example.com']],
2374 // After 2 months and 1 week, don't repeat yet.
2375 'time' => '2012-08-23 02:00:00',
2379 // After 2 months and 2 weeks
2380 'time' => '2012-08-30 02:00:00',
2381 'recipients' => [['test-event@example.com']],
2384 // After 2 months and 4 week
2385 'time' => '2012-09-13 02:00:00',
2386 'recipients' => [['test-event@example.com']],
2389 // After 2 months and 6 weeks
2390 'time' => '2012-09-27 01:00:00',
2397 * Run a series of cron jobs and make an assertion about email deliveries.
2399 * @param array $cronRuns
2400 * array specifying when to run cron and what messages to expect; each item is an array with keys:
2401 * - time: string, e.g. '2012-06-15 21:00:01'
2402 * - recipients: array(array(string)), list of email addresses which should receive messages
2404 * @throws \CRM_Core_Exception
2405 * @noinspection DisconnectedForeachInstructionInspection
2407 public function assertCronRuns(array $cronRuns): void
{
2408 foreach ($cronRuns as $cronRun) {
2409 CRM_Utils_Time
::setTime($cronRun['time']);
2410 $this->callAPISuccess('job', 'send_reminder', []);
2411 $this->mut
->assertRecipients($cronRun['recipients']);
2412 if (array_key_exists('subjects', $cronRun)) {
2413 $this->mut
->assertSubjects($cronRun['subjects']);
2415 $this->mut
->clearMessages();
2422 * (DAO_Name => array(int)) List of items to garbage-collect during tearDown
2424 private $_testObjects = [];
2427 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
2428 * created entities and provides for brainless cleanup.
2430 * However, it is only really brainless when initially writing the code.
2431 * It 'steals and deletes entities that are part of the 'stock build'.
2433 * In general this causes weird stuff.
2436 * @param array $params
2437 * @param int $numObjects
2438 * @param bool $createOnly
2440 * @return array|NULL|object
2441 * @see CRM_Core_DAO::createTestObject
2443 public function createTestObject($daoName, array $params = [], int $numObjects = 1, bool $createOnly = FALSE) {
2444 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
2445 if (is_array($objects)) {
2446 $this->registerTestObjects($objects);
2449 $this->registerTestObjects([$objects]);
2455 * @param array $objects
2456 * DAO or BAO objects.
2458 public function registerTestObjects(array $objects): void
{
2459 //if (is_object($objects)) {
2460 // $objects = array($objects);
2462 foreach ($objects as $object) {
2463 $daoName = str_replace('_BAO_', '_DAO_', get_class($object));
2464 $this->_testObjects
[$daoName][] = $object->id
;
2468 public function deleteTestObjects(): void
{
2469 // Note: You might argue that the FK relations between test
2470 // objects could make this problematic; however, it should
2471 // behave intuitively as long as we mentally split our
2472 // test-objects between the "manual/primary records"
2473 // and the "automatic/secondary records"
2474 foreach ($this->_testObjects
as $daoName => $daoIds) {
2475 foreach ($daoIds as $daoId) {
2476 CRM_Core_DAO
::deleteTestObjects($daoName, ['id' => $daoId]);
2479 $this->_testObjects
= [];
2483 * Test that the various repetition units work correctly.
2485 * @see https://issues.civicrm.org/jira/browse/CRM-17028
2486 * @throws \CRM_Core_Exception
2488 public function testRepetitionFrequencyUnit(): void
{
2489 $membershipTypeParams = [
2490 'duration_interval' => '1',
2491 'duration_unit' => 'year',
2493 'period_type' => 'rolling',
2495 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
2496 $interval_units = ['hour', 'day', 'week', 'month', 'year'];
2497 foreach ($interval_units as $interval_unit) {
2498 $membershipEndDate = DateTime
::createFromFormat('Y-m-d H:i:s', '2013-03-15 00:00:00');
2500 'contact_type' => 'Individual',
2501 'first_name' => 'Test',
2502 'last_name' => "Interval $interval_unit",
2505 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2507 'contact_id' => $contact->id
,
2509 'email' => "test-member-$interval_unit@example.com",
2510 'location_type_id' => 1,
2512 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2513 $membershipParams = [
2514 'membership_type_id' => $membershipType->id
,
2515 'contact_id' => $contact->id
,
2516 'join_date' => '20120315',
2517 'start_date' => '20120315',
2518 'end_date' => '20130315',
2522 $membershipParams['status-id'] = 1;
2523 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2524 $actionScheduleParams = $this->fixtures
['sched_on_membership_end_date_repeat_interval'];
2525 $actionScheduleParams['entity_value'] = $membershipType->id
;
2526 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
2527 $actionScheduleParams['repetition_frequency_interval'] = 2;
2528 $actionSchedule = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
2529 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
2530 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
2531 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
2534 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
2538 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
2539 'recipients' => [["test-member-$interval_unit@example.com"]],
2542 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
2546 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
2547 'recipients' => [["test-member-$interval_unit@example.com"]],
2550 $this->assertCronRuns($cronRuns);
2551 $actionSchedule->delete();
2552 $membership->delete();
2557 * Inherited members without permission to edit the main member contact should
2558 * not get reminders.
2560 * However, just because a contact inherits one membership doesn't mean
2561 * reminders for other memberships should be suppressed.
2565 * @throws \CRM_Core_Exception
2567 public function testInheritedMembershipPermissions(): void
{
2568 // Set up common parameters for memberships.
2569 $membershipParams = $this->fixtures
['rolling_membership'];
2570 $membershipParams['status_id'] = 1;
2572 $membershipParams['membership_type_id']['relationship_type_id'] = 1;
2573 $membershipParams['membership_type_id']['relationship_direction'] = 'b_a';
2574 $membershipType1 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2576 // We'll create a new membership type that can be held at the same time as
2578 $membershipParams['membership_type_id']['relationship_type_id'] = 'NULL';
2579 $membershipParams['membership_type_id']['relationship_direction'] = 'NULL';
2580 $membershipType2 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2582 // Create the parent membership and contact
2583 $membershipParams['membership_type_id'] = $membershipType1->id
;
2584 $mainMembership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2587 'contact_type' => 'Individual',
2588 'first_name' => 'Mom',
2589 'last_name' => 'Rel',
2592 $this->createTestObject('CRM_Contact_DAO_Contact', array_merge($contactParams, ['id' => $mainMembership->contact_id
]));
2595 'contact_id' => $mainMembership->contact_id
,
2596 'email' => 'test-member@example.com',
2597 'location_type_id' => 1,
2600 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2602 // Set up contacts and emails for the two children
2603 $contactParams['first_name'] = 'Favorite';
2604 $permChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2605 $emailParams['email'] = 'favorite@example.com';
2606 $emailParams['contact_id'] = $permChild->id
;
2607 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2609 $contactParams['first_name'] = 'Black Sheep';
2610 $nonPermChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2611 $emailParams['email'] = 'black.sheep@example.com';
2612 $emailParams['contact_id'] = $nonPermChild->id
;
2613 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2615 // Each child gets a relationship, one with permission to edit the parent. This
2616 // will trigger inherited memberships for the first membership type
2618 'relationship_type_id' => 1,
2619 'contact_id_a' => $nonPermChild->id
,
2620 'contact_id_b' => $mainMembership->contact_id
,
2623 $this->callAPISuccess('relationship', 'create', $relParams);
2625 $relParams['contact_id_a'] = $permChild->id
;
2626 $relParams['is_permission_a_b'] = CRM_Contact_BAO_Relationship
::EDIT
;
2627 $this->callAPISuccess('relationship', 'create', $relParams);
2629 // Mom and Black Sheep get their own memberships of the second type.
2630 $membershipParams['membership_type_id'] = $membershipType2->id
;
2631 $membershipParams['owner_membership_id'] = 'NULL';
2632 $membershipParams['contact_id'] = $mainMembership->contact_id
;
2633 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2635 $membershipParams['contact_id'] = $nonPermChild->id
;
2636 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2638 // Test a reminder for the first membership type - that should exclude Black
2640 $this->fixtures
['sched_membership_join_2week']['entity_value'] = $membershipType1->id
;
2641 $this->createScheduleFromFixtures('sched_membership_join_2week');
2643 $this->assertCronRuns([
2645 'time' => '2012-03-29 01:00:00',
2646 'recipients' => [['test-member@example.com'], ['favorite@example.com']],
2648 'subject sched_membership_join_2week (joined March 15th, 2012)',
2649 'subject sched_membership_join_2week (joined March 15th, 2012)',
2654 // Test a reminder for the second membership type - that should include
2656 $this->fixtures
['sched_membership_start_1week']['entity_value'] = $membershipType2->id
;
2657 $this->createScheduleFromFixtures('sched_membership_start_1week');
2659 $this->assertCronRuns([
2661 'time' => '2012-03-22 01:00:00',
2662 'recipients' => [['test-member@example.com'], ['black.sheep@example.com']],
2664 'subject sched_membership_start_1week (joined March 15th, 2012)',
2665 'subject sched_membership_start_1week (joined March 15th, 2012)',
2672 * Modify the date time by the modify rule.
2674 * @param DateTime $origDateTime
2675 * @param string $modifyRule
2679 public function createModifiedDateTime(DateTime
$origDateTime, string $modifyRule): DateTime
{
2680 $newDateTime = clone($origDateTime);
2681 $newDateTime->modify($modifyRule);
2682 return $newDateTime;
2686 * Test absolute date handling for membership.
2688 * @throws \API_Exception
2689 * @throws \CRM_Core_Exception
2690 * @throws \CiviCRM_API3_Exception
2691 * @throws \Civi\API\Exception\UnauthorizedException
2693 public function testMembershipScheduleWithAbsoluteDate(): void
{
2694 $membership = $this->createMembershipFromFixture('rolling_membership', 'New', [
2695 'email' => 'test-member@example.com',
2696 'location_type_id' => 1,
2699 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
2700 $this->fixtures
['sched_membership_absolute_date']['entity_value'] = $membership['membership_type_id'];
2701 $this->createScheduleFromFixtures('sched_membership_absolute_date');
2703 $this->assertCronRuns([
2705 // Before the 24-hour mark, no email
2706 'time' => '2012-06-13 04:00:00',
2711 // On absolute date set on 2012-06-14
2712 'time' => '2012-06-14 00:00:00',
2713 'recipients' => [['test-member@example.com']],
2714 'subjects' => ['subject sched_membership_absolute_date'],
2717 // Run cron 4 hours later; first message already sent
2718 'time' => '2012-06-14 04:00:00',
2726 * @param string $fixture
2727 * Key from $this->fixtures
2728 * @param string $status
2730 * @param array $emailParams
2731 * @param array $membershipOverrides
2734 * @throws \API_Exception
2735 * @throws \CRM_Core_Exception
2736 * @throws \CiviCRM_API3_Exception
2737 * @throws \Civi\API\Exception\UnauthorizedException
2739 protected function createMembershipFromFixture(string $fixture, string $status, array $emailParams = [], array $membershipOverrides = []): array {
2740 $membershipTypeID = $membershipOverrides['membership_type_id'] ??
$this->fixtures
[$fixture]['membership_type_id'];
2741 if (is_array($membershipTypeID)) {
2742 $membershipTypeID = MembershipType
::create()->setValues(array_merge([
2743 'member_of_contact_id' => 1,
2744 'financial_type_id:name' => 'Member Dues',
2745 'name' => 'fixture-created-type',
2746 ], $this->fixtures
[$fixture]['membership_type_id']))->execute()->first()['id'];
2748 $params = array_merge($this->fixtures
[$fixture], [
2750 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', $status),
2751 'membership_type_id' => $membershipTypeID,
2752 ], $membershipOverrides);
2753 if (empty($params['contact_id'])) {
2754 $params['contact_id'] = $this->individualCreate(['email' => '']);
2756 $membership = (array) $this->callAPISuccess('Membership', 'create', $params)['values'][0];
2758 Civi\Api4\Email
::create(FALSE)->setValues(array_merge([
2759 'contact_id' => $membership['contact_id'],
2760 'location_type_id' => 1,
2761 ], $emailParams))->execute();
2767 * Create action schedule from defined fixtures.
2769 * @param string $fixture
2770 * @param array $extraParams
2772 * @throws \CRM_Core_Exception
2774 protected function createScheduleFromFixtures(string $fixture, array $extraParams = []): void
{
2775 $id = $this->callAPISuccess('ActionSchedule', 'create', array_merge($this->fixtures
[$fixture], $extraParams))['id'];
2776 $this->fixtures
[$fixture]['action_schedule_id'] = (int) $id;
2780 * @param string $activityKey
2781 * @param string $contactKey
2783 * @throws \CRM_Core_Exception
2785 protected function createActivityAndContactFromFixtures(string $activityKey = 'phone_call', string $contactKey = 'contact'): void
{
2786 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
[$activityKey]);
2787 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
[$contactKey]);
2791 $source['contact_id'] = $contact['id'];
2792 $source['activity_id'] = $activity->id
;
2793 $source['record_type_id'] = 2;
2794 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
2795 $activityContact->save();