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 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_ActionScheduleTest
14 * @group ActionSchedule
17 * There are additional tests for some specific entities in other classes:
18 * @see CRM_Activity_ActionMappingTest
19 * @see CRM_Contribute_ActionMapping_ByTypeTest
21 class CRM_Core_BAO_ActionScheduleTest
extends CiviUnitTestCase
{
28 public function setUp() {
31 $this->mut
= new CiviMailUtils($this, TRUE);
33 $this->fixtures
['rolling_membership_type'] = [
34 'period_type' => 'rolling',
35 'duration_unit' => 'month',
36 'duration_interval' => '3',
39 'financial_type_id' => 2,
42 $this->fixtures
['rolling_membership'] = [
43 'membership_type_id' => [
44 'period_type' => 'rolling',
45 'duration_unit' => 'month',
46 'duration_interval' => '3',
49 'join_date' => '20120315',
50 'start_date' => '20120315',
51 'end_date' => '20120615',
55 $this->fixtures
['rolling_membership_past'] = [
56 'membership_type_id' => [
57 'period_type' => 'rolling',
58 'duration_unit' => 'month',
59 'duration_interval' => '3',
62 'join_date' => '20100310',
63 'start_date' => '20100310',
64 'end_date' => '20100610',
65 'is_override' => 'NULL',
67 $this->fixtures
['participant'] = [
71 'title' => 'Example Event',
72 'start_date' => '20120315',
73 'end_date' => '20120615',
81 $this->fixtures
['phonecall'] = [
83 'activity_type_id' => 2,
84 'activity_date_time' => '20120615100000',
85 'is_current_revision' => 1,
88 $this->fixtures
['contact'] = [
90 'contact_type' => 'Individual',
91 'email' => 'test-member@example.com',
92 'gender_id' => 'Female',
93 'first_name' => 'Churmondleia',
94 'last_name' => 'Ōtākou',
96 $this->fixtures
['contact_2'] = [
98 'contact_type' => 'Individual',
99 'email' => 'test-contact-2@example.com',
100 'gender_id' => 'Male',
101 'first_name' => 'Fabble',
104 $this->fixtures
['contact_birthdate'] = [
106 'contact_type' => 'Individual',
107 'email' => 'test-bday@example.com',
108 'birth_date' => '20050707',
110 $this->fixtures
['sched_activity_1day'] = [
111 'name' => 'One_Day_Phone_Call_Notice',
112 'title' => 'One Day Phone Call Notice',
114 'absolute_date' => NULL,
115 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
116 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
117 'end_action' => NULL,
119 'end_frequency_interval' => NULL,
120 'end_frequency_unit' => NULL,
121 'entity_status' => '1',
122 'entity_value' => '2',
127 'msg_template_id' => NULL,
129 'recipient_listing' => NULL,
130 'recipient_manual' => NULL,
131 'record_activity' => 1,
132 'repetition_frequency_interval' => NULL,
133 'repetition_frequency_unit' => NULL,
134 'start_action_condition' => 'before',
135 'start_action_date' => 'activity_date_time',
136 'start_action_offset' => '1',
137 'start_action_unit' => 'day',
138 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
140 $this->fixtures
['sched_activity_1day_r'] = [
141 'name' => 'One_Day_Phone_Call_Notice_R',
142 'title' => 'One Day Phone Call Notice R',
144 'absolute_date' => NULL,
145 'body_html' => '<p>1-Day (repeating)</p>',
146 'body_text' => '1-Day (repeating)',
147 'end_action' => 'after',
148 'end_date' => 'activity_date_time',
149 'end_frequency_interval' => '2',
150 'end_frequency_unit' => 'day',
151 'entity_status' => '1',
152 'entity_value' => '2',
157 'msg_template_id' => NULL,
159 'recipient_listing' => NULL,
160 'recipient_manual' => NULL,
161 'record_activity' => NULL,
162 'repetition_frequency_interval' => '6',
163 'repetition_frequency_unit' => 'hour',
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 (repeating) (about {activity.activity_type})',
170 $this->fixtures
['sched_activity_1day_r_on_abs_date'] = [
171 'name' => 'One_Day_Phone_Call_Notice_R',
172 'title' => 'One Day Phone Call Notice R',
174 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
175 'body_html' => '<p>1-Day (repeating)</p>',
176 'body_text' => '1-Day (repeating)',
177 'entity_status' => '1',
178 'entity_value' => '2',
183 'msg_template_id' => NULL,
185 'recipient_listing' => NULL,
186 'recipient_manual' => NULL,
187 'record_activity' => NULL,
188 'repetition_frequency_interval' => '6',
189 'repetition_frequency_unit' => 'hour',
190 'end_action' => 'after',
191 'end_date' => 'activity_date_time',
192 'end_frequency_interval' => '2',
193 'end_frequency_unit' => 'day',
194 'start_action_condition' => '',
195 'start_action_date' => '',
196 'start_action_offset' => '',
197 'start_action_unit' => '',
198 'subject' => '1-Day (repeating) (about {activity.activity_type})',
200 $this->fixtures
['sched_eventname_1day_on_abs_date'] = [
201 'name' => 'sched_eventname_1day_on_abs_date',
202 'title' => 'sched_eventname_1day_on_abs_date',
204 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
205 'body_html' => '<p>sched_eventname_1day_on_abs_date</p>',
206 'body_text' => 'sched_eventname_1day_on_abs_date',
207 'entity_status' => '1',
208 'entity_value' => '2',
213 'msg_template_id' => NULL,
215 'recipient_listing' => NULL,
216 'recipient_manual' => NULL,
217 'record_activity' => NULL,
218 'repetition_frequency_interval' => NULL,
219 'repetition_frequency_unit' => NULL,
220 'end_action' => NULL,
222 'end_frequency_interval' => NULL,
223 'end_frequency_unit' => NULL,
224 'start_action_condition' => NULL,
225 'start_action_date' => NULL,
226 'start_action_offset' => NULL,
227 'start_action_unit' => NULL,
228 'subject' => 'sched_eventname_1day_on_abs_date',
230 $this->fixtures
['sched_membership_join_2week'] = [
231 'name' => 'sched_membership_join_2week',
232 'title' => 'sched_membership_join_2week',
233 'absolute_date' => '',
234 'body_html' => '<p>body sched_membership_join_2week</p>',
235 'body_text' => 'body sched_membership_join_2week',
238 'end_frequency_interval' => '',
239 'end_frequency_unit' => '',
240 'entity_status' => '',
241 'entity_value' => '',
246 'msg_template_id' => '',
248 'recipient_listing' => '',
249 'recipient_manual' => '',
250 'record_activity' => 1,
251 'repetition_frequency_interval' => '',
252 'repetition_frequency_unit' => '',
253 'start_action_condition' => 'after',
254 'start_action_date' => 'membership_join_date',
255 'start_action_offset' => '2',
256 'start_action_unit' => 'week',
257 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
259 $this->fixtures
['sched_membership_start_1week'] = [
260 'name' => 'sched_membership_start_1week',
261 'title' => 'sched_membership_start_1week',
262 'absolute_date' => '',
263 'body_html' => '<p>body sched_membership_start_1week</p>',
264 'body_text' => 'body sched_membership_start_1week',
267 'end_frequency_interval' => '',
268 'end_frequency_unit' => '',
269 'entity_status' => '',
270 'entity_value' => '',
275 'msg_template_id' => '',
277 'recipient_listing' => '',
278 'recipient_manual' => '',
279 'record_activity' => 1,
280 'repetition_frequency_interval' => '',
281 'repetition_frequency_unit' => '',
282 'start_action_condition' => 'after',
283 'start_action_date' => 'membership_start_date',
284 'start_action_offset' => '1',
285 'start_action_unit' => 'week',
286 'subject' => 'subject sched_membership_start_1week (joined {membership.start_date})',
288 $this->fixtures
['sched_membership_end_2week'] = [
289 'name' => 'sched_membership_end_2week',
290 'title' => 'sched_membership_end_2week',
291 'absolute_date' => '',
292 'body_html' => '<p>body sched_membership_end_2week</p>',
293 'body_text' => 'body sched_membership_end_2week',
296 'end_frequency_interval' => '',
297 'end_frequency_unit' => '',
298 'entity_status' => '',
299 'entity_value' => '',
304 'msg_template_id' => '',
306 'recipient_listing' => '',
307 'recipient_manual' => '',
308 'record_activity' => 1,
309 'repetition_frequency_interval' => '',
310 'repetition_frequency_unit' => '',
311 'start_action_condition' => 'before',
312 'start_action_date' => 'membership_end_date',
313 'start_action_offset' => '2',
314 'start_action_unit' => 'week',
315 'subject' => 'subject sched_membership_end_2week',
317 $this->fixtures
['sched_on_membership_end_date'] = [
318 'name' => 'sched_on_membership_end_date',
319 'title' => 'sched_on_membership_end_date',
320 'body_html' => '<p>Your membership expired today</p>',
321 'body_text' => 'Your membership expired today',
324 'record_activity' => 1,
325 'start_action_condition' => 'after',
326 'start_action_date' => 'membership_end_date',
327 'start_action_offset' => '0',
328 'start_action_unit' => 'hour',
329 'subject' => 'subject send reminder on membership_end_date',
331 $this->fixtures
['sched_after_1day_membership_end_date'] = [
332 'name' => 'sched_after_1day_membership_end_date',
333 'title' => 'sched_after_1day_membership_end_date',
334 'body_html' => '<p>Your membership expired yesterday</p>',
335 'body_text' => 'Your membership expired yesterday',
338 'record_activity' => 1,
339 'start_action_condition' => 'after',
340 'start_action_date' => 'membership_end_date',
341 'start_action_offset' => '1',
342 'start_action_unit' => 'day',
343 'subject' => 'subject send reminder on membership_end_date',
346 $this->fixtures
['sched_membership_end_2month'] = [
347 'name' => 'sched_membership_end_2month',
348 'title' => 'sched_membership_end_2month',
349 'absolute_date' => '',
350 'body_html' => '<p>body sched_membership_end_2month</p>',
351 'body_text' => 'body sched_membership_end_2month',
354 'end_frequency_interval' => '',
355 'end_frequency_unit' => '',
356 'entity_status' => '',
357 'entity_value' => '',
362 'msg_template_id' => '',
364 'recipient_listing' => '',
365 'recipient_manual' => '',
366 'record_activity' => 1,
367 'repetition_frequency_interval' => '',
368 'repetition_frequency_unit' => '',
369 'start_action_condition' => 'after',
370 'start_action_date' => 'membership_end_date',
371 'start_action_offset' => '2',
372 'start_action_unit' => 'month',
373 'subject' => 'subject sched_membership_end_2month',
376 $this->fixtures
['sched_membership_absolute_date'] = [
377 'name' => 'sched_membership_absolute_date',
378 'title' => 'sched_membership_absolute_date',
379 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
380 'body_html' => '<p>body sched_membership_absolute_date</p>',
381 'body_text' => 'body sched_membership_absolute_date',
384 'end_frequency_interval' => '',
385 'end_frequency_unit' => '',
386 'entity_status' => '',
387 'entity_value' => '',
392 'msg_template_id' => '',
394 'recipient_listing' => '',
395 'recipient_manual' => '',
396 'record_activity' => 1,
397 'repetition_frequency_interval' => '',
398 'repetition_frequency_unit' => '',
399 'start_action_condition' => '',
400 'start_action_date' => '',
401 'start_action_offset' => '',
402 'start_action_unit' => '',
403 'subject' => 'subject sched_membership_absolute_date',
406 $this->fixtures
['sched_contact_bday_yesterday'] = [
407 'name' => 'sched_contact_bday_yesterday',
408 'title' => 'sched_contact_bday_yesterday',
409 'absolute_date' => '',
410 'body_html' => '<p>you look like you were born yesterday!</p>',
411 'body_text' => 'you look like you were born yesterday!',
414 'end_frequency_interval' => '',
415 'end_frequency_unit' => '',
416 'entity_status' => 1,
417 'entity_value' => 'birth_date',
422 'msg_template_id' => '',
424 'recipient_listing' => '',
425 'recipient_manual' => '',
426 'record_activity' => 1,
427 'repetition_frequency_interval' => '',
428 'repetition_frequency_unit' => '',
429 'start_action_condition' => 'after',
430 'start_action_date' => 'date_field',
431 'start_action_offset' => '1',
432 'start_action_unit' => 'day',
433 'subject' => 'subject sched_contact_bday_yesterday',
436 $this->fixtures
['sched_contact_bday_anniv'] = [
437 'name' => 'sched_contact_bday_anniv',
438 'title' => 'sched_contact_bday_anniv',
439 'absolute_date' => '',
440 'body_html' => '<p>happy birthday!</p>',
441 'body_text' => 'happy birthday!',
444 'end_frequency_interval' => '',
445 'end_frequency_unit' => '',
446 'entity_status' => 2,
447 'entity_value' => 'birth_date',
452 'msg_template_id' => '',
454 'recipient_listing' => '',
455 'recipient_manual' => '',
456 'record_activity' => 1,
457 'repetition_frequency_interval' => '',
458 'repetition_frequency_unit' => '',
459 'start_action_condition' => 'before',
460 'start_action_date' => 'date_field',
461 'start_action_offset' => '1',
462 'start_action_unit' => 'day',
463 'subject' => 'subject sched_contact_bday_anniv',
466 $this->fixtures
['sched_contact_grad_tomorrow'] = [
467 'name' => 'sched_contact_grad_tomorrow',
468 'title' => 'sched_contact_grad_tomorrow',
469 'absolute_date' => '',
470 'body_html' => '<p>congratulations on your graduation!</p>',
471 'body_text' => 'congratulations on your graduation!',
474 'end_frequency_interval' => '',
475 'end_frequency_unit' => '',
476 'entity_status' => 1,
481 'msg_template_id' => '',
483 'recipient_listing' => '',
484 'recipient_manual' => '',
485 'record_activity' => 1,
486 'repetition_frequency_interval' => '',
487 'repetition_frequency_unit' => '',
488 'start_action_condition' => 'before',
489 'start_action_date' => 'date_field',
490 'start_action_offset' => '1',
491 'start_action_unit' => 'day',
492 'subject' => 'subject sched_contact_grad_tomorrow',
495 $this->fixtures
['sched_contact_grad_anniv'] = [
496 'name' => 'sched_contact_grad_anniv',
497 'title' => 'sched_contact_grad_anniv',
498 'absolute_date' => '',
499 'body_html' => '<p>dear alum, please send us money.</p>',
500 'body_text' => 'dear alum, please send us money.',
503 'end_frequency_interval' => '',
504 'end_frequency_unit' => '',
505 'entity_status' => 2,
510 'msg_template_id' => '',
512 'recipient_listing' => '',
513 'recipient_manual' => '',
514 'record_activity' => 1,
515 'repetition_frequency_interval' => '',
516 'repetition_frequency_unit' => '',
517 'start_action_condition' => 'after',
518 'start_action_date' => 'date_field',
519 'start_action_offset' => '1',
520 'start_action_unit' => 'week',
521 'subject' => 'subject sched_contact_grad_anniv',
524 $this->fixtures
['sched_contact_created_yesterday'] = [
525 'name' => 'sched_contact_created_yesterday',
526 'title' => 'sched_contact_created_yesterday',
527 'absolute_date' => '',
528 'body_html' => '<p>Your contact was created yesterday</p>',
529 'body_text' => 'Your contact was created yesterday!',
532 'end_frequency_interval' => '',
533 'end_frequency_unit' => '',
534 'entity_status' => 1,
535 'entity_value' => 'created_date',
540 'msg_template_id' => '',
542 'recipient_listing' => '',
543 'recipient_manual' => '',
544 'record_activity' => 1,
545 'repetition_frequency_interval' => '',
546 'repetition_frequency_unit' => '',
547 'start_action_condition' => 'after',
548 'start_action_date' => 'date_field',
549 'start_action_offset' => '1',
550 'start_action_unit' => 'day',
551 'subject' => 'subject sched_contact_created_yesterday',
554 $this->fixtures
['sched_contact_mod_anniv'] = [
555 'name' => 'sched_contact_mod_anniv',
556 'title' => 'sched_contact_mod_anniv',
557 'absolute_date' => '',
558 'body_html' => '<p>You last updated your data last year</p>',
559 'body_text' => 'Go update your stuff!',
562 'end_frequency_interval' => '',
563 'end_frequency_unit' => '',
564 'entity_status' => 2,
565 'entity_value' => 'modified_date',
570 'msg_template_id' => '',
572 'recipient_listing' => '',
573 'recipient_manual' => '',
574 'record_activity' => 1,
575 'repetition_frequency_interval' => '',
576 'repetition_frequency_unit' => '',
577 'start_action_condition' => 'before',
578 'start_action_date' => 'date_field',
579 'start_action_offset' => '1',
580 'start_action_unit' => 'day',
581 'subject' => 'subject sched_contact_mod_anniv',
584 $this->fixtures
['sched_eventtype_start_1week_before'] = [
585 'name' => 'sched_eventtype_start_1week_before',
586 'title' => 'sched_eventtype_start_1week_before',
587 'absolute_date' => '',
588 'body_html' => '<p>body sched_eventtype_start_1week_before ({event.title})</p>',
589 'body_text' => 'body sched_eventtype_start_1week_before ({event.title})',
592 'end_frequency_interval' => '',
593 'end_frequency_unit' => '',
594 // participant status id
595 'entity_status' => '',
597 'entity_value' => '',
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' => 'event_start_date',
612 'start_action_offset' => '1',
613 'start_action_unit' => 'week',
614 'subject' => 'subject sched_eventtype_start_1week_before ({event.title})',
616 $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'] = [
617 'name' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
618 'title' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
619 'absolute_date' => '',
620 'body_html' => '<p>body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}</p>',
621 'body_text' => 'body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
622 'end_action' => 'after',
623 'end_date' => 'event_end_date',
624 'end_frequency_interval' => '3',
625 'end_frequency_unit' => 'month',
626 // participant status id
627 'entity_status' => '',
629 'entity_value' => '',
635 'msg_template_id' => '',
637 'recipient_listing' => '',
638 'recipient_manual' => '',
639 'record_activity' => 1,
640 'repetition_frequency_interval' => '2',
641 'repetition_frequency_unit' => 'week',
642 'start_action_condition' => 'after',
643 'start_action_date' => 'event_end_date',
644 'start_action_offset' => '2',
645 'start_action_unit' => 'month',
646 'subject' => 'subject sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
649 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = [
650 'name' => 'sched_membership_end_2month',
651 'title' => 'sched_membership_end_2month',
652 'absolute_date' => '',
653 'body_html' => '<p>body sched_membership_end_2month</p>',
654 'body_text' => 'body sched_membership_end_2month',
656 'end_date' => 'membership_end_date',
657 'end_frequency_interval' => '4',
658 'end_frequency_unit' => 'month',
659 'entity_status' => '',
660 'entity_value' => '',
665 'msg_template_id' => '',
667 'recipient_listing' => '',
668 'recipient_manual' => '',
669 'record_activity' => 1,
670 'repetition_frequency_interval' => '4',
671 'repetition_frequency_unit' => 'week',
672 'start_action_condition' => 'after',
673 'start_action_date' => 'membership_end_date',
674 'start_action_offset' => '2',
675 'start_action_unit' => 'month',
676 'subject' => 'subject sched_membership_end_2month',
678 $this->fixtures
['sched_membership_end_limit_to_none'] = [
679 'name' => 'limit to none',
680 'title' => 'limit to none',
681 'absolute_date' => '',
682 'body_html' => '<p>body sched_membership_end_2month</p>',
683 'body_text' => 'body sched_membership_end_2month',
686 'end_frequency_interval' => '4',
687 'end_frequency_unit' => 'month',
688 'entity_status' => '',
689 'entity_value' => '',
695 'msg_template_id' => '',
697 'recipient_listing' => '',
698 'recipient_manual' => '',
699 'record_activity' => 1,
700 'repetition_frequency_interval' => '4',
701 'repetition_frequency_unit' => 'week',
702 'start_action_condition' => 'after',
703 'start_action_date' => 'membership_end_date',
704 'start_action_offset' => '2',
705 'start_action_unit' => 'month',
706 'subject' => 'limit to none',
708 $this->fixtures
['sched_on_membership_end_date_repeat_interval'] = [
709 'name' => 'sched_on_membership_end_date',
710 'title' => 'sched_on_membership_end_date',
711 'body_html' => '<p>Your membership expired 1 unit ago</p>',
712 'body_text' => 'Your membership expired 1 unit ago',
713 'end_frequency_interval' => 10,
714 'end_frequency_unit' => 'year',
718 'record_activity' => 1,
719 'start_action_condition' => 'after',
720 'start_action_date' => 'membership_end_date',
721 'start_action_offset' => '0',
722 'start_action_unit' => 'hour',
723 'subject' => 'subject send reminder every unit after membership_end_date',
726 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
727 'title' => ts('Test Contact Custom group'),
728 'name' => 'test_contact_cg',
729 'extends' => 'Contact',
730 'domain_id' => CRM_Core_Config
::domainID(),
732 'collapse_adv_display' => 0,
733 'collapse_display' => 0,
735 $customField = $this->callAPISuccess('CustomField', 'create', [
736 'label' => 'Test Text',
737 'data_type' => 'String',
738 'html_type' => 'Text',
739 'custom_group_id' => $customGroup['id'],
741 $this->fixtures
['contact_custom_token'] = [
742 'id' => $customField['id'],
743 'token' => sprintf('{contact.custom_%s}', $customField['id']),
744 'name' => sprintf('custom_%s', $customField['id']),
745 'value' => 'text ' . substr(sha1(rand()), 0, 7),
752 * Tears down the fixture, for example, closes a network connection.
754 * This method is called after a test is executed.
756 public function tearDown() {
758 $this->mut
->clearMessages();
761 $this->quickCleanup([
762 'civicrm_action_schedule',
763 'civicrm_action_log',
764 'civicrm_membership',
765 'civicrm_participant',
769 $this->callAPISuccess('CustomField', 'delete', ['id' => $this->fixtures
['contact_custom_token']['id']]);
770 $this->callAPISuccess('CustomGroup', 'delete', [
771 'id' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', 'test_contact_cg', 'id', 'name'),
776 public function mailerExamples() {
779 // Some tokens - short as subject has 128char limit in DB.
780 $someTokensTmpl = implode(';;', [
781 // basic contact token
782 '{contact.display_name}',
783 // funny legacy contact token
785 // funny legacy contact token
786 '{contact.gender_id}',
789 // action-scheduler token
790 '{activity.activity_type}',
792 // Further tokens can be tested in the body text/html.
793 $manyTokensTmpl = implode(';;', [
795 '{contact.email_greeting}',
796 $this->fixture
['contact_custom_token']['token'],
798 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
799 // can really do is check that it has something.
800 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
801 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, $this->fixture
['contact_custom_token']['value']);
803 // In this example, we use a lot of tokens cutting across multiple components.
805 // Schedule definition.
807 'subject' => "subj $someTokensTmpl",
808 'body_html' => "html $manyTokensTmpl",
809 'body_text' => "text $manyTokensTmpl",
811 // Assertions (regex).
813 'from_name' => "/^FIXME\$/",
814 'from_email' => "/^info@EXAMPLE.ORG\$/",
815 'subject' => "/^subj $someTokensExpected\$/",
816 'body_html' => "/^html $manyTokensExpected\$/",
817 'body_text' => "/^text $manyTokensExpected\$/",
821 // In this example, we customize the from address.
823 // Schedule definition.
825 'from_name' => 'Bob',
826 'from_email' => 'bob@example.org',
828 // Assertions (regex).
830 'from_name' => "/^Bob\$/",
831 'from_email' => "/^bob@example.org\$/",
835 // In this example, we autoconvert HTML to text
837 // Schedule definition.
839 'body_html' => '<p>Hello & stuff.</p>',
842 // Assertions (regex).
844 'body_html' => '/^' . preg_quote('<p>Hello & stuff.</p>', '/') . '/',
845 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
849 // In this example, we autoconvert HTML to text
851 // Schedule definition.
854 'body_text' => 'Hello world',
856 // Assertions (regex).
858 'body_html' => '/^--UNDEFINED--$/',
859 'body_text' => '/^Hello world$/',
867 * This generates a single mailing through the scheduled-reminder
868 * system (using an activity-reminder as a baseline) and
869 * checks that the resulting message satisfies various
870 * regular expressions.
872 * @param array $schedule
873 * Values to set/override in the schedule.
874 * Ex: array('subject' => 'Hello, {contact.first_name}!').
875 * @param array $patterns
876 * A list of regexes to compare with the actual email.
877 * Ex: array('subject' => '/^Hello, Alice!/').
878 * Keys: subject, body_text, body_html, from_name, from_email.
879 * @dataProvider mailerExamples
881 public function testMailer($schedule, $patterns) {
882 $actionSchedule = array_merge($this->fixtures
['sched_activity_1day'], $schedule);
883 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
884 $this->assertTrue(is_numeric($actionScheduleDao->id
));
886 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
887 $this->assertTrue(is_numeric($activity->id
));
888 $contact = $this->callAPISuccess('contact', 'create', array_merge(
889 $this->fixtures
['contact'],
891 $this->fixtures
['contact_custom_token']['name'] => $this->fixtures
['contact_custom_token']['value'],
896 $source['contact_id'] = $contact['id'];
897 $source['activity_id'] = $activity->id
;
898 $source['record_type_id'] = 2;
899 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
900 $activityContact->save();
902 CRM_Utils_Time
::setTime('2012-06-14 15:00:00');
903 $this->callAPISuccess('job', 'send_reminder', []);
904 $this->mut
->assertRecipients([['test-member@example.com']]);
905 foreach ($this->mut
->getAllMessages('ezc') as $message) {
906 /** @var ezcMail $message */
909 $messageArray['subject'] = $message->subject
;
910 $messageArray['from_name'] = $message->from
->name
;
911 $messageArray['from_email'] = $message->from
->email
;
912 $messageArray['body_text'] = '--UNDEFINED--';
913 $messageArray['body_html'] = '--UNDEFINED--';
915 foreach ($message->fetchParts() as $part) {
916 /** @var ezcMailText ezcMailText */
917 if ($part instanceof ezcMailText
&& $part->subType
== 'html') {
918 $messageArray['body_html'] = $part->text
;
920 if ($part instanceof ezcMailText
&& $part->subType
== 'plain') {
921 $messageArray['body_text'] = $part->text
;
925 foreach ($patterns as $field => $pattern) {
926 $this->assertRegExp($pattern, $messageArray[$field],
927 "Check that '$field'' matches regex. " . print_r(['expected' => $patterns, 'actual' => $messageArray], 1));
930 $this->mut
->clearMessages();
933 public function testActivityDateTimeMatchNonRepeatableSchedule() {
934 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day']);
935 $this->assertTrue(is_numeric($actionScheduleDao->id
));
937 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
938 $this->assertTrue(is_numeric($activity->id
));
939 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
940 $activity->subject
= "Test subject for Phonecall";
943 $source['contact_id'] = $contact['id'];
944 $source['activity_id'] = $activity->id
;
945 $source['record_type_id'] = 2;
946 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
947 $activityContact->save();
949 $this->assertCronRuns([
951 // Before the 24-hour mark, no email
952 'time' => '2012-06-14 04:00:00',
957 // After the 24-hour mark, an email
958 'time' => '2012-06-14 15:00:00',
959 'recipients' => [['test-member@example.com']],
960 'subjects' => ['1-Day (non-repeating) (about Phone Call)'],
963 // Run cron again; message already sent
968 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
969 $activityDAO = new CRM_Activity_DAO_Activity();
970 $activityDAO->source_record_id
= $activity->id
;
971 $activityDAO->activity_type_id
= array_search('Reminder Sent', $activityTypes);
972 $activityDAO->find();
973 while ($activityDAO->fetch()) {
974 $this->assertContains($activity->subject
, $activityDAO->details
);
978 public function testActivityDateTimeMatchRepeatableSchedule() {
979 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day_r']);
980 $this->assertTrue(is_numeric($actionScheduleDao->id
));
982 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
983 $this->assertTrue(is_numeric($activity->id
));
984 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
987 $source['contact_id'] = $contact['id'];
988 $source['activity_id'] = $activity->id
;
989 $source['record_type_id'] = 2;
990 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
991 $activityContact->save();
993 $this->assertCronRuns([
995 // Before the 24-hour mark, no email
996 'time' => '012-06-14 04:00:00',
1001 // After the 24-hour mark, an email
1002 'time' => '2012-06-14 15:00:00',
1003 'recipients' => [['test-member@example.com']],
1004 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1007 // Run cron 4 hours later; first message already sent
1008 'time' => '2012-06-14 20:00:00',
1013 // Run cron 6 hours later; send second message.
1014 'time' => '2012-06-14 21:00:01',
1015 'recipients' => [['test-member@example.com']],
1016 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1021 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate() {
1022 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day_r_on_abs_date']);
1023 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1025 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
1026 $this->assertTrue(is_numeric($activity->id
));
1027 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1030 $source['contact_id'] = $contact['id'];
1031 $source['activity_id'] = $activity->id
;
1032 $source['record_type_id'] = 2;
1033 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1034 $activityContact->save();
1036 $this->assertCronRuns([
1038 // Before the 24-hour mark, no email
1039 'time' => '2012-06-13 04:00:00',
1044 // On absolute date set on 2012-06-14
1045 'time' => '2012-06-14 00:00:00',
1046 'recipients' => [['test-member@example.com']],
1047 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1050 // Run cron 4 hours later; first message already sent
1051 'time' => '2012-06-14 04:00:00',
1056 // Run cron 6 hours later; send second message.
1057 'time' => '2012-06-14 06:00:01',
1058 'recipients' => [['test-member@example.com']],
1059 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1064 public function testEventNameWithAbsoluteDateAndNothingElse() {
1065 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 1]));
1066 $this->callAPISuccess('Email', 'create', [
1067 'contact_id' => $participant->contact_id
,
1068 'email' => 'test-event@example.com',
1070 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
1072 $actionSchedule = $this->fixtures
['sched_eventname_1day_on_abs_date'];
1073 $actionSchedule['entity_value'] = $participant->event_id
;
1074 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1076 $this->assertCronRuns([
1078 // Before the 24-hour mark, no email
1079 'time' => '2012-06-13 04:00:00',
1084 // On absolute date set on 2012-06-14
1085 'time' => '2012-06-14 00:00:00',
1086 'recipients' => [['test-event@example.com']],
1087 'subjects' => ['sched_eventname_1day_on_abs_date'],
1090 // Run cron 4 hours later; first message already sent
1091 'time' => '2012-06-14 04:00:00',
1099 * For contacts/activities which don't match the schedule filter,
1100 * an email should *not* be sent.
1102 // TODO // function testActivityDateTime_NonMatch() { }
1105 * For contacts/members which match schedule based on join/start date,
1106 * an email should be sent.
1108 public function testMembershipDateMatch() {
1109 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 1]));
1110 $this->assertTrue(is_numeric($membership->id
));
1111 $result = $this->callAPISuccess('Email', 'create', [
1112 'contact_id' => $membership->contact_id
,
1113 'email' => 'test-member@example.com',
1114 'location_type_id' => 1,
1118 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1119 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1120 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1121 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1122 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1124 // start_date=2012-03-15 ; schedule is 2 weeks after join_date
1125 $this->assertCronRuns([
1127 // Before the 2-week mark, no email.
1128 'time' => '2012-03-28 01:00:00',
1133 // After the 2-week mark, send an email.
1134 'time' => '2012-03-29 01:00:00',
1135 'recipients' => [['test-member@example.com']],
1136 'subjects' => ['subject sched_membership_join_2week (joined March 15th, 2012)'],
1140 $actionSchedule = $this->fixtures
['sched_membership_start_1week'];
1141 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1142 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1143 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1145 // start_date=2012-03-15 ; schedule is 1 weeks after start_date
1146 $this->assertCronRuns([
1148 // Before the 2-week mark, no email.
1149 'time' => '2012-03-21 01:00:00',
1154 // After the 2-week mark, send an email.
1155 'time' => '2012-03-22 01:00:00',
1156 'recipients' => [['test-member@example.com']],
1157 'subjects' => ['subject sched_membership_start_1week (joined March 15th, 2012)'],
1163 * CRM-21675: Support parent and smart group in 'Limit to' field
1165 public function testScheduleReminderWithParentGroup() {
1166 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1167 $contactID1 = $this->individualCreate(['birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com']);
1168 // Contact B with birth-date at '07-07-2005', later got added in regular group
1169 $contactID2 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'def@test.com'], 1);
1170 // Contact C with birth-date at '07-07-2005', but not included in any group
1171 $contactID3 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'ghi@test.com'], 2);
1173 // create regular group and add Contact B to it
1174 $groupID = $this->groupCreate();
1175 $this->callAPISuccess('GroupContact', 'Create', [
1176 'group_id' => $groupID,
1177 'contact_id' => $contactID2,
1180 // create smart group which will contain all Male contacts
1181 $smartGroupParams = ['form_values' => ['gender_id' => 1]];
1182 $smartGroupID = $this->smartGroupCreate(
1185 'name' => 'new_smart_group',
1186 'title' => 'New Smart Group',
1187 'parents' => [$groupID => 1],
1191 $actionScheduleParams = [
1192 'name' => 'sched_contact_bday_yesterday',
1193 'title' => 'sched_contact_bday_yesterday',
1194 'absolute_date' => '',
1195 'body_html' => '<p>you look like you were born yesterday!</p>',
1196 'body_text' => 'you look like you were born yesterday!',
1199 'end_frequency_interval' => '',
1200 'end_frequency_unit' => '',
1201 'entity_status' => 1,
1202 'entity_value' => 'birth_date',
1204 'group_id' => $groupID,
1208 'msg_template_id' => '',
1210 'recipient_listing' => '',
1211 'recipient_manual' => '',
1212 'record_activity' => 1,
1213 'repetition_frequency_interval' => '',
1214 'repetition_frequency_unit' => '',
1215 'start_action_condition' => 'after',
1216 'start_action_date' => 'date_field',
1217 'start_action_offset' => '1',
1218 'start_action_unit' => 'day',
1219 'subject' => 'subject sched_contact_bday_yesterday',
1222 // Create schedule reminder where parent group ($groupID) is selectd to limit recipients,
1223 // which contain a individual contact - $contactID2 and is parent to smart group.
1224 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
1225 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1226 $this->assertCronRuns([
1228 // On the birthday, no email.
1229 'time' => '2005-07-07 01:00:00',
1233 // The next day, send an email.
1234 'time' => '2005-07-08 20:00:00',
1245 $this->groupDelete($smartGroupID);
1246 $this->groupDelete($groupID);
1250 * Test end date email sent.
1252 * For contacts/members which match schedule based on join date,
1253 * an email should be sent.
1255 public function testMembershipJoinDateNonMatch() {
1256 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures
['rolling_membership']);
1257 $this->assertTrue(is_numeric($membership->id
));
1258 $result = $this->callAPISuccess('Email', 'create', [
1259 'contact_id' => $membership->contact_id
,
1260 'location_type_id' => 1,
1261 'email' => 'test-member@example.com',
1264 // Add an alternative membership type, and only send messages for that type
1265 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', []);
1266 $this->assertTrue(is_numeric($extraMembershipType->id
));
1267 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_membership_join_2week']);
1268 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1269 $actionScheduleDao->entity_value
= $extraMembershipType->id
;
1270 $actionScheduleDao->save();
1272 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1273 $this->assertCronRuns([
1275 // After the 2-week mark, don't send email because we have different membership type.
1276 'time' => '2012-03-29 01:00:00',
1283 * Test that the first and SECOND notifications are sent out.
1285 public function testMembershipEndDateRepeat() {
1286 // creates membership with end_date = 20120615
1287 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1288 $result = $this->callAPISuccess('Email', 'create', [
1289 'contact_id' => $membership->contact_id
,
1290 'email' => 'test-member@example.com',
1292 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1294 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
1295 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1296 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1298 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1299 $this->assertCronRuns([
1301 // After the 1-month mark, no email
1302 'time' => '2012-07-15 01:00:00',
1306 // After the 2-month mark, send an email.
1307 'time' => '2012-08-15 01:00:00',
1308 'recipients' => [['test-member@example.com']],
1311 // 4 weeks after first email send first repeat
1312 'time' => '2012-09-12 01:00:00',
1313 'recipients' => [['test-member@example.com']],
1316 // 1 week after first repeat send nothing
1317 // There was a bug where the first repeat went out and then
1318 // it would keep going out every cron run. This is to check that's
1320 'time' => '2012-09-19 01:00:00',
1324 // 4 weeks after first repeat send second repeat
1325 'time' => '2012-10-10 01:00:00',
1326 'recipients' => [['test-member@example.com']],
1329 // 4 months after membership end, send nothing
1330 'time' => '2012-10-15 01:00:00',
1334 // 5 months after membership end, send nothing
1335 'time' => '2012-11-15 01:00:00',
1342 * Test behaviour when date changes.
1344 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1348 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
1349 // creates membership with end_date = 20120615
1350 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1351 $this->callAPISuccess('Email', 'create', [
1352 'contact_id' => $membership->contact_id
,
1353 'email' => 'test-member@example.com',
1355 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1357 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
1358 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1359 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1360 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1361 $this->assertCronRuns([
1363 // After the 2-week mark, send an email.
1364 'time' => '2012-08-15 01:00:00',
1365 'recipients' => [['test-member@example.com']],
1369 // Extend membership - reminder should NOT go out.
1370 $this->callAPISuccess('membership', 'create', ['id' => $membership->id
, 'end_date' => '2014-01-01']);
1371 $this->assertCronRuns([
1373 // After the 2-week mark, send an email.
1374 'time' => '2012-09-12 01:00:00',
1381 * Test membership end date email sends.
1383 * For contacts/members which match schedule based on end date,
1384 * an email should be sent.
1386 public function testMembershipEndDateMatch() {
1387 // creates membership with end_date = 20120615
1388 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1389 $this->assertTrue(is_numeric($membership->id
));
1390 $this->callAPISuccess('Email', 'create', [
1391 'contact_id' => $membership->contact_id
,
1392 'email' => 'test-member@example.com',
1394 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1396 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
1397 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1398 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1399 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1401 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1402 $this->assertCronRuns([
1404 // Before the 2-week mark, no email.
1405 'time' => '2012-05-31 01:00:00',
1409 // After the 2-week mark, send an email.
1410 'time' => '2012-06-01 01:00:00',
1411 'recipients' => [['test-member@example.com']],
1414 // After the email is sent, another one is not sent
1415 'time' => '2012-06-01 02:00:00',
1420 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1421 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1422 $membership->end_date
= '2012-09-15';
1423 $membership->save();
1425 //change the email id of chosen membership contact to assert
1426 //recipient of not the previously sent mail but the new one
1427 $result = $this->callAPISuccess('Email', 'create', [
1429 'contact_id' => $membership->contact_id
,
1430 'email' => 'member2@example.com',
1432 $this->assertAPISuccess($result);
1434 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1435 $this->assertCronRuns([
1437 // Before the 2-week mark, no email
1438 'time' => '2012-08-31 01:00:00',
1442 // After the 2-week mark, send an email
1443 'time' => '2012-09-01 01:00:00',
1444 'recipients' => [['member2@example.com']],
1447 // After the email is sent, another one is not sent
1448 'time' => '2012-09-01 02:00:00',
1453 $membership->end_date
= '2012-12-15';
1454 $membership->save();
1455 // end_date=2012-12-15 ; schedule is 2 weeks before end_date
1456 $this->assertCronRuns([
1458 // Before the 2-week mark, no email
1459 'time' => '2012-11-30 01:00:00',
1463 // After the 2-week mark, send an email
1464 'time' => '2012-12-01 01:00:00',
1465 'recipients' => [['member2@example.com']],
1468 // After the email is sent, another one is not sent
1469 'time' => '2012-12-01 02:00:00',
1476 public function createMembershipAndContact($contactFixture, $membershipTypeId) {
1477 $result = $this->callAPISuccess('contact', 'create', $contactFixture);
1478 $contact = $result['values'][$result['id']];
1481 'contact_id' => $contact['id'],
1482 'membership_type_id' => $membershipTypeId,
1483 'owner_membership_id' => 'NULL',
1485 $params = array_merge($this->fixtures
['rolling_membership'], $params);
1486 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $params);
1487 $this->assertTrue(is_numeric($membership->id
));
1492 * This test is very similar to testMembershipEndDateMatch, but it adds
1493 * another contact because there was a bug in
1494 * RecipientBuilder::buildRelFirstPass where it was only sending the
1495 * reminder for the first contact returned in a query for renewed
1496 * memberships. Other contacts wouldn't get the mail.
1498 public function testMultipleMembershipEndDateMatch() {
1499 $membershipTypeId = $this->membershipTypeCreate($this->fixtures
['rolling_membership']['membership_type_id']);
1500 $membershipOne = $this->createMembershipAndContact($this->fixtures
['contact'], $membershipTypeId);
1501 $membershipTwo = $this->createMembershipAndContact($this->fixtures
['contact_2'], $membershipTypeId);
1502 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
1503 $actionSchedule['entity_value'] = $membershipTypeId;
1504 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1505 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1507 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1508 $this->assertCronRuns([
1510 // Before the 2-week mark, no email.
1511 'time' => '2012-05-31 01:00:00',
1515 // After the 2-week mark, send emails.
1516 'time' => '2012-06-01 01:00:00',
1518 ['test-member@example.com'],
1519 ['test-contact-2@example.com'],
1523 // After the email is sent, another one is not sent
1524 'time' => '2012-06-01 02:00:00',
1529 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1530 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1531 $membershipOne->end_date
= '2012-09-15';
1532 $membershipOne->save();
1533 $membershipTwo->end_date
= '2012-09-15';
1534 $membershipTwo->save();
1536 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1537 $this->assertCronRuns([
1539 // Before the 2-week mark, no email
1540 'time' => '2012-08-31 01:00:00',
1544 // After the 2-week mark, send an email
1545 'time' => '2012-09-01 01:00:00',
1547 ['test-member@example.com'],
1548 ['test-contact-2@example.com'],
1552 // After the email is sent, another one is not sent
1553 'time' => '2012-06-01 02:00:00',
1560 * Test membership end date email.
1562 * For contacts/members which match schedule based on end date,
1563 * an email should be sent.
1565 public function testMembershipEndDateNoMatch() {
1566 // creates membership with end_date = 20120615
1567 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 3]));
1568 $this->assertTrue(is_numeric($membership->id
));
1569 $result = $this->callAPISuccess('Email', 'create', [
1570 'contact_id' => $membership->contact_id
,
1571 'email' => 'test-member@example.com',
1573 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1575 $actionSchedule = $this->fixtures
['sched_membership_end_2month'];
1576 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1577 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1578 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1580 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1581 $this->assertCronRuns([
1583 // Before the 2-week mark, no email.
1584 'time' => '2012-05-31 01:00:00',
1588 // After the 2-week mark, no email
1589 'time' => '2013-05-01 01:00:00',
1595 public function testContactBirthDateNoAnniv() {
1596 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1597 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1598 $actionSchedule = $this->fixtures
['sched_contact_bday_yesterday'];
1599 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1600 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1601 $this->assertCronRuns([
1603 // On the birthday, no email.
1604 'time' => '2005-07-07 01:00:00',
1608 // The next day, send an email.
1609 'time' => '2005-07-08 20:00:00',
1610 'recipients' => [['test-bday@example.com']],
1615 public function testContactBirthDateAnniversary() {
1616 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1617 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1618 $actionSchedule = $this->fixtures
['sched_contact_bday_anniv'];
1619 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1620 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1621 $this->assertCronRuns([
1623 // On some random day, no email.
1624 'time' => '2014-03-07 01:00:00',
1628 // On the eve of their 9th birthday, send an email.
1629 'time' => '2014-07-06 20:00:00',
1630 'recipients' => [['test-bday@example.com']],
1635 public function testContactCustomDateNoAnniv() {
1637 'title' => 'Test_Group',
1638 'name' => 'test_group',
1639 'extends' => ['Individual'],
1640 'style' => 'Inline',
1641 'is_multiple' => FALSE,
1644 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1646 'label' => 'Graduation',
1647 'data_type' => 'Date',
1648 'html_type' => 'Select Date',
1649 'custom_group_id' => $createGroup['id'],
1651 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1652 $contactParams = $this->fixtures
['contact'];
1653 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1654 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1655 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1656 $actionSchedule = $this->fixtures
['sched_contact_grad_tomorrow'];
1657 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1658 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1659 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1660 $this->assertCronRuns([
1662 // On some random day, no email.
1663 'time' => '2014-03-07 01:00:00',
1667 // On the eve of their graduation, send an email.
1668 'time' => '2013-12-15 20:00:00',
1669 'recipients' => [['test-member@example.com']],
1672 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1675 public function testContactCreatedNoAnniv() {
1676 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1677 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1678 $actionSchedule = $this->fixtures
['sched_contact_created_yesterday'];
1679 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1680 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1681 $this->assertCronRuns([
1683 // On the date created, no email.
1684 'time' => $contact['values'][$contact['id']]['created_date'],
1688 // The next day, send an email.
1689 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1690 'recipients' => [['test-bday@example.com']],
1695 public function testContactModifiedAnniversary() {
1696 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1697 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1698 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', ['id' => $contact['id'], 'return' => 'modified_date']);
1699 $actionSchedule = $this->fixtures
['sched_contact_mod_anniv'];
1700 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1701 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1702 $this->assertCronRuns([
1704 // On some random day, no email.
1705 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1709 // On the eve of 3 years after they were modified, send an email.
1710 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1711 'recipients' => [['test-bday@example.com']],
1717 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
1719 public function testMembershipLimitToNone() {
1720 // creates membership with end_date = 20120615
1721 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1723 $this->assertTrue(is_numeric($membership->id
));
1724 $result = $this->callAPISuccess('Email', 'create', [
1725 'contact_id' => $membership->contact_id
,
1726 'email' => 'member@example.com',
1728 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1729 $this->callAPISuccess('contact', 'create', ['email' => 'b@c.com', 'contact_type' => 'Individual']);
1731 $this->assertAPISuccess($result);
1733 $actionSchedule = $this->fixtures
['sched_membership_end_limit_to_none'];
1734 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1735 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1736 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1738 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1739 $this->assertCronRuns([
1741 // Before the 2-week mark, no email.
1742 'time' => '2012-05-31 01:00:00',
1743 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1749 public function testMembership_referenceDate() {
1750 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1752 $this->assertTrue(is_numeric($membership->id
));
1753 $result = $this->callAPISuccess('Email', 'create', [
1754 'contact_id' => $membership->contact_id
,
1755 'email' => 'member@example.com',
1758 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1759 $this->assertAPISuccess($result);
1761 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1762 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1763 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1764 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1766 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1767 $this->assertCronRuns([
1769 // After the 2-week mark, send an email
1770 'time' => '2012-03-29 01:00:00',
1771 'recipients' => [['member@example.com']],
1774 // After the 2-week 1day mark, don't send an email
1775 'time' => '2012-03-30 01:00:00',
1780 //check if reference date is set to membership's join date
1781 //as per the action_start_date chosen for current schedule reminder
1782 $this->assertEquals('2012-03-15 00:00:00',
1783 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id
, 'reference_date', 'contact_id')
1786 //change current membership join date that may signifies as membership renewal activity
1787 $membership->join_date
= '2012-03-29';
1788 $membership->save();
1790 $this->assertCronRuns([
1792 // After the 13 days of the changed join date 2012-03-29, don't send an email
1793 'time' => '2012-04-11 01:00:00',
1797 // After the 2-week of the changed join date 2012-03-29, send an email
1798 'time' => '2012-04-12 01:00:00',
1799 'recipients' => [['member@example.com']],
1802 $this->assertCronRuns([
1804 // It should not re-send on the same day
1805 'time' => '2012-04-12 01:00:00',
1811 public function testMembershipOnMultipleReminder() {
1812 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 2]));
1814 $this->assertTrue(is_numeric($membership->id
));
1815 $result = $this->callAPISuccess('Email', 'create', [
1816 'contact_id' => $membership->contact_id
,
1817 'email' => 'member@example.com',
1819 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1820 $this->assertAPISuccess($result);
1822 // Send email 2 weeks before end_date
1823 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week'];
1824 // Send email on end_date/expiry date
1825 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date'];
1826 // Send email 1 day after end_date/grace period
1827 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date'];
1828 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id
;
1829 foreach (['actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter'] as $value) {
1830 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
1831 $this->assertTrue(is_numeric($
$value->id
));
1834 $this->assertCronRuns(
1837 // 1day 2weeks before membership end date(MED), don't send mail
1838 'time' => '2012-05-31 01:00:00',
1842 // 2 weeks before MED, send an email
1843 'time' => '2012-06-01 01:00:00',
1844 'recipients' => [['member@example.com']],
1847 // 1day before MED, don't send mail
1848 'time' => '2012-06-14 01:00:00',
1852 // On MED, send an email
1853 'time' => '2012-06-15 00:00:00',
1854 'recipients' => [['member@example.com']],
1857 // After 1day of MED, send an email
1858 'time' => '2012-06-16 01:00:00',
1859 'recipients' => [['member@example.com']],
1862 // After 1day 1min of MED, don't send an email
1863 'time' => '2012-06-17 00:01:00',
1869 // Assert the timestamp as of when the emails of respective three reminders as configured
1870 // 2 weeks before, on and 1 day after MED, are sent
1871 $this->assertApproxEquals(
1872 strtotime('2012-06-01 01:00:00'),
1873 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1874 // Variation in test execution time.
1877 $this->assertApproxEquals(
1878 strtotime('2012-06-15 00:00:00'),
1879 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1880 // Variation in test execution time.
1883 $this->assertApproxEquals(
1884 strtotime('2012-06-16 01:00:00'),
1885 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1886 // Variation in test execution time.
1890 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1891 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1892 $membership->end_date
= '2012-06-20';
1893 $membership->save();
1895 $result = $this->callAPISuccess('Contact', 'get', ['id' => $membership->contact_id
]);
1896 $this->assertCronRuns(
1899 // 1day 2weeks before membership end date(MED), don't send mail
1900 'time' => '2012-06-05 01:00:00',
1904 // 2 weeks before MED, send an email
1905 'time' => '2012-06-06 01:00:00',
1906 'recipients' => [['member@example.com']],
1909 // 1day before MED, don't send mail
1910 'time' => '2012-06-19 01:00:00',
1914 // On MED, send an email
1915 'time' => '2012-06-20 00:00:00',
1916 'recipients' => [['member@example.com']],
1919 // After 1day of MED, send an email
1920 'time' => '2012-06-21 01:00:00',
1921 'recipients' => [['member@example.com']],
1924 // After 1day 1min of MED, don't send an email
1925 'time' => '2012-07-21 00:01:00',
1931 public function testContactCustomDate_Anniv() {
1933 'title' => 'Test_Group now',
1934 'name' => 'test_group_now',
1935 'extends' => ['Individual'],
1936 'style' => 'Inline',
1937 'is_multiple' => FALSE,
1940 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1942 'label' => 'Graduation',
1943 'data_type' => 'Date',
1944 'html_type' => 'Select Date',
1945 'custom_group_id' => $createGroup['id'],
1947 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1949 $contactParams = $this->fixtures
['contact'];
1950 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1951 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1952 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1953 $actionSchedule = $this->fixtures
['sched_contact_grad_anniv'];
1954 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1955 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1956 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1957 $this->assertCronRuns([
1959 // On some random day, no email.
1960 'time' => '2014-03-07 01:00:00',
1964 // A week after their 5th anniversary of graduation, send an email.
1965 'time' => '2018-12-23 20:00:00',
1966 'recipients' => [['test-member@example.com']],
1969 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1973 * Test sched reminder set via registration date.
1975 public function testEventTypeRegistrationDate() {
1978 'email' => 'test-event@example.com',
1980 $contact = $this->individualCreate($contactParams);
1981 //Add it as a participant to an event ending registration - 7 days from now.
1983 'start_date' => date('Ymd', strtotime('-5 day')),
1984 'end_date' => date('Ymd', strtotime('+7 day')),
1985 'registration_start_date' => date('Ymd', strtotime('-5 day')),
1986 'registration_end_date' => date('Ymd', strtotime('+7 day')),
1988 $event = $this->eventCreate($params);
1989 $this->participantCreate(['contact_id' => $contact, 'event_id' => $event['id']]);
1991 //Create a scheduled reminder to send email 7 days before registration date.
1992 $actionSchedule = $this->fixtures
['sched_eventtype_start_1week_before'];
1993 $actionSchedule['start_action_offset'] = 7;
1994 $actionSchedule['start_action_unit'] = 'day';
1995 $actionSchedule['start_action_date'] = 'registration_end_date';
1996 $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id'];
1997 $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', [
1999 'name' => "Attended",
2001 $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2002 //Run the cron and verify if an email was sent.
2003 $this->assertCronRuns([
2005 'time' => date('Y-m-d'),
2006 'recipients' => [['test-event@example.com']],
2012 'email' => 'test-event2@example.com',
2014 $contact2 = $this->individualCreate($contactParams);
2015 //Create an event with registration end date = 2 week from now.
2016 $params['end_date'] = date('Ymd', strtotime('+2 week'));
2017 $params['registration_end_date'] = date('Ymd', strtotime('+2 week'));
2018 $event2 = $this->eventCreate($params);
2019 $this->participantCreate(['contact_id' => $contact2, 'event_id' => $event2['id']]);
2021 //Assert there is no reminder sent to the contact.
2022 $this->assertCronRuns([
2024 'time' => date('Y-m-d'),
2029 //Modify the sched reminder to be sent 2 week from registration end date.
2030 $this->callAPISuccess('action_schedule', 'create', [
2031 'id' => $actionSched['id'],
2032 'start_action_offset' => 2,
2033 'start_action_unit' => 'week',
2036 //Contact should receive the reminder now.
2037 $this->assertCronRuns([
2039 'time' => date('Y-m-d'),
2040 'recipients' => [['test-event2@example.com']],
2046 * Test sched reminder set via start date.
2048 public function testEventTypeStartDate() {
2049 // Create event+participant with start_date = 20120315, end_date = 20120615.
2050 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2051 $this->callAPISuccess('Email', 'create', [
2052 'contact_id' => $participant->contact_id
,
2053 'email' => 'test-event@example.com',
2055 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2057 $actionSchedule = $this->fixtures
['sched_eventtype_start_1week_before'];
2058 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2059 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2061 //echo "CREATED\n"; ob_flush(); sleep(20);
2063 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2064 $this->assertCronRuns([
2067 'time' => '2012-03-02 01:00:00',
2072 'time' => '2012-03-08 01:00:00',
2073 'recipients' => [['test-event@example.com']],
2076 // And then nothing else
2077 'time' => '2012-03-16 01:00:00',
2083 public function testEventTypeEndDateRepeat() {
2084 // Create event+participant with start_date = 20120315, end_date = 20120615.
2085 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2086 $this->callAPISuccess('Email', 'create', [
2087 'contact_id' => $participant->contact_id
,
2088 'email' => 'test-event@example.com',
2090 $c = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2092 $actionSchedule = $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'];
2093 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2094 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2096 $this->assertCronRuns([
2099 'time' => '2012-08-13 01:00:00',
2103 // After the 2-month mark, send an email.
2104 'time' => '2012-08-16 01:00:00',
2105 'recipients' => [['test-event@example.com']],
2108 // After 2 months and 1 week, don't repeat yet.
2109 'time' => '2012-08-23 02:00:00',
2113 // After 2 months and 2 weeks
2114 'time' => '2012-08-30 02:00:00',
2115 'recipients' => [['test-event@example.com']],
2118 // After 2 months and 4 week
2119 'time' => '2012-09-13 02:00:00',
2120 'recipients' => [['test-event@example.com']],
2123 // After 2 months and 6 weeks
2124 'time' => '2012-09-27 01:00:00',
2130 // TODO // function testMembershipEndDate_NonMatch() { }
2131 // TODO // function testEventTypeStartDate_Match() { }
2132 // TODO // function testEventTypeEndDate_Match() { }
2133 // TODO // function testEventNameStartDate_Match() { }
2134 // TODO // function testEventNameEndDate_Match() { }
2137 * Run a series of cron jobs and make an assertion about email deliveries.
2139 * @param array $cronRuns
2140 * array specifying when to run cron and what messages to expect; each item is an array with keys:
2141 * - time: string, e.g. '2012-06-15 21:00:01'
2142 * - recipients: array(array(string)), list of email addresses which should receive messages
2144 public function assertCronRuns($cronRuns) {
2145 foreach ($cronRuns as $cronRun) {
2146 CRM_Utils_Time
::setTime($cronRun['time']);
2147 $this->callAPISuccess('job', 'send_reminder', []);
2148 $this->mut
->assertRecipients($cronRun['recipients']);
2149 if (array_key_exists('subjects', $cronRun)) {
2150 $this->mut
->assertSubjects($cronRun['subjects']);
2152 $this->mut
->clearMessages();
2158 * (DAO_Name => array(int)) List of items to garbage-collect during tearDown
2160 private $_testObjects;
2163 * Sets up the fixture, for example, opens a network connection.
2165 * This method is called before a test is executed.
2167 protected function _setUp() {
2168 $this->_testObjects
= [];
2172 * Tears down the fixture, for example, closes a network connection.
2174 * This method is called after a test is executed.
2176 protected function _tearDown() {
2178 $this->deleteTestObjects();
2182 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
2183 * created entities and provides for brainless cleanup.
2185 * @see CRM_Core_DAO::createTestObject
2188 * @param array $params
2189 * @param int $numObjects
2190 * @param bool $createOnly
2192 * @return array|NULL|object
2194 public function createTestObject($daoName, $params = [], $numObjects = 1, $createOnly = FALSE) {
2195 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
2196 if (is_array($objects)) {
2197 $this->registerTestObjects($objects);
2200 $this->registerTestObjects([$objects]);
2206 * @param array $objects
2207 * DAO or BAO objects.
2209 public function registerTestObjects($objects) {
2210 //if (is_object($objects)) {
2211 // $objects = array($objects);
2213 foreach ($objects as $object) {
2214 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
2215 $this->_testObjects
[$daoName][] = $object->id
;
2219 public function deleteTestObjects() {
2220 // Note: You might argue that the FK relations between test
2221 // objects could make this problematic; however, it should
2222 // behave intuitively as long as we mentally split our
2223 // test-objects between the "manual/primary records"
2224 // and the "automatic/secondary records"
2225 foreach ($this->_testObjects
as $daoName => $daoIds) {
2226 foreach ($daoIds as $daoId) {
2227 CRM_Core_DAO
::deleteTestObjects($daoName, ['id' => $daoId]);
2230 $this->_testObjects
= [];
2234 * Test that the various repetition units work correctly.
2237 public function testRepetitionFrequencyUnit() {
2238 $membershipTypeParams = [
2239 'duration_interval' => '1',
2240 'duration_unit' => 'year',
2242 'period_type' => 'rolling',
2244 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
2245 $interval_units = ['hour', 'day', 'week', 'month', 'year'];
2246 foreach ($interval_units as $interval_unit) {
2247 $membershipEndDate = DateTime
::createFromFormat('Y-m-d H:i:s', "2013-03-15 00:00:00");
2249 'contact_type' => 'Individual',
2250 'first_name' => 'Test',
2251 'last_name' => "Interval $interval_unit",
2254 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2255 $this->assertTrue(is_numeric($contact->id
));
2257 'contact_id' => $contact->id
,
2259 'email' => "test-member-{$interval_unit}@example.com",
2260 'location_type_id' => 1,
2262 $email = $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2263 $this->assertTrue(is_numeric($email->id
));
2264 $membershipParams = [
2265 'membership_type_id' => $membershipType->id
,
2266 'contact_id' => $contact->id
,
2267 'join_date' => '20120315',
2268 'start_date' => '20120315',
2269 'end_date' => '20130315',
2273 $membershipParams['status-id'] = 1;
2274 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2275 $actionScheduleParams = $this->fixtures
['sched_on_membership_end_date_repeat_interval'];
2276 $actionScheduleParams['entity_value'] = $membershipType->id
;
2277 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
2278 $actionScheduleParams['repetition_frequency_interval'] = 2;
2279 $actionSchedule = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
2280 $this->assertTrue(is_numeric($actionSchedule->id
));
2281 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
2282 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
2283 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
2286 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
2290 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
2291 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2294 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
2298 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
2299 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2302 $this->assertCronRuns($cronRuns);
2303 $actionSchedule->delete();
2304 $membership->delete();
2309 * Inherited members without permission to edit the main member contact should
2310 * not get reminders.
2312 * However, just because a contact inherits one membership doesn't mean
2313 * reminders for other memberships should be suppressed.
2317 public function testInheritedMembershipPermissions() {
2318 // Set up common parameters for memberships.
2319 $membershipParams = $this->fixtures
['rolling_membership'];
2320 $membershipParams['status_id'] = 1;
2322 $membershipParams['membership_type_id']['relationship_type_id'] = 1;
2323 $membershipParams['membership_type_id']['relationship_direction'] = 'b_a';
2324 $membershipType1 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2326 // We'll create a new membership type that can be held at the same time as
2328 $membershipParams['membership_type_id']['relationship_type_id'] = 'NULL';
2329 $membershipParams['membership_type_id']['relationship_direction'] = 'NULL';
2330 $membershipType2 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2332 // Create the parent membership and contact
2333 $membershipParams['membership_type_id'] = $membershipType1->id
;
2334 $mainMembership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2337 'contact_type' => 'Individual',
2338 'first_name' => 'Mom',
2339 'last_name' => 'Rel',
2342 $this->createTestObject('CRM_Contact_DAO_Contact', array_merge($contactParams, ['id' => $mainMembership->contact_id
]));
2345 'contact_id' => $mainMembership->contact_id
,
2346 'email' => 'test-member@example.com',
2347 'location_type_id' => 1,
2350 $email = $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2352 // Set up contacts and emails for the two children
2353 $contactParams['first_name'] = 'Favorite';
2354 $permChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2355 $emailParams['email'] = 'favorite@example.com';
2356 $emailParams['contact_id'] = $permChild->id
;
2357 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2359 $contactParams['first_name'] = 'Black Sheep';
2360 $nonPermChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2361 $emailParams['email'] = 'black.sheep@example.com';
2362 $emailParams['contact_id'] = $nonPermChild->id
;
2363 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2365 // Each child gets a relationship, one with permission to edit the parent. This
2366 // will trigger inherited memberships for the first membership type
2368 'relationship_type_id' => 1,
2369 'contact_id_a' => $nonPermChild->id
,
2370 'contact_id_b' => $mainMembership->contact_id
,
2373 $this->callAPISuccess('relationship', 'create', $relParams);
2375 $relParams['contact_id_a'] = $permChild->id
;
2376 $relParams['is_permission_a_b'] = CRM_Contact_BAO_Relationship
::EDIT
;
2377 $this->callAPISuccess('relationship', 'create', $relParams);
2379 // Mom and Black Sheep get their own memberships of the second type.
2380 $membershipParams['membership_type_id'] = $membershipType2->id
;
2381 $membershipParams['owner_membership_id'] = 'NULL';
2382 $membershipParams['contact_id'] = $mainMembership->contact_id
;
2383 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2385 $membershipParams['contact_id'] = $nonPermChild->id
;
2386 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2388 // Test a reminder for the first membership type - that should exclude Black
2390 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
2391 $actionSchedule['entity_value'] = $membershipType1->id
;
2392 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2393 $this->assertTrue(is_numeric($actionScheduleDao->id
));
2395 $this->assertCronRuns([
2397 'time' => '2012-03-29 01:00:00',
2398 'recipients' => [['test-member@example.com'], ['favorite@example.com']],
2400 'subject sched_membership_join_2week (joined March 15th, 2012)',
2401 'subject sched_membership_join_2week (joined March 15th, 2012)',
2406 // Test a reminder for the second membership type - that should include
2408 $actionSchedule = $this->fixtures
['sched_membership_start_1week'];
2409 $actionSchedule['entity_value'] = $membershipType2->id
;
2410 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2411 $this->assertTrue(is_numeric($actionScheduleDao->id
));
2413 $this->assertCronRuns([
2415 'time' => '2012-03-22 01:00:00',
2416 'recipients' => [['test-member@example.com'], ['black.sheep@example.com']],
2418 'subject sched_membership_start_1week (joined March 15th, 2012)',
2419 'subject sched_membership_start_1week (joined March 15th, 2012)',
2425 public function createModifiedDateTime($origDateTime, $modifyRule) {
2426 $newDateTime = clone($origDateTime);
2427 $newDateTime->modify($modifyRule);
2428 return $newDateTime;
2431 public function testMembershipScheduleWithAbsoluteDate() {
2432 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 1]));
2433 $this->assertTrue(is_numeric($membership->id
));
2434 $result = $this->callAPISuccess('Email', 'create', [
2435 'contact_id' => $membership->contact_id
,
2436 'email' => 'test-member@example.com',
2437 'location_type_id' => 1,
2441 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
2442 $actionSchedule = $this->fixtures
['sched_membership_absolute_date'];
2443 $actionSchedule['entity_value'] = $membership->membership_type_id
;
2444 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2445 $this->assertTrue(is_numeric($actionScheduleDao->id
));
2447 $this->assertCronRuns([
2449 // Before the 24-hour mark, no email
2450 'time' => '2012-06-13 04:00:00',
2455 // On absolute date set on 2012-06-14
2456 'time' => '2012-06-14 00:00:00',
2457 'recipients' => [['test-member@example.com']],
2458 'subjects' => ['subject sched_membership_absolute_date'],
2461 // Run cron 4 hours later; first message already sent
2462 'time' => '2012-06-14 04:00:00',