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,
116 $this->fixtures
['contact'] = [
118 'contact_type' => 'Individual',
119 'email' => 'test-member@example.com',
120 'gender_id' => 'Female',
121 'first_name' => 'Churmondleia',
122 'last_name' => 'Ōtākou',
124 $this->fixtures
['contact_2'] = [
126 'contact_type' => 'Individual',
127 'email' => 'test-contact-2@example.com',
128 'gender_id' => 'Male',
129 'first_name' => 'Fabio',
132 $this->fixtures
['contact_birthdate'] = [
134 'contact_type' => 'Individual',
135 'email' => 'test-birth_day@example.com',
136 'birth_date' => '20050707',
138 $this->fixtures
['sched_activity_1day'] = [
139 'name' => 'One_Day_Phone_Call_Notice',
140 'title' => 'One Day Phone Call Notice',
142 'absolute_date' => NULL,
143 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
144 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
145 'end_action' => NULL,
147 'end_frequency_interval' => NULL,
148 'end_frequency_unit' => NULL,
149 'entity_status' => '1',
150 'entity_value' => '2',
155 'msg_template_id' => NULL,
157 'recipient_listing' => NULL,
158 'recipient_manual' => NULL,
159 'record_activity' => 1,
160 'repetition_frequency_interval' => NULL,
161 'repetition_frequency_unit' => NULL,
162 'start_action_condition' => 'before',
163 'start_action_date' => 'activity_date_time',
164 'start_action_offset' => '1',
165 'start_action_unit' => 'day',
166 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
167 'effective_start_date' => '2012-06-14 00:00:00',
168 'effective_end_date' => '2012-06-15 00:00:00',
170 $this->fixtures
['sched_activity_1day_r'] = [
171 'name' => 'One_Day_Phone_Call_Notice_R',
172 'title' => 'One Day Phone Call Notice R',
174 'absolute_date' => NULL,
175 'body_html' => '<p>1-Day (repeating)</p>',
176 'body_text' => '1-Day (repeating)',
177 'end_action' => 'after',
178 'end_date' => 'activity_date_time',
179 'end_frequency_interval' => '2',
180 'end_frequency_unit' => 'day',
181 'entity_status' => '1',
182 'entity_value' => '2',
187 'msg_template_id' => NULL,
189 'recipient_listing' => NULL,
190 'recipient_manual' => NULL,
191 'record_activity' => NULL,
192 'repetition_frequency_interval' => '6',
193 'repetition_frequency_unit' => 'hour',
194 'start_action_condition' => 'before',
195 'start_action_date' => 'activity_date_time',
196 'start_action_offset' => '1',
197 'start_action_unit' => 'day',
198 'subject' => '1-Day (repeating) (about {activity.activity_type})',
199 'effective_end_date' => '2012-06-14 16:00:00',
201 $this->fixtures
['sched_activity_1day_r_on_abs_date'] = [
202 'name' => 'One_Day_Phone_Call_Notice_R',
203 'title' => 'One Day Phone Call Notice R',
205 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
206 'body_html' => '<p>1-Day (repeating)</p>',
207 'body_text' => '1-Day (repeating)',
208 'entity_status' => '1',
209 'entity_value' => '2',
214 'msg_template_id' => NULL,
216 'recipient_listing' => NULL,
217 'recipient_manual' => NULL,
218 'record_activity' => NULL,
219 'repetition_frequency_interval' => '6',
220 'repetition_frequency_unit' => 'hour',
221 'end_action' => 'after',
222 'end_date' => 'activity_date_time',
223 'end_frequency_interval' => '2',
224 'end_frequency_unit' => 'day',
225 'start_action_condition' => '',
226 'start_action_date' => '',
227 'start_action_offset' => '',
228 'start_action_unit' => '',
229 'subject' => '1-Day (repeating) (about {activity.activity_type})',
231 $this->fixtures
['sched_event_name_1day_on_abs_date'] = [
232 'name' => 'sched_event_name_1day_on_abs_date',
233 'title' => 'sched_event_name_1day_on_abs_date',
235 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
236 'body_html' => '<p>sched_event_name_1day_on_abs_date</p>',
237 'body_text' => 'sched_event_name_1day_on_abs_date',
238 'entity_status' => '1',
239 'entity_value' => '2',
244 'msg_template_id' => NULL,
246 'recipient_listing' => NULL,
247 'recipient_manual' => NULL,
248 'record_activity' => NULL,
249 'repetition_frequency_interval' => NULL,
250 'repetition_frequency_unit' => NULL,
251 'end_action' => NULL,
253 'end_frequency_interval' => NULL,
254 'end_frequency_unit' => NULL,
255 'start_action_condition' => NULL,
256 'start_action_date' => NULL,
257 'start_action_offset' => NULL,
258 'start_action_unit' => NULL,
259 'subject' => 'sched_event_name_1day_on_abs_date',
261 $this->fixtures
['sched_membership_join_2week'] = [
262 'name' => 'sched_membership_join_2week',
263 'title' => 'sched_membership_join_2week',
264 'absolute_date' => '',
265 'body_html' => '<p>body sched_membership_join_2week</p>',
266 'body_text' => 'body sched_membership_join_2week',
269 'end_frequency_interval' => '',
270 'end_frequency_unit' => '',
271 'entity_status' => '',
272 'entity_value' => '',
277 'msg_template_id' => '',
279 'recipient_listing' => '',
280 'recipient_manual' => '',
281 'record_activity' => 1,
282 'repetition_frequency_interval' => '',
283 'repetition_frequency_unit' => '',
284 'start_action_condition' => 'after',
285 'start_action_date' => 'membership_join_date',
286 'start_action_offset' => '2',
287 'start_action_unit' => 'week',
288 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
290 $this->fixtures
['sched_membership_start_1week'] = [
291 'name' => 'sched_membership_start_1week',
292 'title' => 'sched_membership_start_1week',
293 'absolute_date' => '',
294 'body_html' => '<p>body sched_membership_start_1week</p>',
295 'body_text' => 'body sched_membership_start_1week',
298 'end_frequency_interval' => '',
299 'end_frequency_unit' => '',
300 'entity_status' => '',
301 'entity_value' => '',
306 'msg_template_id' => '',
308 'recipient_listing' => '',
309 'recipient_manual' => '',
310 'record_activity' => 1,
311 'repetition_frequency_interval' => '',
312 'repetition_frequency_unit' => '',
313 'start_action_condition' => 'after',
314 'start_action_date' => 'membership_start_date',
315 'start_action_offset' => '1',
316 'start_action_unit' => 'week',
317 'subject' => 'subject sched_membership_start_1week (joined {membership.start_date})',
319 $this->fixtures
['sched_membership_end_2week'] = [
320 'name' => 'sched_membership_end_2week',
321 'title' => 'sched_membership_end_2week',
322 'absolute_date' => '',
323 'body_html' => '<p>body sched_membership_end_2week</p>',
324 'body_text' => 'body sched_membership_end_2week',
327 'end_frequency_interval' => '',
328 'end_frequency_unit' => '',
329 'entity_status' => '',
330 'entity_value' => '',
335 'msg_template_id' => '',
337 'recipient_listing' => '',
338 'recipient_manual' => '',
339 'record_activity' => 1,
340 'repetition_frequency_interval' => '',
341 'repetition_frequency_unit' => '',
342 'start_action_condition' => 'before',
343 'start_action_date' => 'membership_end_date',
344 'start_action_offset' => '2',
345 'start_action_unit' => 'week',
346 'subject' => 'subject sched_membership_end_2week',
347 'effective_start_date' => '2012-05-01 01:00:00',
349 $this->fixtures
['sched_on_membership_end_date'] = [
350 'name' => 'sched_on_membership_end_date',
351 'title' => 'sched_on_membership_end_date',
352 'body_html' => '<p>Your membership expired today</p>',
353 'body_text' => 'Your membership expired today',
356 'record_activity' => 1,
357 'start_action_condition' => 'after',
358 'start_action_date' => 'membership_end_date',
359 'start_action_offset' => '0',
360 'start_action_unit' => 'hour',
361 'subject' => 'subject send reminder on membership_end_date',
363 $this->fixtures
['sched_after_1day_membership_end_date'] = [
364 'name' => 'sched_after_1day_membership_end_date',
365 'title' => 'sched_after_1day_membership_end_date',
366 'body_html' => '<p>Your membership expired yesterday</p>',
367 'body_text' => 'Your membership expired yesterday',
370 'record_activity' => 1,
371 'start_action_condition' => 'after',
372 'start_action_date' => 'membership_end_date',
373 'start_action_offset' => '1',
374 'start_action_unit' => 'day',
375 'subject' => 'subject send reminder on membership_end_date',
378 $this->fixtures
['sched_membership_end_2month'] = [
379 'name' => 'sched_membership_end_2month',
380 'title' => 'sched_membership_end_2month',
381 'absolute_date' => '',
382 'body_html' => '<p>body sched_membership_end_2month</p>',
383 'body_text' => 'body sched_membership_end_2month',
386 'end_frequency_interval' => '',
387 'end_frequency_unit' => '',
388 'entity_status' => '',
389 'entity_value' => '',
394 'msg_template_id' => '',
396 'recipient_listing' => '',
397 'recipient_manual' => '',
398 'record_activity' => 1,
399 'repetition_frequency_interval' => '',
400 'repetition_frequency_unit' => '',
401 'start_action_condition' => 'after',
402 'start_action_date' => 'membership_end_date',
403 'start_action_offset' => '2',
404 'start_action_unit' => 'month',
405 'subject' => 'subject sched_membership_end_2month',
408 $this->fixtures
['sched_membership_absolute_date'] = [
409 'name' => 'sched_membership_absolute_date',
410 'title' => 'sched_membership_absolute_date',
411 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
412 'body_html' => '<p>body sched_membership_absolute_date</p>',
413 'body_text' => 'body sched_membership_absolute_date',
416 'end_frequency_interval' => '',
417 'end_frequency_unit' => '',
418 'entity_status' => '',
419 'entity_value' => '',
424 'msg_template_id' => '',
426 'recipient_listing' => '',
427 'recipient_manual' => '',
428 'record_activity' => 1,
429 'repetition_frequency_interval' => '',
430 'repetition_frequency_unit' => '',
431 'start_action_condition' => '',
432 'start_action_date' => '',
433 'start_action_offset' => '',
434 'start_action_unit' => '',
435 'subject' => 'subject sched_membership_absolute_date',
438 $this->fixtures
['sched_contact_birth_day_yesterday'] = [
439 'name' => 'sched_contact_birth_day_yesterday',
440 'title' => 'sched_contact_birth_day_yesterday',
441 'absolute_date' => '',
442 'body_html' => '<p>you look like you were born yesterday!</p>',
443 'body_text' => 'you look like you were born yesterday!',
446 'end_frequency_interval' => '',
447 'end_frequency_unit' => '',
448 'entity_status' => 1,
449 'entity_value' => 'birth_date',
454 'msg_template_id' => '',
456 'recipient_listing' => '',
457 'recipient_manual' => '',
458 'record_activity' => 1,
459 'repetition_frequency_interval' => '',
460 'repetition_frequency_unit' => '',
461 'start_action_condition' => 'after',
462 'start_action_date' => 'date_field',
463 'start_action_offset' => '1',
464 'start_action_unit' => 'day',
465 'subject' => 'subject sched_contact_birth_day_yesterday',
468 $this->fixtures
['sched_contact_birth_day_anniversary'] = [
469 'name' => 'sched_contact_birth_day_anniversary',
470 'title' => 'sched_contact_birth_day_anniversary',
471 'absolute_date' => '',
472 'body_html' => '<p>happy birthday!</p>',
473 'body_text' => 'happy birthday!',
476 'end_frequency_interval' => '',
477 'end_frequency_unit' => '',
478 'entity_status' => 2,
479 'entity_value' => 'birth_date',
484 'msg_template_id' => '',
486 'recipient_listing' => '',
487 'recipient_manual' => '',
488 'record_activity' => 1,
489 'repetition_frequency_interval' => '',
490 'repetition_frequency_unit' => '',
491 'start_action_condition' => 'before',
492 'start_action_date' => 'date_field',
493 'start_action_offset' => '1',
494 'start_action_unit' => 'day',
495 'subject' => 'subject sched_contact_birth_day_anniversary',
498 $this->fixtures
['sched_contact_grad_tomorrow'] = [
499 'name' => 'sched_contact_grad_tomorrow',
500 'title' => 'sched_contact_grad_tomorrow',
501 'absolute_date' => '',
502 'body_html' => '<p>congratulations on your graduation!</p>',
503 'body_text' => 'congratulations on your graduation!',
506 'end_frequency_interval' => '',
507 'end_frequency_unit' => '',
508 'entity_status' => 1,
513 'msg_template_id' => '',
515 'recipient_listing' => '',
516 'recipient_manual' => '',
517 'record_activity' => 1,
518 'repetition_frequency_interval' => '',
519 'repetition_frequency_unit' => '',
520 'start_action_condition' => 'before',
521 'start_action_date' => 'date_field',
522 'start_action_offset' => '1',
523 'start_action_unit' => 'day',
524 'subject' => 'subject sched_contact_grad_tomorrow',
525 'effective_start_date' => '2013-10-15 20:00:00',
528 $this->fixtures
['sched_contact_grad_anniversary'] = [
529 'name' => 'sched_contact_grad_anniversary',
530 'title' => 'sched_contact_grad_anniversary',
531 'absolute_date' => '',
532 'body_html' => '<p>dear alum, please send us money.</p>',
533 'body_text' => 'dear alum, please send us money.',
536 'end_frequency_interval' => '',
537 'end_frequency_unit' => '',
538 'entity_status' => 2,
543 'msg_template_id' => '',
545 'recipient_listing' => '',
546 'recipient_manual' => '',
547 'record_activity' => 1,
548 'repetition_frequency_interval' => '',
549 'repetition_frequency_unit' => '',
550 'start_action_condition' => 'after',
551 'start_action_date' => 'date_field',
552 'start_action_offset' => '1',
553 'start_action_unit' => 'week',
554 'subject' => 'subject sched_contact_grad_anniversary',
557 $this->fixtures
['sched_contact_created_yesterday'] = [
558 'name' => 'sched_contact_created_yesterday',
559 'title' => 'sched_contact_created_yesterday',
560 'absolute_date' => '',
561 'body_html' => '<p>Your contact was created yesterday</p>',
562 'body_text' => 'Your contact was created yesterday!',
565 'end_frequency_interval' => '',
566 'end_frequency_unit' => '',
567 'entity_status' => 1,
568 'entity_value' => 'created_date',
573 'msg_template_id' => '',
575 'recipient_listing' => '',
576 'recipient_manual' => '',
577 'record_activity' => 1,
578 'repetition_frequency_interval' => '',
579 'repetition_frequency_unit' => '',
580 'start_action_condition' => 'after',
581 'start_action_date' => 'date_field',
582 'start_action_offset' => '1',
583 'start_action_unit' => 'day',
584 'subject' => 'subject sched_contact_created_yesterday',
587 $this->fixtures
['sched_contact_mod_anniversary'] = [
588 'name' => 'sched_contact_mod_anniversary',
589 'title' => 'sched_contact_mod_anniversary',
590 'absolute_date' => '',
591 'body_html' => '<p>You last updated your data last year</p>',
592 'body_text' => 'Go update your stuff!',
595 'end_frequency_interval' => '',
596 'end_frequency_unit' => '',
597 'entity_status' => 2,
598 'entity_value' => 'modified_date',
603 'msg_template_id' => '',
605 'recipient_listing' => '',
606 'recipient_manual' => '',
607 'record_activity' => 1,
608 'repetition_frequency_interval' => '',
609 'repetition_frequency_unit' => '',
610 'start_action_condition' => 'before',
611 'start_action_date' => 'date_field',
612 'start_action_offset' => '1',
613 'start_action_unit' => 'day',
614 'subject' => 'subject sched_contact_mod_anniversary',
617 $this->fixtures
['sched_event_type_start_1week_before'] = [
618 'name' => 'sched_event_type_start_1week_before',
619 'title' => 'sched_event_type_start_1week_before',
620 'absolute_date' => '',
621 'body_html' => '<p>body sched_event_type_start_1week_before ({event.title})</p>',
622 'body_text' => 'body sched_event_type_start_1week_before ({event.title})',
625 'end_frequency_interval' => '',
626 'end_frequency_unit' => '',
627 // participant status id
628 'entity_status' => '',
630 'entity_value' => '',
636 'msg_template_id' => '',
638 'recipient_listing' => '',
639 'recipient_manual' => '',
640 'record_activity' => 1,
641 'repetition_frequency_interval' => '',
642 'repetition_frequency_unit' => '',
643 'start_action_condition' => 'before',
644 'start_action_date' => 'event_start_date',
645 'start_action_offset' => '1',
646 'start_action_unit' => 'week',
647 'subject' => 'subject sched_event_type_start_1week_before ({event.title})',
649 $this->fixtures
['sched_event_type_end_2month_repeat_twice_2_weeks'] = [
650 'name' => 'sched_event_type_end_2month_repeat_twice_2_weeks',
651 'title' => 'sched_event_type_end_2month_repeat_twice_2_weeks',
652 'absolute_date' => '',
653 'body_html' => '<p>body sched_event_type_end_2month_repeat_twice_2_weeks {event.title}</p>',
654 'body_text' => 'body sched_event_type_end_2month_repeat_twice_2_weeks {event.title}',
655 'end_action' => 'after',
656 'end_date' => 'event_end_date',
657 'end_frequency_interval' => '3',
658 'end_frequency_unit' => 'month',
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' => '2',
674 'repetition_frequency_unit' => 'week',
675 'start_action_condition' => 'after',
676 'start_action_date' => 'event_end_date',
677 'start_action_offset' => '2',
678 'start_action_unit' => 'month',
679 'subject' => 'subject sched_event_type_end_2month_repeat_twice_2_weeks {event.title}',
682 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = [
683 'name' => 'sched_membership_end_2month',
684 'title' => 'sched_membership_end_2month',
685 'absolute_date' => '',
686 'body_html' => '<p>body sched_membership_end_2month</p>',
687 'body_text' => 'body sched_membership_end_2month',
689 'end_date' => 'membership_end_date',
690 'end_frequency_interval' => '4',
691 'end_frequency_unit' => 'month',
692 'entity_status' => '',
693 'entity_value' => '',
698 'msg_template_id' => '',
700 'recipient_listing' => '',
701 'recipient_manual' => '',
702 'record_activity' => 1,
703 'repetition_frequency_interval' => '4',
704 'repetition_frequency_unit' => 'week',
705 'start_action_condition' => 'after',
706 'start_action_date' => 'membership_end_date',
707 'start_action_offset' => '2',
708 'start_action_unit' => 'month',
709 'subject' => 'subject sched_membership_end_2month',
711 $this->fixtures
['sched_membership_end_limit_to_none'] = [
712 'name' => 'limit to none',
713 'title' => 'limit to none',
714 'absolute_date' => '',
715 'body_html' => '<p>body sched_membership_end_2month</p>',
716 'body_text' => 'body sched_membership_end_2month',
719 'end_frequency_interval' => '4',
720 'end_frequency_unit' => 'month',
721 'entity_status' => '',
722 'entity_value' => '',
728 'msg_template_id' => '',
730 'recipient_listing' => '',
731 'recipient_manual' => '',
732 'record_activity' => 1,
733 'repetition_frequency_interval' => '4',
734 'repetition_frequency_unit' => 'week',
735 'start_action_condition' => 'after',
736 'start_action_date' => 'membership_end_date',
737 'start_action_offset' => '2',
738 'start_action_unit' => 'month',
739 'subject' => 'limit to none',
741 $this->fixtures
['sched_on_membership_end_date_repeat_interval'] = [
742 'name' => 'sched_on_membership_end_date',
743 'title' => 'sched_on_membership_end_date',
744 'body_html' => '<p>Your membership expired 1 unit ago</p>',
745 'body_text' => 'Your membership expired 1 unit ago',
746 'end_frequency_interval' => 10,
747 'end_frequency_unit' => 'year',
751 'record_activity' => 1,
752 'start_action_condition' => 'after',
753 'start_action_date' => 'membership_end_date',
754 'start_action_offset' => '0',
755 'start_action_unit' => 'hour',
756 'subject' => 'subject send reminder every unit after membership_end_date',
759 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
760 'title' => ts('Test Contact Custom group'),
761 'name' => 'test_contact_cg',
762 'extends' => 'Contact',
763 'domain_id' => CRM_Core_Config
::domainID(),
765 'collapse_adv_display' => 0,
766 'collapse_display' => 0,
768 $customField = $this->callAPISuccess('CustomField', 'create', [
769 'label' => 'Test Text',
770 'data_type' => 'String',
771 'html_type' => 'Text',
772 'custom_group_id' => $customGroup['id'],
774 $customDateField = $this->callAPISuccess('CustomField', 'create', [
775 'label' => 'Test Date Field',
776 'data_type' => 'Date',
777 'html_type' => 'Select Date',
778 'date_format' => 'mm/dd/yy',
779 'custom_group_id' => $customGroup['id'],
782 $this->fixtures
['contact_custom_token'] = [
783 'id' => $customField['id'],
784 'token' => sprintf('{contact.custom_%s}', $customField['id']),
785 'name' => sprintf('custom_%s', $customField['id']),
786 'value' => 'text ' . substr(sha1(mt_rand()), 0, 7),
789 $this->fixtures
['sched_on_custom_date'] = [
790 'name' => 'sched_on_custom_date',
791 'title' => 'sched_on_custom_date',
792 'body_html' => '<p>Send reminder before 1 hour of custom date field</p>',
793 'body_text' => 'Send reminder on custom date field',
794 'subject' => 'Send reminder on custom date field',
796 'entity_value' => 'custom_' . $customDateField['id'],
797 'entity_status' => 2,
800 ['custom_' . $customDateField['id']],
803 'start_action_offset' => 1,
804 'start_action_unit' => 'hour',
805 'start_action_condition' => 'before',
806 'start_action_date' => 'date_field',
807 'record_activity' => 1,
808 'repetition_frequency_unit' => 'hour',
809 'end_frequency_unit' => 'hour',
810 'end_action' => 'before',
811 'end_date' => 'date_field',
812 'custom_field_name' => 'custom_' . $customDateField['id'],
817 * Tears down the fixture, for example, closes a network connection.
819 * This method is called after a test is executed.
821 * @throws \CRM_Core_Exception
822 * @throws \CiviCRM_API3_Exception
823 * @throws \API_Exception
825 public function tearDown(): void
{
826 $this->deleteTestObjects();
827 MembershipType
::delete()->addWhere('name', 'NOT IN', ['General', 'Student', 'Lifetime'])->execute();
828 $this->quickCleanup([
829 'civicrm_action_schedule',
830 'civicrm_action_log',
831 'civicrm_membership',
833 'civicrm_participant',
837 $this->quickCleanUpFinancialEntities();
842 * Get a usable membership type id - creating one if none exists.
844 * It should exist but this class over-deletes in not-fully-diagnosed places.
846 * @throws \API_Exception
848 protected function getMembershipTypeID(): int {
849 $generalTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'membership_type_id', 'General');
850 if ($generalTypeID) {
851 $this->membershipTypeID
= $generalTypeID;
854 $this->membershipTypeID
= (int) MembershipType
::create()
857 'period_type' => 'rolling',
858 'member_of_contact_id' => 1,
859 'financial_type_id:name' => 'Member Dues',
860 'duration_unit' => 1,
862 )->execute()->first()['id'];
864 return $this->membershipTypeID
;
868 * Get mailer examples.
872 public function mailerExamples(): array {
875 // Some tokens - short as subject has 128char limit in DB.
876 $someTokensTmpl = implode(';;', [
877 // basic contact token
878 '{contact.display_name}',
879 // funny legacy contact token
881 // funny legacy contact token
882 '{contact.gender_id}',
885 // action-scheduler token
886 '{activity.activity_type}',
888 // Further tokens can be tested in the body text/html.
889 // We use a dummy string to represent the custom token as this is done in setUp which is run after this function is called.
890 $manyTokensTmpl = implode(';;', [
892 '{contact.email_greeting}',
893 '{contactCustomToken}',
895 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
896 // can really do is check that it has something.
897 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
898 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, '{contactCustomTokenValue}');
900 // In this example, we use a lot of tokens cutting across multiple components.
902 // Schedule definition.
904 'subject' => "subj $someTokensTmpl",
905 'body_html' => "html $manyTokensTmpl",
906 'body_text' => "text $manyTokensTmpl",
908 // Assertions (regex).
910 'from_name' => '/^FIXME$/',
911 'from_email' => '/^info@EXAMPLE.ORG$/',
912 'subject' => "/^subj $someTokensExpected\$/",
913 'body_html' => "/^html $manyTokensExpected\$/",
914 'body_text' => "/^text $manyTokensExpected\$/",
918 // In this example, we customize the from address.
920 // Schedule definition.
922 'from_name' => 'Bob',
923 'from_email' => 'bob@example.org',
925 // Assertions (regex).
927 'from_name' => '/^Bob$/',
928 'from_email' => '/^bob@example.org$/',
932 // In this example, we auto-convert HTML to text
934 // Schedule definition.
936 'body_html' => '<p>Hello & stuff.</p>',
939 // Assertions (regex).
941 'body_html' => '/^' . preg_quote('<p>Hello & stuff.</p>', '/') . '/',
942 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
946 // In this example, we autoconvert HTML to text
948 // Schedule definition.
951 'body_text' => 'Hello world',
953 // Assertions (regex).
955 'body_html' => '/^--UNDEFINED--$/',
956 'body_text' => '/^Hello world$/',
964 * This generates a single mailing through the scheduled-reminder
965 * system (using an activity-reminder as a baseline) and
966 * checks that the resulting message satisfies various
967 * regular expressions.
969 * @param array $schedule
970 * Values to set/override in the schedule.
971 * Ex: array('subject' => 'Hello, {contact.first_name}!').
972 * @param array $patterns
973 * A list of regexes to compare with the actual email.
974 * Ex: array('subject' => '/^Hello, Alice!/').
975 * Keys: subject, body_text, body_html, from_name, from_email.
977 * @throws \API_Exception
978 * @throws \CRM_Core_Exception
979 * @throws \Civi\API\Exception\UnauthorizedException
980 * @dataProvider mailerExamples
982 public function testMailer(array $schedule, array $patterns): void
{
983 // Replace the dummy custom contact token referecnes in schedule and patterns that we had to insert because phpunit
984 // evaluates dataProviders before running setUp
985 foreach ($schedule as $type => $content) {
986 $schedule[$type] = str_replace('{contactCustomToken}', $this->fixtures
['contact_custom_token']['token'], $content);
988 foreach ($patterns as $type => $content) {
989 $patterns[$type] = str_replace('{contactCustomTokenValue}', $this->fixtures
['contact_custom_token']['value'], $content);
991 $this->createScheduleFromFixtures('sched_activity_1day', $schedule);
992 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phone_call']);
993 $contact = $this->callAPISuccess('contact', 'create', array_merge(
994 $this->fixtures
['contact'],
996 $this->fixtures
['contact_custom_token']['name'] => $this->fixtures
['contact_custom_token']['value'],
1001 ActivityContact
::create(FALSE)->setValues([
1002 'contact_id' => $contact['id'],
1003 'activity_id' => $activity->id
,
1004 'record_type_id:name' => 'Activity Source',
1007 CRM_Utils_Time
::setTime('2012-06-14 15:00:00');
1008 $this->callAPISuccess('job', 'send_reminder');
1009 $this->mut
->assertRecipients([['test-member@example.com']]);
1010 foreach ($this->mut
->getAllMessages('ezc') as $message) {
1011 /** @var ezcMail $message */
1014 $messageArray['subject'] = $message->subject
;
1015 $messageArray['from_name'] = $message->from
->name
;
1016 $messageArray['from_email'] = $message->from
->email
;
1017 $messageArray['body_text'] = '--UNDEFINED--';
1018 $messageArray['body_html'] = '--UNDEFINED--';
1020 foreach ($message->fetchParts() as $part) {
1021 /** @var ezcMailText ezcMailText */
1022 if ($part instanceof ezcMailText
&& $part->subType
=== 'html') {
1023 $messageArray['body_html'] = $part->text
;
1025 if ($part instanceof ezcMailText
&& $part->subType
=== 'plain') {
1026 $messageArray['body_text'] = $part->text
;
1030 foreach ($patterns as $field => $pattern) {
1031 $this->assertRegExp($pattern, $messageArray[$field],
1032 "Check that '$field'' matches regex. " . print_r(['expected' => $patterns, 'actual' => $messageArray], 1));
1035 $this->mut
->clearMessages();
1039 * Send reminder 1 hour before custom date field
1041 * @throws \CRM_Core_Exception
1043 public function testReminderWithCustomDateField(): void
{
1044 $this->createScheduleFromFixtures('sched_on_custom_date');
1045 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], [$this->fixtures
['sched_on_custom_date']['custom_field_name'] => '04/06/2021']));
1046 $this->assertCronRuns([
1048 // Before the 24-hour mark, no email
1049 'time' => '2021-04-02 04:00:00',
1054 // After the 24-hour mark, an email
1055 'time' => '2021-04-05 23:00:00',
1056 'recipients' => [['test-member@example.com']],
1057 'subjects' => ['Send reminder on custom date field'],
1060 // Run cron again; message already sent
1068 * Test calculated activity schedule.
1070 * @throws \API_Exception
1071 * @throws \CRM_Core_Exception
1073 public function testActivityDateTimeMatchNonRepeatableSchedule(): void
{
1074 $this->createScheduleFromFixtures('sched_activity_1day');
1076 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phone_call']);
1077 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1078 $activity->subject
= 'Test subject for phone_call';
1081 $source['contact_id'] = $contact['id'];
1082 $source['activity_id'] = $activity->id
;
1083 $source['record_type_id'] = 2;
1084 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1085 $activityContact->save();
1087 $this->assertCronRuns([
1089 // Before the 24-hour mark, no email
1090 'time' => '2012-06-14 04:00:00',
1095 // After the 24-hour mark, an email
1096 'time' => '2012-06-14 15:00:00',
1097 'recipients' => [['test-member@example.com']],
1098 'subjects' => ['1-Day (non-repeating) (about Phone Call)'],
1101 // Run cron again; message already sent
1106 $activities = Activity
::get(FALSE)
1107 ->setSelect(['details'])
1108 ->addWhere('activity_type_id:name', '=', 'Reminder Sent')
1109 ->addWhere('source_record_id', '=', $activity->id
)
1111 foreach ($activities as $activityDetails) {
1112 $this->assertStringContainsString($activity->subject
, $activityDetails['details']);
1117 * Test schedule creation on repeatable schedule.
1119 * @throws \CRM_Core_Exception
1121 public function testActivityDateTimeMatchRepeatableSchedule(): void
{
1122 $this->createScheduleFromFixtures('sched_activity_1day_r');
1123 $this->createActivityAndContactFromFixtures();
1125 $this->assertCronRuns([
1127 // Before the 24-hour mark, no email
1128 'time' => '2012-06-14 04:00:00',
1133 // After the 24-hour mark, an email
1134 'time' => '2012-06-14 15:00:00',
1135 'recipients' => [['test-member@example.com']],
1136 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1139 // Run cron 4 hours later; first message already sent
1140 'time' => '2012-06-14 20:00:00',
1145 // Run cron 6 hours later; send second message.
1146 'time' => '2012-06-14 21:00:01',
1147 'recipients' => [['test-member@example.com']],
1148 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1154 * @throws \CRM_Core_Exception
1156 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate(): void
{
1157 $this->createScheduleFromFixtures('sched_activity_1day_r_on_abs_date');
1158 $this->createActivityAndContactFromFixtures();
1160 $this->assertCronRuns([
1162 // Before the 24-hour mark, no email
1163 'time' => '2012-06-13 04:00:00',
1168 // On absolute date set on 2012-06-14
1169 'time' => '2012-06-14 00:00:00',
1170 'recipients' => [['test-member@example.com']],
1171 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1174 // Run cron 4 hours later; first message already sent
1175 'time' => '2012-06-14 04:00:00',
1180 // Run cron 6 hours later; send second message.
1181 'time' => '2012-06-14 06:00:01',
1182 'recipients' => [['test-member@example.com']],
1183 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1189 * Test event with only an absolute date.
1191 * @throws \CRM_Core_Exception
1193 public function testEventNameWithAbsoluteDateAndNothingElse(): void
{
1194 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 1]));
1195 $this->callAPISuccess('Email', 'create', [
1196 'contact_id' => $participant->contact_id
,
1197 'email' => 'test-event@example.com',
1199 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
1201 $actionSchedule = $this->fixtures
['sched_event_name_1day_on_abs_date'];
1202 $actionSchedule['entity_value'] = $participant->event_id
;
1203 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1205 $this->assertCronRuns([
1207 // Before the 24-hour mark, no email
1208 'time' => '2012-06-13 04:00:00',
1213 // On absolute date set on 2012-06-14
1214 'time' => '2012-06-14 00:00:00',
1215 'recipients' => [['test-event@example.com']],
1216 'subjects' => ['sched_event_name_1day_on_abs_date'],
1219 // Run cron 4 hours later; first message already sent
1220 'time' => '2012-06-14 04:00:00',
1228 * For contacts/members which match schedule based on join/start date,
1229 * an email should be sent.
1231 * @throws \API_Exception
1232 * @throws \CRM_Core_Exception
1233 * @throws \CiviCRM_API3_Exception
1235 public function testMembershipDateMatch(): void
{
1236 $contactID = $this->individualCreate(array_merge($this->fixtures
['contact'], ['email' => 'test-member@example.com']));
1237 $membershipTypeID = $this->getMembershipTypeID();
1238 $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];
1239 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $membershipTypeID]);
1241 // start_date=2012-03-15 ; schedule is 2 weeks after join_date
1242 $this->assertCronRuns([
1244 // Before the 2-week mark, no email.
1245 'time' => '2012-03-28 01:00:00',
1250 // After the 2-week mark, send an email.
1251 'time' => '2012-03-29 01:00:00',
1252 'recipients' => [['test-member@example.com']],
1253 'subjects' => ['subject sched_membership_join_2week (joined March 15th, 2012)'],
1257 $this->createScheduleFromFixtures('sched_membership_start_1week', ['entity_value' => $membership['membership_type_id']]);
1259 // start_date=2012-03-15 ; schedule is 1 weeks after start_date
1260 $this->assertCronRuns([
1262 // Before the 2-week mark, no email.
1263 'time' => '2012-03-21 01:00:00',
1268 // After the 2-week mark, send an email.
1269 'time' => '2012-03-22 01:00:00',
1270 'recipients' => [['test-member@example.com']],
1271 'subjects' => ['subject sched_membership_start_1week (joined March 15th, 2012)'],
1277 * CRM-21675: Support parent and smart group in 'Limit to' field
1279 * @throws \CRM_Core_Exception
1280 * @throws \CiviCRM_API3_Exception
1282 public function testScheduleReminderWithParentGroup(): void
{
1283 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1284 $this->individualCreate(['birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com']);
1285 // Contact B with birth-date at '07-07-2005', later got added in regular group
1286 $contactID2 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'def@test.com'], 1);
1287 // Contact C with birth-date at '07-07-2005', but not included in any group
1288 $this->individualCreate(['birth_date' => '20050707', 'email' => 'ghi@test.com'], 2);
1290 // create regular group and add Contact B to it
1291 $groupID = $this->groupCreate();
1292 $this->callAPISuccess('GroupContact', 'Create', [
1293 'group_id' => $groupID,
1294 'contact_id' => $contactID2,
1297 // create smart group which will contain all Male contacts
1298 $smartGroupParams = ['form_values' => ['gender_id' => 1]];
1299 $smartGroupID = $this->smartGroupCreate(
1302 'name' => 'new_smart_group',
1303 'title' => 'New Smart Group',
1304 'parents' => [$groupID => 1],
1308 $actionScheduleParams = [
1309 'name' => 'sched_contact_birth_day_yesterday',
1310 'title' => 'sched_contact_birth_day_yesterday',
1311 'absolute_date' => '',
1312 'body_html' => '<p>you look like you were born yesterday!</p>',
1313 'body_text' => 'you look like you were born yesterday!',
1316 'end_frequency_interval' => '',
1317 'end_frequency_unit' => '',
1318 'entity_status' => 1,
1319 'entity_value' => 'birth_date',
1321 'group_id' => $groupID,
1325 'msg_template_id' => '',
1327 'recipient_listing' => '',
1328 'recipient_manual' => '',
1329 'record_activity' => 1,
1330 'repetition_frequency_interval' => '',
1331 'repetition_frequency_unit' => '',
1332 'start_action_condition' => 'after',
1333 'start_action_date' => 'date_field',
1334 'start_action_offset' => '1',
1335 'start_action_unit' => 'day',
1336 'subject' => 'subject sched_contact_birth_day_yesterday',
1339 // Create schedule reminder where parent group ($groupID) is selected to limit recipients,
1340 // which contain a individual contact - $contactID2 and is parent to smart group.
1341 $this->callAPISuccess('ActionSchedule', 'create', $actionScheduleParams);
1342 $this->assertCronRuns([
1344 // On the birthday, no email.
1345 'time' => '2005-07-07 01:00:00',
1349 // The next day, send an email.
1350 'time' => '2005-07-08 20:00:00',
1361 $this->groupDelete($smartGroupID);
1362 $this->groupDelete($groupID);
1366 * Test end date email sent.
1368 * For contacts/members which match schedule based on join date,
1369 * an email should be sent.
1371 * @throws \API_Exception
1372 * @throws \CRM_Core_Exception
1373 * @throws \CiviCRM_API3_Exception
1375 public function testMembershipJoinDateNonMatch(): void
{
1376 $this->createMembershipFromFixture('rolling_membership', '', ['email' => 'test-member@example.com']);
1377 // Add an alternative membership type, and only send messages for that type
1378 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', []);
1379 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $extraMembershipType->id
]);
1381 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1382 $this->assertCronRuns([
1384 // After the 2-week mark, don't send email because we have different membership type.
1385 'time' => '2012-03-29 01:00:00',
1392 * Test that the first and SECOND notifications are sent out.
1394 * @throws \API_Exception
1395 * @throws \CRM_Core_Exception
1396 * @throws \CiviCRM_API3_Exception
1398 public function testMembershipEndDateRepeat(): void
{
1399 // creates membership with end_date = 20120615
1400 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1401 $this->callAPISuccess('Email', 'create', [
1402 'contact_id' => $membership['contact_id'],
1403 'email' => 'test-member@example.com',
1405 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1407 $this->createScheduleFromFixtures('sched_membership_end_2month_repeat_twice_4_weeks', ['entity_value' => $membership['membership_type_id']]);
1409 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1410 $this->assertCronRuns([
1412 // After the 1-month mark, no email
1413 'time' => '2012-07-15 01:00:00',
1417 // After the 2-month mark, send an email.
1418 'time' => '2012-08-15 01:00:00',
1419 'recipients' => [['test-member@example.com']],
1422 // 4 weeks after first email send first repeat
1423 'time' => '2012-09-12 01:00:00',
1424 'recipients' => [['test-member@example.com']],
1427 // 1 week after first repeat send nothing
1428 // There was a bug where the first repeat went out and then
1429 // it would keep going out every cron run. This is to check that's
1431 'time' => '2012-09-19 01:00:00',
1435 // 4 weeks after first repeat send second repeat
1436 'time' => '2012-10-10 01:00:00',
1437 'recipients' => [['test-member@example.com']],
1440 // 4 months after membership end, send nothing
1441 'time' => '2012-10-15 01:00:00',
1445 // 5 months after membership end, send nothing
1446 'time' => '2012-11-15 01:00:00',
1453 * Test behaviour when date changes.
1455 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1459 * @throws \API_Exception
1460 * @throws \CRM_Core_Exception
1461 * @throws \CiviCRM_API3_Exception
1463 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376(): void
{
1464 // creates membership with end_date = 20120615
1465 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1466 $this->callAPISuccess('Email', 'create', [
1467 'contact_id' => $membership['contact_id'],
1468 'email' => 'test-member@example.com',
1470 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1472 $this->createScheduleFromFixtures('sched_membership_end_2month_repeat_twice_4_weeks', ['entity_value' => $membership['membership_type_id']]);
1473 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1474 $this->assertCronRuns([
1476 // After the 2-week mark, send an email.
1477 'time' => '2012-08-15 01:00:00',
1478 'recipients' => [['test-member@example.com']],
1482 // Extend membership - reminder should NOT go out.
1483 $this->callAPISuccess('membership', 'create', ['id' => $membership['id'], 'end_date' => '2014-01-01']);
1484 $this->assertCronRuns([
1486 // After the 2-week mark, send an email.
1487 'time' => '2012-09-12 01:00:00',
1494 * Test membership end date email sends.
1496 * For contacts/members which match schedule based on end date,
1497 * an email should be sent.
1499 * @throws \API_Exception
1500 * @throws \CRM_Core_Exception
1501 * @throws \CiviCRM_API3_Exception
1503 public function testMembershipEndDateMatch(): void
{
1504 // creates membership with end_date = 20120615
1505 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1506 $this->callAPISuccess('Email', 'create', [
1507 'contact_id' => $membership['contact_id'],
1508 'email' => 'test-member@example.com',
1510 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1512 $this->createScheduleFromFixtures('sched_membership_end_2week', [
1513 'entity_value' => $membership['membership_type_id'],
1514 'effective_start_date' => '2012-06-01 00:00:00',
1517 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1518 $this->assertCronRuns([
1520 // Before the 2-week mark, no email.
1521 'time' => '2012-05-31 01:00:00',
1525 // After the 2-week mark, send an email.
1526 'time' => '2012-06-01 01:00:00',
1527 'recipients' => [['test-member@example.com']],
1530 // After the email is sent, another one is not sent
1531 'time' => '2012-06-01 02:00:00',
1536 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1537 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1538 $membershipBAO = new CRM_Member_BAO_Membership();
1539 $membershipBAO->id
= $membership['id'];
1540 $membershipBAO->end_date
= '2012-09-15';
1541 $membershipBAO->save();
1543 //change the email id of chosen membership contact to assert
1544 //recipient of not the previously sent mail but the new one
1545 $result = $this->callAPISuccess('Email', 'create', [
1547 'contact_id' => $membership['contact_id'],
1548 'email' => 'member2@example.com',
1550 $this->assertAPISuccess($result);
1552 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1553 $this->assertCronRuns([
1555 // Before the 2-week mark, no email
1556 'time' => '2012-08-31 01:00:00',
1560 // After the 2-week mark, send an email
1561 'time' => '2012-09-01 01:00:00',
1562 'recipients' => [['member2@example.com']],
1565 // After the email is sent, another one is not sent
1566 'time' => '2012-09-01 02:00:00',
1570 $membershipBAO = new CRM_Member_BAO_Membership();
1571 $membershipBAO->id
= $membership['id'];
1572 $membershipBAO->end_date
= '2012-12-15';
1573 $membershipBAO->save();
1574 // end_date=2012-12-15 ; schedule is 2 weeks before end_date
1575 $this->assertCronRuns([
1577 // Before the 2-week mark, no email
1578 'time' => '2012-11-30 01:00:00',
1582 // After the 2-week mark, send an email
1583 'time' => '2012-12-01 01:00:00',
1584 'recipients' => [['member2@example.com']],
1587 // After the email is sent, another one is not sent
1588 'time' => '2012-12-01 02:00:00',
1596 * This test is very similar to testMembershipEndDateMatch, but it adds
1597 * another contact because there was a bug in
1598 * RecipientBuilder::buildRelFirstPass where it was only sending the
1599 * reminder for the first contact returned in a query for renewed
1600 * memberships. Other contacts wouldn't get the mail.
1602 * @throws \API_Exception
1603 * @throws \CRM_Core_Exception
1604 * @throws \CiviCRM_API3_Exception
1606 public function testMultipleMembershipEndDateMatch(): void
{
1607 $contactID = $this->callAPISuccess('Contact', 'create', array_merge($this->fixtures
['contact'], ['email' => 'test-member@example.com']))['id'];
1608 $contactID2 = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_2'])['id'];
1609 $membershipOne = $this->createMembershipFromFixture('rolling_membership', 2, [], ['contact_id' => $contactID]);
1610 $membershipTypeId = $membershipOne['membership_type_id'];
1611 $membershipTwo = $this->createMembershipFromFixture('rolling_membership', 2, [], ['contact_id' => $contactID2, 'membership_type_id' => $membershipTypeId]);
1612 // We are using dates that 'should' be expired but the test expects them not to be
1613 CRM_Core_DAO
::executeQuery('UPDATE civicrm_membership SET status_id = 2 WHERE 1');
1614 $this->createScheduleFromFixtures('sched_membership_end_2week', ['entity_value' => $membershipTypeId]);
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 emails.
1625 'time' => '2012-06-01 01:00:00',
1627 ['test-member@example.com'],
1628 ['test-contact-2@example.com'],
1632 // After the email is sent, another one is not sent
1633 'time' => '2012-06-01 02:00:00',
1638 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1639 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1640 $membershipOneBAO = new CRM_Member_BAO_Membership();
1641 $membershipOneBAO->id
= $membershipOne['id'];
1642 $membershipOneBAO->end_date
= '2012-09-15';
1643 $membershipOneBAO->save();
1644 $membershipTwoBAO = new CRM_Member_BAO_Membership();
1645 $membershipTwoBAO->id
= $membershipTwo['id'];
1646 $membershipTwoBAO->end_date
= '2012-09-15';
1647 $membershipTwoBAO->save();
1649 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1650 $this->assertCronRuns([
1652 // Before the 2-week mark, no email
1653 'time' => '2012-08-31 01:00:00',
1657 // After the 2-week mark, send an email
1658 'time' => '2012-09-01 01:00:00',
1660 ['test-member@example.com'],
1661 ['test-contact-2@example.com'],
1665 // After the email is sent, another one is not sent
1666 'time' => '2012-06-01 02:00:00',
1673 * Test membership end date email.
1675 * For contacts/members which match schedule based on end date,
1676 * an email should be sent.
1678 * @throws \API_Exception
1679 * @throws \CRM_Core_Exception
1680 * @throws \CiviCRM_API3_Exception
1682 public function testMembershipEndDateNoMatch(): void
{
1683 // creates membership with end_date = 20120615
1684 $membership = $this->createMembershipFromFixture('rolling_membership', 'Grace');
1685 $this->callAPISuccess('Email', 'create', [
1686 'contact_id' => $membership['contact_id'],
1687 'email' => 'test-member@example.com',
1689 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1690 $this->createScheduleFromFixtures('sched_membership_end_2month', ['entity_value' => $membership['membership_type_id']]);
1692 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1693 $this->assertCronRuns([
1695 // Before the 2-week mark, no email.
1696 'time' => '2012-05-31 01:00:00',
1700 // After the 2-week mark, no email
1701 'time' => '2013-05-01 01:00:00',
1708 * @throws \CRM_Core_Exception
1710 public function testContactBirthDateNoAnniversary(): void
{
1711 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1712 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1713 $this->createScheduleFromFixtures('sched_contact_birth_day_yesterday');
1714 $this->assertCronRuns([
1716 // On the birthday, no email.
1717 'time' => '2005-07-07 01:00:00',
1721 // The next day, send an email.
1722 'time' => '2005-07-08 20:00:00',
1723 'recipients' => [['test-birth_day@example.com']],
1729 * @throws \CRM_Core_Exception
1731 public function testContactBirthDateAnniversary(): void
{
1732 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1733 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1734 $this->createScheduleFromFixtures('sched_contact_birth_day_anniversary');
1735 $this->assertCronRuns([
1737 // On some random day, no email.
1738 'time' => '2014-03-07 01:00:00',
1742 // On the eve of their 9th birthday, send an email.
1743 'time' => '2014-07-06 20:00:00',
1744 'recipients' => [['test-birth_day@example.com']],
1750 * @throws \CRM_Core_Exception
1752 public function testContactCustomDateNoAnniversary(): void
{
1754 'title' => 'Test_Group',
1755 'name' => 'test_group',
1756 'extends' => ['Individual'],
1757 'style' => 'Inline',
1758 'is_multiple' => FALSE,
1761 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1763 'label' => 'Graduation',
1764 'data_type' => 'Date',
1765 'html_type' => 'Select Date',
1766 'custom_group_id' => $createGroup['id'],
1768 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1769 $contactParams = $this->fixtures
['contact'];
1770 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1771 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1772 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1773 $this->createScheduleFromFixtures('sched_contact_grad_tomorrow', ['entity_value' => "custom_{$createField['id']}"]);
1774 $this->assertCronRuns([
1776 // On some random day, no email.
1777 'time' => '2014-03-07 01:00:00',
1781 // On the eve of their graduation, send an email.
1782 'time' => '2013-12-15 20:00:00',
1783 'recipients' => [['test-member@example.com']],
1786 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1790 * @throws \CRM_Core_Exception
1792 public function testContactCreatedNoAnniversary(): void
{
1793 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1794 $this->createScheduleFromFixtures('sched_contact_created_yesterday');
1795 $this->assertCronRuns([
1797 // On the date created, no email.
1798 'time' => $contact['values'][$contact['id']]['created_date'],
1802 // The next day, send an email.
1803 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1804 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']],
1810 * Test the impact of changing the anniversary.
1812 * @throws \CRM_Core_Exception
1814 public function testContactModifiedAnniversary(): void
{
1815 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1816 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1817 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', ['id' => $contact['id'], 'return' => 'modified_date']);
1818 $actionSchedule = $this->createScheduleFromFixtures('sched_contact_mod_anniversary');
1819 $actionSchedule['effective_start_date'] = date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date']));
1820 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1821 $this->assertCronRuns([
1823 // On some random day, no email.
1824 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1828 // On the eve of 3 years after they were modified, send an email.
1829 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1830 'recipients' => [['test-birth_day@example.com'], ['fixme.domainemail@example.org'], ['domainemail2@example.org']],
1836 * Check that limit_to + an empty recipients doesn't sent to multiple
1839 * @throws \API_Exception
1840 * @throws \CRM_Core_Exception
1841 * @throws \CiviCRM_API3_Exception
1843 public function testMembershipLimitToNone(): void
{
1844 // creates membership with end_date = 20120615
1845 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1846 $result = $this->callAPISuccess('Email', 'create', [
1847 'contact_id' => $membership['contact_id'],
1848 'email' => 'member@example.com',
1850 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1851 $this->callAPISuccess('contact', 'create', ['email' => 'b@c.com', 'contact_type' => 'Individual']);
1853 $this->assertAPISuccess($result);
1855 $this->createScheduleFromFixtures('sched_membership_end_limit_to_none', ['entity_value' => $membership['membership_type_id']]);
1857 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1858 $this->assertCronRuns([
1860 // Before the 2-week mark, no email.
1861 'time' => '2012-05-31 01:00:00',
1868 * Test handling of reference date for memberships.
1870 * @throws \API_Exception
1871 * @throws \CRM_Core_Exception
1872 * @throws \CiviCRM_API3_Exception
1874 public function testMembershipWithReferenceDate(): void
{
1875 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1876 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1878 $this->createScheduleFromFixtures('sched_membership_join_2week', ['entity_value' => $membership['membership_type_id']]);
1880 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1881 $this->assertCronRuns([
1883 // After the 2-week mark, send an email
1884 'time' => '2012-03-29 01:00:00',
1885 'recipients' => [['member@example.com']],
1888 // After the 2-week 1day mark, don't send an email
1889 'time' => '2012-03-30 01:00:00',
1894 //check if reference date is set to membership's join date
1895 //as per the action_start_date chosen for current schedule reminder
1896 $this->assertEquals('2012-03-15 00:00:00',
1897 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership['contact_id'], 'reference_date', 'contact_id')
1900 //change current membership join date that may signifies as membership renewal activity
1901 $membershipBAO = new CRM_Member_BAO_Membership();
1902 $membershipBAO->id
= $membership['id'];
1903 $membershipBAO->join_date
= '2012-03-29';
1904 $membershipBAO->save();
1906 $this->assertCronRuns([
1908 // After the 13 days of the changed join date 2012-03-29, don't send an email
1909 'time' => '2012-04-11 01:00:00',
1913 // After the 2-week of the changed join date 2012-03-29, send an email
1914 'time' => '2012-04-12 01:00:00',
1915 'recipients' => [['member@example.com']],
1918 $this->assertCronRuns([
1920 // It should not re-send on the same day
1921 'time' => '2012-04-12 01:00:00',
1928 * Test multiple membership reminder.
1930 * @throws \API_Exception
1931 * @throws \CRM_Core_Exception
1932 * @throws \CiviCRM_API3_Exception
1934 public function testMembershipOnMultipleReminder(): void
{
1935 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1936 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
1938 // Send email 2 weeks before end_date
1939 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week'];
1940 // Send email on end_date/expiry date
1941 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date'];
1942 $actionScheduleOn['effective_start_date'] = '2012-06-14 00:00:00';
1943 $actionScheduleAfter['effective_end_date'] = '2012-06-15 01:00:00';
1944 // Send email 1 day after end_date/grace period
1945 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date'];
1946 $actionScheduleAfter['effective_start_date'] = '2012-06-15 01:00:00';
1947 $actionScheduleAfter['effective_end_date'] = '2012-06-16 02:00:00';
1948 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership['membership_type_id'];
1949 foreach (['actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter'] as $value) {
1950 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
1953 $this->assertCronRuns(
1956 // 1day 2weeks before membership end date(MED), don't send mail
1957 'time' => '2012-05-31 01:00:00',
1961 // 2 weeks before MED, send an email
1962 'time' => '2012-06-01 01:00:00',
1963 'recipients' => [['member@example.com']],
1966 // 1day before MED, don't send mail
1967 'time' => '2012-06-14 01:00:00',
1971 // On MED, send an email
1972 'time' => '2012-06-15 00:00:00',
1973 'recipients' => [['member@example.com']],
1976 // After 1day of MED, send an email
1977 'time' => '2012-06-16 01:00:00',
1978 'recipients' => [['member@example.com']],
1981 // After 1day 1min of MED, don't send an email
1982 'time' => '2012-06-17 00:01:00',
1988 // Assert the timestamp as of when the emails of respective three reminders as configured
1989 // 2 weeks before, on and 1 day after MED, are sent
1990 $this->assertApproxEquals(
1991 strtotime('2012-06-01 01:00:00'),
1992 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1993 // Variation in test execution time.
1996 $this->assertApproxEquals(
1997 strtotime('2012-06-15 00:00:00'),
1998 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1999 // Variation in test execution time.
2002 $this->assertApproxEquals(
2003 strtotime('2012-06-16 01:00:00'),
2004 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE)),
2005 // Variation in test execution time.
2009 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
2010 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
2011 $membershipBAO = new CRM_Member_BAO_Membership();
2012 $membershipBAO->id
= $membership['id'];
2013 $membershipBAO->end_date
= '2012-06-20';
2014 $membershipBAO->save();
2016 // increase the effective end date to future
2017 $actionScheduleAfter->effective_end_date
= '2012-07-22 00:00:00';
2018 $actionScheduleAfter->save();
2020 $this->callAPISuccess('Contact', 'get', ['id' => $membership['contact_id']]);
2021 $this->assertCronRuns(
2024 // 1day 2weeks before membership end date(MED), don't send mail
2025 'time' => '2012-06-05 01:00:00',
2029 // 2 weeks before MED, send an email
2030 'time' => '2012-06-06 01:00:00',
2031 'recipients' => [['member@example.com']],
2034 // 1day before MED, don't send mail
2035 'time' => '2012-06-19 01:00:00',
2039 // On MED, send an email
2040 'time' => '2012-06-20 00:00:00',
2041 'recipients' => [['member@example.com']],
2044 // After 1day of MED, send an email
2045 'time' => '2012-06-21 01:00:00',
2046 'recipients' => [['member@example.com']],
2049 // After 1day 1min of MED, don't send an email
2050 'time' => '2012-07-21 00:01:00',
2057 * Test reminders sent on custom data anniversary.
2059 * @throws \API_Exception
2060 * @throws \CRM_Core_Exception
2062 public function testContactCustomDate_Anniversary(): void
{
2063 $this->createCustomGroupWithFieldOfType([], 'date');
2064 $contactParams = $this->fixtures
['contact'];
2065 $contactParams[$this->getCustomFieldName('date')] = '2013-12-16';
2066 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
2067 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
2068 $this->fixtures
['sched_contact_grad_anniversary']['entity_value'] = $this->getCustomFieldName('date');
2069 $this->createScheduleFromFixtures('sched_contact_grad_anniversary');
2071 $this->assertCronRuns([
2073 // On some random day, no email.
2074 'time' => '2014-03-07 01:00:00',
2078 // A week after their 5th anniversary of graduation, send an email.
2079 'time' => '2018-12-23 20:00:00',
2080 'recipients' => [['test-member@example.com']],
2086 * Test sched reminder set via registration date.
2088 * @throws \CRM_Core_Exception
2089 * @throws \CiviCRM_API3_Exception
2091 public function testEventTypeRegistrationDate(): void
{
2092 $contact = $this->individualCreate(['email' => 'test-event@example.com']);
2093 //Add it as a participant to an event ending registration - 7 days from now.
2095 'start_date' => date('Ymd', strtotime('-5 day')),
2096 'end_date' => date('Ymd', strtotime('+7 day')),
2097 'registration_start_date' => date('Ymd', strtotime('-5 day')),
2098 'registration_end_date' => date('Ymd', strtotime('+7 day')),
2100 $event = $this->eventCreate($params);
2101 $this->participantCreate(['contact_id' => $contact, 'event_id' => $event['id']]);
2103 //Create a scheduled reminder to send email 7 days before registration date.
2104 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2105 $actionSchedule['start_action_offset'] = 7;
2106 $actionSchedule['start_action_unit'] = 'day';
2107 $actionSchedule['start_action_date'] = 'registration_end_date';
2108 $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id'];
2109 $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', [
2111 'name' => 'Attended',
2113 $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2114 //Run the cron and verify if an email was sent.
2115 $this->assertCronRuns([
2117 'time' => date('Y-m-d'),
2118 'recipients' => [['test-event@example.com']],
2124 'email' => 'test-event2@example.com',
2126 $contact2 = $this->individualCreate($contactParams);
2127 //Create an event with registration end date = 2 week from now.
2128 $params['end_date'] = date('Ymd', strtotime('+2 week'));
2129 $params['registration_end_date'] = date('Ymd', strtotime('+2 week'));
2130 $event2 = $this->eventCreate($params);
2131 $this->participantCreate(['contact_id' => $contact2, 'event_id' => $event2['id']]);
2133 //Assert there is no reminder sent to the contact.
2134 $this->assertCronRuns([
2136 'time' => date('Y-m-d'),
2141 //Modify the sched reminder to be sent 2 week from registration end date.
2142 $this->callAPISuccess('action_schedule', 'create', [
2143 'id' => $actionSched['id'],
2144 'start_action_offset' => 2,
2145 'start_action_unit' => 'week',
2148 //Contact should receive the reminder now.
2149 $this->assertCronRuns([
2151 'time' => date('Y-m-d'),
2152 'recipients' => [['test-event2@example.com']],
2158 * Test sched reminder set via start date.
2160 * @throws \CRM_Core_Exception
2161 * @throws \CiviCRM_API3_Exception
2163 public function testEventTypeStartDate(): void
{
2164 // Create event+participant with start_date = 20120315, end_date = 20120615.
2165 $params = $this->fixtures
['participant'];
2166 $params['event_id'] = $this->callAPISuccess('Event', 'create', array_merge($this->fixtures
['participant']['event_id'], ['event_type_id' => 1]))['id'];
2167 $params['status_id'] = 2;
2168 $params['contact_id'] = $this->individualCreate(array_merge($this->fixtures
['contact'], ['email' => 'test-event@example.com']));
2169 $this->callAPISuccess('Participant', 'create', $params);
2171 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2172 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2173 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2175 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2176 $this->assertCronRuns([
2179 'time' => '2012-03-02 01:00:00',
2184 'time' => '2012-03-08 01:00:00',
2185 'recipients' => [['test-event@example.com']],
2188 // And then nothing else
2189 'time' => '2012-03-16 01:00:00',
2194 // CASE 2: Create a schedule reminder which was created 1 day after the schdule day,
2195 // so it shouldn't deliver reminders schedule to send 1 week before the event start date
2196 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2197 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2198 $actionSchedule['effective_start_date'] = '20120309000000';
2199 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2200 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2201 $this->assertCronRuns([
2204 'time' => '2012-03-02 01:00:00',
2209 'time' => '2012-03-08 01:00:00',
2213 // And then nothing else
2214 'time' => '2012-03-16 01:00:00',
2219 // CASE 3: Create a schedule reminder which is created less then a week before the event start date,
2220 // 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
2221 $actionSchedule = $this->fixtures
['sched_event_type_start_1week_before'];
2222 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'event_type_id');
2223 $actionSchedule['effective_end_date'] = '20120309010000';
2224 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2225 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2226 $this->assertCronRuns([
2229 'time' => '2012-03-02 01:00:00',
2234 'time' => '2012-03-08 01:00:00',
2235 'recipients' => [['test-event@example.com']],
2238 // And then nothing else
2239 'time' => '2012-03-16 01:00:00',
2246 * Test schedule on event end date.
2248 * @throws \CRM_Core_Exception
2250 public function testEventTypeEndDateRepeat(): void
{
2251 // Create event+participant with start_date = 20120315, end_date = 20120615.
2252 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2253 $this->callAPISuccess('Email', 'create', [
2254 'contact_id' => $participant->contact_id
,
2255 'email' => 'test-event@example.com',
2257 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2259 $actionSchedule = $this->fixtures
['sched_event_type_end_2month_repeat_twice_2_weeks'];
2260 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2261 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2263 $this->assertCronRuns([
2266 'time' => '2012-08-13 01:00:00',
2270 // After the 2-month mark, send an email.
2271 'time' => '2012-08-16 01:00:00',
2272 'recipients' => [['test-event@example.com']],
2275 // After 2 months and 1 week, don't repeat yet.
2276 'time' => '2012-08-23 02:00:00',
2280 // After 2 months and 2 weeks
2281 'time' => '2012-08-30 02:00:00',
2282 'recipients' => [['test-event@example.com']],
2285 // After 2 months and 4 week
2286 'time' => '2012-09-13 02:00:00',
2287 'recipients' => [['test-event@example.com']],
2290 // After 2 months and 6 weeks
2291 'time' => '2012-09-27 01:00:00',
2298 * Run a series of cron jobs and make an assertion about email deliveries.
2300 * @param array $cronRuns
2301 * array specifying when to run cron and what messages to expect; each item is an array with keys:
2302 * - time: string, e.g. '2012-06-15 21:00:01'
2303 * - recipients: array(array(string)), list of email addresses which should receive messages
2305 * @throws \CRM_Core_Exception
2306 * @noinspection DisconnectedForeachInstructionInspection
2308 public function assertCronRuns(array $cronRuns): void
{
2309 foreach ($cronRuns as $cronRun) {
2310 CRM_Utils_Time
::setTime($cronRun['time']);
2311 $this->callAPISuccess('job', 'send_reminder', []);
2312 $this->mut
->assertRecipients($cronRun['recipients']);
2313 if (array_key_exists('subjects', $cronRun)) {
2314 $this->mut
->assertSubjects($cronRun['subjects']);
2316 $this->mut
->clearMessages();
2323 * (DAO_Name => array(int)) List of items to garbage-collect during tearDown
2325 private $_testObjects = [];
2328 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
2329 * created entities and provides for brainless cleanup.
2331 * However, it is only really brainless when initially writing the code.
2332 * It 'steals and deletes entities that are part of the 'stock build'.
2334 * In general this causes weird stuff.
2337 * @param array $params
2338 * @param int $numObjects
2339 * @param bool $createOnly
2341 * @return array|NULL|object
2342 * @see CRM_Core_DAO::createTestObject
2344 public function createTestObject($daoName, array $params = [], int $numObjects = 1, bool $createOnly = FALSE) {
2345 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
2346 if (is_array($objects)) {
2347 $this->registerTestObjects($objects);
2350 $this->registerTestObjects([$objects]);
2356 * @param array $objects
2357 * DAO or BAO objects.
2359 public function registerTestObjects(array $objects): void
{
2360 //if (is_object($objects)) {
2361 // $objects = array($objects);
2363 foreach ($objects as $object) {
2364 $daoName = str_replace('_BAO_', '_DAO_', get_class($object));
2365 $this->_testObjects
[$daoName][] = $object->id
;
2369 public function deleteTestObjects(): void
{
2370 // Note: You might argue that the FK relations between test
2371 // objects could make this problematic; however, it should
2372 // behave intuitively as long as we mentally split our
2373 // test-objects between the "manual/primary records"
2374 // and the "automatic/secondary records"
2375 foreach ($this->_testObjects
as $daoName => $daoIds) {
2376 foreach ($daoIds as $daoId) {
2377 CRM_Core_DAO
::deleteTestObjects($daoName, ['id' => $daoId]);
2380 $this->_testObjects
= [];
2384 * Test that the various repetition units work correctly.
2386 * @see https://issues.civicrm.org/jira/browse/CRM-17028
2387 * @throws \CRM_Core_Exception
2389 public function testRepetitionFrequencyUnit(): void
{
2390 $membershipTypeParams = [
2391 'duration_interval' => '1',
2392 'duration_unit' => 'year',
2394 'period_type' => 'rolling',
2396 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
2397 $interval_units = ['hour', 'day', 'week', 'month', 'year'];
2398 foreach ($interval_units as $interval_unit) {
2399 $membershipEndDate = DateTime
::createFromFormat('Y-m-d H:i:s', '2013-03-15 00:00:00');
2401 'contact_type' => 'Individual',
2402 'first_name' => 'Test',
2403 'last_name' => "Interval $interval_unit",
2406 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2408 'contact_id' => $contact->id
,
2410 'email' => "test-member-$interval_unit@example.com",
2411 'location_type_id' => 1,
2413 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2414 $membershipParams = [
2415 'membership_type_id' => $membershipType->id
,
2416 'contact_id' => $contact->id
,
2417 'join_date' => '20120315',
2418 'start_date' => '20120315',
2419 'end_date' => '20130315',
2423 $membershipParams['status-id'] = 1;
2424 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2425 $actionScheduleParams = $this->fixtures
['sched_on_membership_end_date_repeat_interval'];
2426 $actionScheduleParams['entity_value'] = $membershipType->id
;
2427 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
2428 $actionScheduleParams['repetition_frequency_interval'] = 2;
2429 $actionSchedule = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
2430 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
2431 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
2432 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
2435 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
2439 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
2440 'recipients' => [["test-member-$interval_unit@example.com"]],
2443 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
2447 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
2448 'recipients' => [["test-member-$interval_unit@example.com"]],
2451 $this->assertCronRuns($cronRuns);
2452 $actionSchedule->delete();
2453 $membership->delete();
2458 * Inherited members without permission to edit the main member contact should
2459 * not get reminders.
2461 * However, just because a contact inherits one membership doesn't mean
2462 * reminders for other memberships should be suppressed.
2466 * @throws \CRM_Core_Exception
2468 public function testInheritedMembershipPermissions(): void
{
2469 // Set up common parameters for memberships.
2470 $membershipParams = $this->fixtures
['rolling_membership'];
2471 $membershipParams['status_id'] = 1;
2473 $membershipParams['membership_type_id']['relationship_type_id'] = 1;
2474 $membershipParams['membership_type_id']['relationship_direction'] = 'b_a';
2475 $membershipType1 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2477 // We'll create a new membership type that can be held at the same time as
2479 $membershipParams['membership_type_id']['relationship_type_id'] = 'NULL';
2480 $membershipParams['membership_type_id']['relationship_direction'] = 'NULL';
2481 $membershipType2 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2483 // Create the parent membership and contact
2484 $membershipParams['membership_type_id'] = $membershipType1->id
;
2485 $mainMembership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2488 'contact_type' => 'Individual',
2489 'first_name' => 'Mom',
2490 'last_name' => 'Rel',
2493 $this->createTestObject('CRM_Contact_DAO_Contact', array_merge($contactParams, ['id' => $mainMembership->contact_id
]));
2496 'contact_id' => $mainMembership->contact_id
,
2497 'email' => 'test-member@example.com',
2498 'location_type_id' => 1,
2501 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2503 // Set up contacts and emails for the two children
2504 $contactParams['first_name'] = 'Favorite';
2505 $permChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2506 $emailParams['email'] = 'favorite@example.com';
2507 $emailParams['contact_id'] = $permChild->id
;
2508 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2510 $contactParams['first_name'] = 'Black Sheep';
2511 $nonPermChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2512 $emailParams['email'] = 'black.sheep@example.com';
2513 $emailParams['contact_id'] = $nonPermChild->id
;
2514 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2516 // Each child gets a relationship, one with permission to edit the parent. This
2517 // will trigger inherited memberships for the first membership type
2519 'relationship_type_id' => 1,
2520 'contact_id_a' => $nonPermChild->id
,
2521 'contact_id_b' => $mainMembership->contact_id
,
2524 $this->callAPISuccess('relationship', 'create', $relParams);
2526 $relParams['contact_id_a'] = $permChild->id
;
2527 $relParams['is_permission_a_b'] = CRM_Contact_BAO_Relationship
::EDIT
;
2528 $this->callAPISuccess('relationship', 'create', $relParams);
2530 // Mom and Black Sheep get their own memberships of the second type.
2531 $membershipParams['membership_type_id'] = $membershipType2->id
;
2532 $membershipParams['owner_membership_id'] = 'NULL';
2533 $membershipParams['contact_id'] = $mainMembership->contact_id
;
2534 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2536 $membershipParams['contact_id'] = $nonPermChild->id
;
2537 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2539 // Test a reminder for the first membership type - that should exclude Black
2541 $this->fixtures
['sched_membership_join_2week']['entity_value'] = $membershipType1->id
;
2542 $this->createScheduleFromFixtures('sched_membership_join_2week');
2544 $this->assertCronRuns([
2546 'time' => '2012-03-29 01:00:00',
2547 'recipients' => [['test-member@example.com'], ['favorite@example.com']],
2549 'subject sched_membership_join_2week (joined March 15th, 2012)',
2550 'subject sched_membership_join_2week (joined March 15th, 2012)',
2555 // Test a reminder for the second membership type - that should include
2557 $this->fixtures
['sched_membership_start_1week']['entity_value'] = $membershipType2->id
;
2558 $this->createScheduleFromFixtures('sched_membership_start_1week');
2560 $this->assertCronRuns([
2562 'time' => '2012-03-22 01:00:00',
2563 'recipients' => [['test-member@example.com'], ['black.sheep@example.com']],
2565 'subject sched_membership_start_1week (joined March 15th, 2012)',
2566 'subject sched_membership_start_1week (joined March 15th, 2012)',
2573 * Modify the date time by the modify rule.
2575 * @param DateTime $origDateTime
2576 * @param string $modifyRule
2580 public function createModifiedDateTime(DateTime
$origDateTime, string $modifyRule): DateTime
{
2581 $newDateTime = clone($origDateTime);
2582 $newDateTime->modify($modifyRule);
2583 return $newDateTime;
2587 * Test absolute date handling for membership.
2589 * @throws \API_Exception
2590 * @throws \CRM_Core_Exception
2591 * @throws \CiviCRM_API3_Exception
2592 * @throws \Civi\API\Exception\UnauthorizedException
2594 public function testMembershipScheduleWithAbsoluteDate(): void
{
2595 $membership = $this->createMembershipFromFixture('rolling_membership', 'New', [
2596 'email' => 'test-member@example.com',
2597 'location_type_id' => 1,
2600 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership['contact_id']]));
2601 $this->fixtures
['sched_membership_absolute_date']['entity_value'] = $membership['membership_type_id'];
2602 $this->createScheduleFromFixtures('sched_membership_absolute_date');
2604 $this->assertCronRuns([
2606 // Before the 24-hour mark, no email
2607 'time' => '2012-06-13 04:00:00',
2612 // On absolute date set on 2012-06-14
2613 'time' => '2012-06-14 00:00:00',
2614 'recipients' => [['test-member@example.com']],
2615 'subjects' => ['subject sched_membership_absolute_date'],
2618 // Run cron 4 hours later; first message already sent
2619 'time' => '2012-06-14 04:00:00',
2627 * @param string $fixture
2628 * Key from $this->fixtures
2629 * @param string $status
2631 * @param array $emailParams
2632 * @param array $membershipOverrides
2635 * @throws \API_Exception
2636 * @throws \CRM_Core_Exception
2637 * @throws \CiviCRM_API3_Exception
2638 * @throws \Civi\API\Exception\UnauthorizedException
2640 protected function createMembershipFromFixture(string $fixture, string $status, array $emailParams = [], array $membershipOverrides = []): array {
2641 $membershipTypeID = $membershipOverrides['membership_type_id'] ??
$this->fixtures
[$fixture]['membership_type_id'];
2642 if (is_array($membershipTypeID)) {
2643 $membershipTypeID = MembershipType
::create()->setValues(array_merge([
2644 'member_of_contact_id' => 1,
2645 'financial_type_id:name' => 'Member Dues',
2646 'name' => 'fixture-created-type',
2647 ], $this->fixtures
[$fixture]['membership_type_id']))->execute()->first()['id'];
2649 $params = array_merge($this->fixtures
[$fixture], [
2651 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', $status),
2652 'membership_type_id' => $membershipTypeID,
2653 ], $membershipOverrides);
2654 if (empty($params['contact_id'])) {
2655 $params['contact_id'] = $this->individualCreate(['email' => '']);
2657 $membership = (array) $this->callAPISuccess('Membership', 'create', $params)['values'][0];
2659 Civi\Api4\Email
::create(FALSE)->setValues(array_merge([
2660 'contact_id' => $membership['contact_id'],
2661 'location_type_id' => 1,
2662 ], $emailParams))->execute();
2668 * Create action schedule from defined fixtures.
2670 * @param string $fixture
2671 * @param array $extraParams
2673 * @throws \CRM_Core_Exception
2675 protected function createScheduleFromFixtures(string $fixture, array $extraParams = []): void
{
2676 $id = $this->callAPISuccess('ActionSchedule', 'create', array_merge($this->fixtures
[$fixture], $extraParams))['id'];
2677 $this->fixtures
[$fixture]['action_schedule_id'] = (int) $id;
2681 * @param string $activityKey
2682 * @param string $contactKey
2684 * @throws \CRM_Core_Exception
2686 protected function createActivityAndContactFromFixtures(string $activityKey = 'phone_call', string $contactKey = 'contact'): void
{
2687 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
[$activityKey]);
2688 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
[$contactKey]);
2692 $source['contact_id'] = $contact['id'];
2693 $source['activity_id'] = $activity->id
;
2694 $source['record_type_id'] = 2;
2695 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
2696 $activityContact->save();