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
{
29 * Entities set up for the test.
33 private $fixtures = [];
38 * @throws CRM_Core_Exception
40 public function setUp() {
43 $this->mut
= new CiviMailUtils($this, TRUE);
45 $this->fixtures
['rolling_membership_type'] = [
46 'period_type' => 'rolling',
47 'duration_unit' => 'month',
48 'duration_interval' => '3',
51 'financial_type_id' => 2,
54 $this->fixtures
['rolling_membership'] = [
55 'membership_type_id' => [
56 'period_type' => 'rolling',
57 'duration_unit' => 'month',
58 'duration_interval' => '3',
61 'join_date' => '20120315',
62 'start_date' => '20120315',
63 'end_date' => '20120615',
67 $this->fixtures
['rolling_membership_past'] = [
68 'membership_type_id' => [
69 'period_type' => 'rolling',
70 'duration_unit' => 'month',
71 'duration_interval' => '3',
74 'join_date' => '20100310',
75 'start_date' => '20100310',
76 'end_date' => '20100610',
77 'is_override' => 'NULL',
79 $this->fixtures
['participant'] = [
83 'title' => 'Example Event',
84 'start_date' => '20120315',
85 'end_date' => '20120615',
93 $this->fixtures
['phonecall'] = [
95 'activity_type_id' => 2,
96 'activity_date_time' => '20120615100000',
97 'is_current_revision' => 1,
100 $this->fixtures
['contact'] = [
102 'contact_type' => 'Individual',
103 'email' => 'test-member@example.com',
104 'gender_id' => 'Female',
105 'first_name' => 'Churmondleia',
106 'last_name' => 'Ōtākou',
108 $this->fixtures
['contact_2'] = [
110 'contact_type' => 'Individual',
111 'email' => 'test-contact-2@example.com',
112 'gender_id' => 'Male',
113 'first_name' => 'Fabble',
116 $this->fixtures
['contact_birthdate'] = [
118 'contact_type' => 'Individual',
119 'email' => 'test-bday@example.com',
120 'birth_date' => '20050707',
122 $this->fixtures
['sched_activity_1day'] = [
123 'name' => 'One_Day_Phone_Call_Notice',
124 'title' => 'One Day Phone Call Notice',
126 'absolute_date' => NULL,
127 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
128 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
129 'end_action' => NULL,
131 'end_frequency_interval' => NULL,
132 'end_frequency_unit' => NULL,
133 'entity_status' => '1',
134 'entity_value' => '2',
139 'msg_template_id' => NULL,
141 'recipient_listing' => NULL,
142 'recipient_manual' => NULL,
143 'record_activity' => 1,
144 'repetition_frequency_interval' => NULL,
145 'repetition_frequency_unit' => NULL,
146 'start_action_condition' => 'before',
147 'start_action_date' => 'activity_date_time',
148 'start_action_offset' => '1',
149 'start_action_unit' => 'day',
150 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
152 $this->fixtures
['sched_activity_1day_r'] = [
153 'name' => 'One_Day_Phone_Call_Notice_R',
154 'title' => 'One Day Phone Call Notice R',
156 'absolute_date' => NULL,
157 'body_html' => '<p>1-Day (repeating)</p>',
158 'body_text' => '1-Day (repeating)',
159 'end_action' => 'after',
160 'end_date' => 'activity_date_time',
161 'end_frequency_interval' => '2',
162 'end_frequency_unit' => 'day',
163 'entity_status' => '1',
164 'entity_value' => '2',
169 'msg_template_id' => NULL,
171 'recipient_listing' => NULL,
172 'recipient_manual' => NULL,
173 'record_activity' => NULL,
174 'repetition_frequency_interval' => '6',
175 'repetition_frequency_unit' => 'hour',
176 'start_action_condition' => 'before',
177 'start_action_date' => 'activity_date_time',
178 'start_action_offset' => '1',
179 'start_action_unit' => 'day',
180 'subject' => '1-Day (repeating) (about {activity.activity_type})',
182 $this->fixtures
['sched_activity_1day_r_on_abs_date'] = [
183 'name' => 'One_Day_Phone_Call_Notice_R',
184 'title' => 'One Day Phone Call Notice R',
186 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
187 'body_html' => '<p>1-Day (repeating)</p>',
188 'body_text' => '1-Day (repeating)',
189 'entity_status' => '1',
190 'entity_value' => '2',
195 'msg_template_id' => NULL,
197 'recipient_listing' => NULL,
198 'recipient_manual' => NULL,
199 'record_activity' => NULL,
200 'repetition_frequency_interval' => '6',
201 'repetition_frequency_unit' => 'hour',
202 'end_action' => 'after',
203 'end_date' => 'activity_date_time',
204 'end_frequency_interval' => '2',
205 'end_frequency_unit' => 'day',
206 'start_action_condition' => '',
207 'start_action_date' => '',
208 'start_action_offset' => '',
209 'start_action_unit' => '',
210 'subject' => '1-Day (repeating) (about {activity.activity_type})',
212 $this->fixtures
['sched_eventname_1day_on_abs_date'] = [
213 'name' => 'sched_eventname_1day_on_abs_date',
214 'title' => 'sched_eventname_1day_on_abs_date',
216 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
217 'body_html' => '<p>sched_eventname_1day_on_abs_date</p>',
218 'body_text' => 'sched_eventname_1day_on_abs_date',
219 'entity_status' => '1',
220 'entity_value' => '2',
225 'msg_template_id' => NULL,
227 'recipient_listing' => NULL,
228 'recipient_manual' => NULL,
229 'record_activity' => NULL,
230 'repetition_frequency_interval' => NULL,
231 'repetition_frequency_unit' => NULL,
232 'end_action' => NULL,
234 'end_frequency_interval' => NULL,
235 'end_frequency_unit' => NULL,
236 'start_action_condition' => NULL,
237 'start_action_date' => NULL,
238 'start_action_offset' => NULL,
239 'start_action_unit' => NULL,
240 'subject' => 'sched_eventname_1day_on_abs_date',
242 $this->fixtures
['sched_membership_join_2week'] = [
243 'name' => 'sched_membership_join_2week',
244 'title' => 'sched_membership_join_2week',
245 'absolute_date' => '',
246 'body_html' => '<p>body sched_membership_join_2week</p>',
247 'body_text' => 'body sched_membership_join_2week',
250 'end_frequency_interval' => '',
251 'end_frequency_unit' => '',
252 'entity_status' => '',
253 'entity_value' => '',
258 'msg_template_id' => '',
260 'recipient_listing' => '',
261 'recipient_manual' => '',
262 'record_activity' => 1,
263 'repetition_frequency_interval' => '',
264 'repetition_frequency_unit' => '',
265 'start_action_condition' => 'after',
266 'start_action_date' => 'membership_join_date',
267 'start_action_offset' => '2',
268 'start_action_unit' => 'week',
269 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
271 $this->fixtures
['sched_membership_start_1week'] = [
272 'name' => 'sched_membership_start_1week',
273 'title' => 'sched_membership_start_1week',
274 'absolute_date' => '',
275 'body_html' => '<p>body sched_membership_start_1week</p>',
276 'body_text' => 'body sched_membership_start_1week',
279 'end_frequency_interval' => '',
280 'end_frequency_unit' => '',
281 'entity_status' => '',
282 'entity_value' => '',
287 'msg_template_id' => '',
289 'recipient_listing' => '',
290 'recipient_manual' => '',
291 'record_activity' => 1,
292 'repetition_frequency_interval' => '',
293 'repetition_frequency_unit' => '',
294 'start_action_condition' => 'after',
295 'start_action_date' => 'membership_start_date',
296 'start_action_offset' => '1',
297 'start_action_unit' => 'week',
298 'subject' => 'subject sched_membership_start_1week (joined {membership.start_date})',
300 $this->fixtures
['sched_membership_end_2week'] = [
301 'name' => 'sched_membership_end_2week',
302 'title' => 'sched_membership_end_2week',
303 'absolute_date' => '',
304 'body_html' => '<p>body sched_membership_end_2week</p>',
305 'body_text' => 'body sched_membership_end_2week',
308 'end_frequency_interval' => '',
309 'end_frequency_unit' => '',
310 'entity_status' => '',
311 'entity_value' => '',
316 'msg_template_id' => '',
318 'recipient_listing' => '',
319 'recipient_manual' => '',
320 'record_activity' => 1,
321 'repetition_frequency_interval' => '',
322 'repetition_frequency_unit' => '',
323 'start_action_condition' => 'before',
324 'start_action_date' => 'membership_end_date',
325 'start_action_offset' => '2',
326 'start_action_unit' => 'week',
327 'subject' => 'subject sched_membership_end_2week',
329 $this->fixtures
['sched_on_membership_end_date'] = [
330 'name' => 'sched_on_membership_end_date',
331 'title' => 'sched_on_membership_end_date',
332 'body_html' => '<p>Your membership expired today</p>',
333 'body_text' => 'Your membership expired today',
336 'record_activity' => 1,
337 'start_action_condition' => 'after',
338 'start_action_date' => 'membership_end_date',
339 'start_action_offset' => '0',
340 'start_action_unit' => 'hour',
341 'subject' => 'subject send reminder on membership_end_date',
343 $this->fixtures
['sched_after_1day_membership_end_date'] = [
344 'name' => 'sched_after_1day_membership_end_date',
345 'title' => 'sched_after_1day_membership_end_date',
346 'body_html' => '<p>Your membership expired yesterday</p>',
347 'body_text' => 'Your membership expired yesterday',
350 'record_activity' => 1,
351 'start_action_condition' => 'after',
352 'start_action_date' => 'membership_end_date',
353 'start_action_offset' => '1',
354 'start_action_unit' => 'day',
355 'subject' => 'subject send reminder on membership_end_date',
358 $this->fixtures
['sched_membership_end_2month'] = [
359 'name' => 'sched_membership_end_2month',
360 'title' => 'sched_membership_end_2month',
361 'absolute_date' => '',
362 'body_html' => '<p>body sched_membership_end_2month</p>',
363 'body_text' => 'body sched_membership_end_2month',
366 'end_frequency_interval' => '',
367 'end_frequency_unit' => '',
368 'entity_status' => '',
369 'entity_value' => '',
374 'msg_template_id' => '',
376 'recipient_listing' => '',
377 'recipient_manual' => '',
378 'record_activity' => 1,
379 'repetition_frequency_interval' => '',
380 'repetition_frequency_unit' => '',
381 'start_action_condition' => 'after',
382 'start_action_date' => 'membership_end_date',
383 'start_action_offset' => '2',
384 'start_action_unit' => 'month',
385 'subject' => 'subject sched_membership_end_2month',
388 $this->fixtures
['sched_membership_absolute_date'] = [
389 'name' => 'sched_membership_absolute_date',
390 'title' => 'sched_membership_absolute_date',
391 'absolute_date' => CRM_Utils_Date
::processDate('20120614100000'),
392 'body_html' => '<p>body sched_membership_absolute_date</p>',
393 'body_text' => 'body sched_membership_absolute_date',
396 'end_frequency_interval' => '',
397 'end_frequency_unit' => '',
398 'entity_status' => '',
399 'entity_value' => '',
404 'msg_template_id' => '',
406 'recipient_listing' => '',
407 'recipient_manual' => '',
408 'record_activity' => 1,
409 'repetition_frequency_interval' => '',
410 'repetition_frequency_unit' => '',
411 'start_action_condition' => '',
412 'start_action_date' => '',
413 'start_action_offset' => '',
414 'start_action_unit' => '',
415 'subject' => 'subject sched_membership_absolute_date',
418 $this->fixtures
['sched_contact_bday_yesterday'] = [
419 'name' => 'sched_contact_bday_yesterday',
420 'title' => 'sched_contact_bday_yesterday',
421 'absolute_date' => '',
422 'body_html' => '<p>you look like you were born yesterday!</p>',
423 'body_text' => 'you look like you were born yesterday!',
426 'end_frequency_interval' => '',
427 'end_frequency_unit' => '',
428 'entity_status' => 1,
429 'entity_value' => 'birth_date',
434 'msg_template_id' => '',
436 'recipient_listing' => '',
437 'recipient_manual' => '',
438 'record_activity' => 1,
439 'repetition_frequency_interval' => '',
440 'repetition_frequency_unit' => '',
441 'start_action_condition' => 'after',
442 'start_action_date' => 'date_field',
443 'start_action_offset' => '1',
444 'start_action_unit' => 'day',
445 'subject' => 'subject sched_contact_bday_yesterday',
448 $this->fixtures
['sched_contact_bday_anniv'] = [
449 'name' => 'sched_contact_bday_anniv',
450 'title' => 'sched_contact_bday_anniv',
451 'absolute_date' => '',
452 'body_html' => '<p>happy birthday!</p>',
453 'body_text' => 'happy birthday!',
456 'end_frequency_interval' => '',
457 'end_frequency_unit' => '',
458 'entity_status' => 2,
459 'entity_value' => 'birth_date',
464 'msg_template_id' => '',
466 'recipient_listing' => '',
467 'recipient_manual' => '',
468 'record_activity' => 1,
469 'repetition_frequency_interval' => '',
470 'repetition_frequency_unit' => '',
471 'start_action_condition' => 'before',
472 'start_action_date' => 'date_field',
473 'start_action_offset' => '1',
474 'start_action_unit' => 'day',
475 'subject' => 'subject sched_contact_bday_anniv',
478 $this->fixtures
['sched_contact_grad_tomorrow'] = [
479 'name' => 'sched_contact_grad_tomorrow',
480 'title' => 'sched_contact_grad_tomorrow',
481 'absolute_date' => '',
482 'body_html' => '<p>congratulations on your graduation!</p>',
483 'body_text' => 'congratulations on your graduation!',
486 'end_frequency_interval' => '',
487 'end_frequency_unit' => '',
488 'entity_status' => 1,
493 'msg_template_id' => '',
495 'recipient_listing' => '',
496 'recipient_manual' => '',
497 'record_activity' => 1,
498 'repetition_frequency_interval' => '',
499 'repetition_frequency_unit' => '',
500 'start_action_condition' => 'before',
501 'start_action_date' => 'date_field',
502 'start_action_offset' => '1',
503 'start_action_unit' => 'day',
504 'subject' => 'subject sched_contact_grad_tomorrow',
507 $this->fixtures
['sched_contact_grad_anniv'] = [
508 'name' => 'sched_contact_grad_anniv',
509 'title' => 'sched_contact_grad_anniv',
510 'absolute_date' => '',
511 'body_html' => '<p>dear alum, please send us money.</p>',
512 'body_text' => 'dear alum, please send us money.',
515 'end_frequency_interval' => '',
516 'end_frequency_unit' => '',
517 'entity_status' => 2,
522 'msg_template_id' => '',
524 'recipient_listing' => '',
525 'recipient_manual' => '',
526 'record_activity' => 1,
527 'repetition_frequency_interval' => '',
528 'repetition_frequency_unit' => '',
529 'start_action_condition' => 'after',
530 'start_action_date' => 'date_field',
531 'start_action_offset' => '1',
532 'start_action_unit' => 'week',
533 'subject' => 'subject sched_contact_grad_anniv',
536 $this->fixtures
['sched_contact_created_yesterday'] = [
537 'name' => 'sched_contact_created_yesterday',
538 'title' => 'sched_contact_created_yesterday',
539 'absolute_date' => '',
540 'body_html' => '<p>Your contact was created yesterday</p>',
541 'body_text' => 'Your contact was created yesterday!',
544 'end_frequency_interval' => '',
545 'end_frequency_unit' => '',
546 'entity_status' => 1,
547 'entity_value' => 'created_date',
552 'msg_template_id' => '',
554 'recipient_listing' => '',
555 'recipient_manual' => '',
556 'record_activity' => 1,
557 'repetition_frequency_interval' => '',
558 'repetition_frequency_unit' => '',
559 'start_action_condition' => 'after',
560 'start_action_date' => 'date_field',
561 'start_action_offset' => '1',
562 'start_action_unit' => 'day',
563 'subject' => 'subject sched_contact_created_yesterday',
566 $this->fixtures
['sched_contact_mod_anniv'] = [
567 'name' => 'sched_contact_mod_anniv',
568 'title' => 'sched_contact_mod_anniv',
569 'absolute_date' => '',
570 'body_html' => '<p>You last updated your data last year</p>',
571 'body_text' => 'Go update your stuff!',
574 'end_frequency_interval' => '',
575 'end_frequency_unit' => '',
576 'entity_status' => 2,
577 'entity_value' => 'modified_date',
582 'msg_template_id' => '',
584 'recipient_listing' => '',
585 'recipient_manual' => '',
586 'record_activity' => 1,
587 'repetition_frequency_interval' => '',
588 'repetition_frequency_unit' => '',
589 'start_action_condition' => 'before',
590 'start_action_date' => 'date_field',
591 'start_action_offset' => '1',
592 'start_action_unit' => 'day',
593 'subject' => 'subject sched_contact_mod_anniv',
596 $this->fixtures
['sched_eventtype_start_1week_before'] = [
597 'name' => 'sched_eventtype_start_1week_before',
598 'title' => 'sched_eventtype_start_1week_before',
599 'absolute_date' => '',
600 'body_html' => '<p>body sched_eventtype_start_1week_before ({event.title})</p>',
601 'body_text' => 'body sched_eventtype_start_1week_before ({event.title})',
604 'end_frequency_interval' => '',
605 'end_frequency_unit' => '',
606 // participant status id
607 'entity_status' => '',
609 'entity_value' => '',
615 'msg_template_id' => '',
617 'recipient_listing' => '',
618 'recipient_manual' => '',
619 'record_activity' => 1,
620 'repetition_frequency_interval' => '',
621 'repetition_frequency_unit' => '',
622 'start_action_condition' => 'before',
623 'start_action_date' => 'event_start_date',
624 'start_action_offset' => '1',
625 'start_action_unit' => 'week',
626 'subject' => 'subject sched_eventtype_start_1week_before ({event.title})',
628 $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'] = [
629 'name' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
630 'title' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
631 'absolute_date' => '',
632 'body_html' => '<p>body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}</p>',
633 'body_text' => 'body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
634 'end_action' => 'after',
635 'end_date' => 'event_end_date',
636 'end_frequency_interval' => '3',
637 'end_frequency_unit' => 'month',
638 // participant status id
639 'entity_status' => '',
641 'entity_value' => '',
647 'msg_template_id' => '',
649 'recipient_listing' => '',
650 'recipient_manual' => '',
651 'record_activity' => 1,
652 'repetition_frequency_interval' => '2',
653 'repetition_frequency_unit' => 'week',
654 'start_action_condition' => 'after',
655 'start_action_date' => 'event_end_date',
656 'start_action_offset' => '2',
657 'start_action_unit' => 'month',
658 'subject' => 'subject sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
661 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = [
662 'name' => 'sched_membership_end_2month',
663 'title' => 'sched_membership_end_2month',
664 'absolute_date' => '',
665 'body_html' => '<p>body sched_membership_end_2month</p>',
666 'body_text' => 'body sched_membership_end_2month',
668 'end_date' => 'membership_end_date',
669 'end_frequency_interval' => '4',
670 'end_frequency_unit' => 'month',
671 'entity_status' => '',
672 'entity_value' => '',
677 'msg_template_id' => '',
679 'recipient_listing' => '',
680 'recipient_manual' => '',
681 'record_activity' => 1,
682 'repetition_frequency_interval' => '4',
683 'repetition_frequency_unit' => 'week',
684 'start_action_condition' => 'after',
685 'start_action_date' => 'membership_end_date',
686 'start_action_offset' => '2',
687 'start_action_unit' => 'month',
688 'subject' => 'subject sched_membership_end_2month',
690 $this->fixtures
['sched_membership_end_limit_to_none'] = [
691 'name' => 'limit to none',
692 'title' => 'limit to none',
693 'absolute_date' => '',
694 'body_html' => '<p>body sched_membership_end_2month</p>',
695 'body_text' => 'body sched_membership_end_2month',
698 'end_frequency_interval' => '4',
699 'end_frequency_unit' => 'month',
700 'entity_status' => '',
701 'entity_value' => '',
707 'msg_template_id' => '',
709 'recipient_listing' => '',
710 'recipient_manual' => '',
711 'record_activity' => 1,
712 'repetition_frequency_interval' => '4',
713 'repetition_frequency_unit' => 'week',
714 'start_action_condition' => 'after',
715 'start_action_date' => 'membership_end_date',
716 'start_action_offset' => '2',
717 'start_action_unit' => 'month',
718 'subject' => 'limit to none',
720 $this->fixtures
['sched_on_membership_end_date_repeat_interval'] = [
721 'name' => 'sched_on_membership_end_date',
722 'title' => 'sched_on_membership_end_date',
723 'body_html' => '<p>Your membership expired 1 unit ago</p>',
724 'body_text' => 'Your membership expired 1 unit ago',
725 'end_frequency_interval' => 10,
726 'end_frequency_unit' => 'year',
730 'record_activity' => 1,
731 'start_action_condition' => 'after',
732 'start_action_date' => 'membership_end_date',
733 'start_action_offset' => '0',
734 'start_action_unit' => 'hour',
735 'subject' => 'subject send reminder every unit after membership_end_date',
738 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
739 'title' => ts('Test Contact Custom group'),
740 'name' => 'test_contact_cg',
741 'extends' => 'Contact',
742 'domain_id' => CRM_Core_Config
::domainID(),
744 'collapse_adv_display' => 0,
745 'collapse_display' => 0,
747 $customField = $this->callAPISuccess('CustomField', 'create', [
748 'label' => 'Test Text',
749 'data_type' => 'String',
750 'html_type' => 'Text',
751 'custom_group_id' => $customGroup['id'],
753 $this->fixtures
['contact_custom_token'] = [
754 'id' => $customField['id'],
755 'token' => sprintf('{contact.custom_%s}', $customField['id']),
756 'name' => sprintf('custom_%s', $customField['id']),
757 'value' => 'text ' . substr(sha1(rand()), 0, 7),
764 * Tears down the fixture, for example, closes a network connection.
766 * This method is called after a test is executed.
768 * @throws \CRM_Core_Exception
770 public function tearDown() {
772 $this->mut
->clearMessages();
775 $this->quickCleanup([
776 'civicrm_action_schedule',
777 'civicrm_action_log',
778 'civicrm_membership',
779 'civicrm_participant',
783 $this->callAPISuccess('CustomField', 'delete', ['id' => $this->fixtures
['contact_custom_token']['id']]);
784 $this->callAPISuccess('CustomGroup', 'delete', [
785 'id' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', 'test_contact_cg', 'id', 'name'),
791 * Get mailer examples.
795 public function mailerExamples() {
798 // Some tokens - short as subject has 128char limit in DB.
799 $someTokensTmpl = implode(';;', [
800 // basic contact token
801 '{contact.display_name}',
802 // funny legacy contact token
804 // funny legacy contact token
805 '{contact.gender_id}',
808 // action-scheduler token
809 '{activity.activity_type}',
811 // Further tokens can be tested in the body text/html.
812 $manyTokensTmpl = implode(';;', [
814 '{contact.email_greeting}',
815 $this->fixtures
['contact_custom_token']['token'],
817 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
818 // can really do is check that it has something.
819 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
820 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, $this->fixtures
['contact_custom_token']['value']);
822 // In this example, we use a lot of tokens cutting across multiple components.
824 // Schedule definition.
826 'subject' => "subj $someTokensTmpl",
827 'body_html' => "html $manyTokensTmpl",
828 'body_text' => "text $manyTokensTmpl",
830 // Assertions (regex).
832 'from_name' => '/^FIXME$/',
833 'from_email' => '/^info@EXAMPLE.ORG$/',
834 'subject' => "/^subj $someTokensExpected\$/",
835 'body_html' => "/^html $manyTokensExpected\$/",
836 'body_text' => "/^text $manyTokensExpected\$/",
840 // In this example, we customize the from address.
842 // Schedule definition.
844 'from_name' => 'Bob',
845 'from_email' => 'bob@example.org',
847 // Assertions (regex).
849 'from_name' => '/^Bob$/',
850 'from_email' => '/^bob@example.org$/',
854 // In this example, we autoconvert HTML to text
856 // Schedule definition.
858 'body_html' => '<p>Hello & stuff.</p>',
861 // Assertions (regex).
863 'body_html' => '/^' . preg_quote('<p>Hello & stuff.</p>', '/') . '/',
864 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
868 // In this example, we autoconvert HTML to text
870 // Schedule definition.
873 'body_text' => 'Hello world',
875 // Assertions (regex).
877 'body_html' => '/^--UNDEFINED--$/',
878 'body_text' => '/^Hello world$/',
886 * This generates a single mailing through the scheduled-reminder
887 * system (using an activity-reminder as a baseline) and
888 * checks that the resulting message satisfies various
889 * regular expressions.
891 * @param array $schedule
892 * Values to set/override in the schedule.
893 * Ex: array('subject' => 'Hello, {contact.first_name}!').
894 * @param array $patterns
895 * A list of regexes to compare with the actual email.
896 * Ex: array('subject' => '/^Hello, Alice!/').
897 * Keys: subject, body_text, body_html, from_name, from_email.
899 * @dataProvider mailerExamples
901 * @throws \CRM_Core_Exception
903 public function testMailer($schedule, $patterns) {
904 $actionSchedule = array_merge($this->fixtures
['sched_activity_1day'], $schedule);
905 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
906 $this->assertInternalType('numeric', $actionScheduleDao->id
);
908 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
909 $this->assertInternalType('numeric', $activity->id
);
910 $contact = $this->callAPISuccess('contact', 'create', array_merge(
911 $this->fixtures
['contact'],
913 $this->fixtures
['contact_custom_token']['name'] => $this->fixtures
['contact_custom_token']['value'],
918 $source['contact_id'] = $contact['id'];
919 $source['activity_id'] = $activity->id
;
920 $source['record_type_id'] = 2;
921 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
922 $activityContact->save();
924 CRM_Utils_Time
::setTime('2012-06-14 15:00:00');
925 $this->callAPISuccess('job', 'send_reminder');
926 $this->mut
->assertRecipients([['test-member@example.com']]);
927 foreach ($this->mut
->getAllMessages('ezc') as $message) {
928 /** @var ezcMail $message */
931 $messageArray['subject'] = $message->subject
;
932 $messageArray['from_name'] = $message->from
->name
;
933 $messageArray['from_email'] = $message->from
->email
;
934 $messageArray['body_text'] = '--UNDEFINED--';
935 $messageArray['body_html'] = '--UNDEFINED--';
937 foreach ($message->fetchParts() as $part) {
938 /** @var ezcMailText ezcMailText */
939 if ($part instanceof ezcMailText
&& $part->subType
=== 'html') {
940 $messageArray['body_html'] = $part->text
;
942 if ($part instanceof ezcMailText
&& $part->subType
=== 'plain') {
943 $messageArray['body_text'] = $part->text
;
947 foreach ($patterns as $field => $pattern) {
948 $this->assertRegExp($pattern, $messageArray[$field],
949 "Check that '$field'' matches regex. " . print_r(['expected' => $patterns, 'actual' => $messageArray], 1));
952 $this->mut
->clearMessages();
956 * Test calculated activity schedule.
958 * @throws \CRM_Core_Exception
960 public function testActivityDateTimeMatchNonRepeatableSchedule() {
961 $this->createScheduleFromFixtures('sched_activity_1day');
963 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
964 $this->assertInternalType('numeric', $activity->id
);
965 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
966 $activity->subject
= 'Test subject for Phonecall';
969 $source['contact_id'] = $contact['id'];
970 $source['activity_id'] = $activity->id
;
971 $source['record_type_id'] = 2;
972 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
973 $activityContact->save();
975 $this->assertCronRuns([
977 // Before the 24-hour mark, no email
978 'time' => '2012-06-14 04:00:00',
983 // After the 24-hour mark, an email
984 'time' => '2012-06-14 15:00:00',
985 'recipients' => [['test-member@example.com']],
986 'subjects' => ['1-Day (non-repeating) (about Phone Call)'],
989 // Run cron again; message already sent
994 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, FALSE, FALSE, 'name');
995 $activityDAO = new CRM_Activity_DAO_Activity();
996 $activityDAO->source_record_id
= $activity->id
;
997 $activityDAO->activity_type_id
= array_search('Reminder Sent', $activityTypes);
998 $activityDAO->find();
999 while ($activityDAO->fetch()) {
1000 $this->assertContains($activity->subject
, $activityDAO->details
);
1005 * Test schedule creation on repeatable schedule.
1007 * @throws \CRM_Core_Exception
1009 public function testActivityDateTimeMatchRepeatableSchedule() {
1010 $this->createScheduleFromFixtures('sched_activity_1day_r');
1012 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
1013 $this->assertTrue(is_numeric($activity->id
));
1014 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1017 $source['contact_id'] = $contact['id'];
1018 $source['activity_id'] = $activity->id
;
1019 $source['record_type_id'] = 2;
1020 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1021 $activityContact->save();
1023 $this->assertCronRuns([
1025 // Before the 24-hour mark, no email
1026 'time' => '012-06-14 04:00:00',
1031 // After the 24-hour mark, an email
1032 'time' => '2012-06-14 15:00:00',
1033 'recipients' => [['test-member@example.com']],
1034 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1037 // Run cron 4 hours later; first message already sent
1038 'time' => '2012-06-14 20:00:00',
1043 // Run cron 6 hours later; send second message.
1044 'time' => '2012-06-14 21:00:01',
1045 'recipients' => [['test-member@example.com']],
1046 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1052 * @throws \CRM_Core_Exception
1054 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate() {
1055 $this->createScheduleFromFixtures('sched_activity_1day_r_on_abs_date');
1057 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
1058 $this->assertInternalType('numeric', $activity->id
);
1059 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
1062 $source['contact_id'] = $contact['id'];
1063 $source['activity_id'] = $activity->id
;
1064 $source['record_type_id'] = 2;
1065 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
1066 $activityContact->save();
1068 $this->assertCronRuns([
1070 // Before the 24-hour mark, no email
1071 'time' => '2012-06-13 04:00:00',
1076 // On absolute date set on 2012-06-14
1077 'time' => '2012-06-14 00:00:00',
1078 'recipients' => [['test-member@example.com']],
1079 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1082 // Run cron 4 hours later; first message already sent
1083 'time' => '2012-06-14 04:00:00',
1088 // Run cron 6 hours later; send second message.
1089 'time' => '2012-06-14 06:00:01',
1090 'recipients' => [['test-member@example.com']],
1091 'subjects' => ['1-Day (repeating) (about Phone Call)'],
1097 * Test event with only an absolute date.
1099 * @throws \CRM_Core_Exception
1101 public function testEventNameWithAbsoluteDateAndNothingElse() {
1102 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 1]));
1103 $this->callAPISuccess('Email', 'create', [
1104 'contact_id' => $participant->contact_id
,
1105 'email' => 'test-event@example.com',
1107 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
1109 $actionSchedule = $this->fixtures
['sched_eventname_1day_on_abs_date'];
1110 $actionSchedule['entity_value'] = $participant->event_id
;
1111 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1113 $this->assertCronRuns([
1115 // Before the 24-hour mark, no email
1116 'time' => '2012-06-13 04:00:00',
1121 // On absolute date set on 2012-06-14
1122 'time' => '2012-06-14 00:00:00',
1123 'recipients' => [['test-event@example.com']],
1124 'subjects' => ['sched_eventname_1day_on_abs_date'],
1127 // Run cron 4 hours later; first message already sent
1128 'time' => '2012-06-14 04:00:00',
1136 * For contacts/activities which don't match the schedule filter,
1137 * an email should *not* be sent.
1139 // TODO // function testActivityDateTime_NonMatch() { }
1142 * For contacts/members which match schedule based on join/start date,
1143 * an email should be sent.
1145 * @throws \CRM_Core_Exception
1147 public function testMembershipDateMatch() {
1148 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], ['status_id' => 1]));
1149 $this->assertInternalType('numeric', $membership->id
);
1150 $this->callAPISuccess('Email', 'create', [
1151 'contact_id' => $membership->contact_id
,
1152 'email' => 'test-member@example.com',
1153 'location_type_id' => 1,
1157 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1158 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1159 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1160 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1161 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1163 // start_date=2012-03-15 ; schedule is 2 weeks after join_date
1164 $this->assertCronRuns([
1166 // Before the 2-week mark, no email.
1167 'time' => '2012-03-28 01:00:00',
1172 // After the 2-week mark, send an email.
1173 'time' => '2012-03-29 01:00:00',
1174 'recipients' => [['test-member@example.com']],
1175 'subjects' => ['subject sched_membership_join_2week (joined March 15th, 2012)'],
1179 $actionSchedule = $this->fixtures
['sched_membership_start_1week'];
1180 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1181 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1182 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1184 // start_date=2012-03-15 ; schedule is 1 weeks after start_date
1185 $this->assertCronRuns([
1187 // Before the 2-week mark, no email.
1188 'time' => '2012-03-21 01:00:00',
1193 // After the 2-week mark, send an email.
1194 'time' => '2012-03-22 01:00:00',
1195 'recipients' => [['test-member@example.com']],
1196 'subjects' => ['subject sched_membership_start_1week (joined March 15th, 2012)'],
1202 * CRM-21675: Support parent and smart group in 'Limit to' field
1204 * @throws \CRM_Core_Exception
1206 public function testScheduleReminderWithParentGroup() {
1207 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1208 $this->individualCreate(['birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com']);
1209 // Contact B with birth-date at '07-07-2005', later got added in regular group
1210 $contactID2 = $this->individualCreate(['birth_date' => '20050707', 'email' => 'def@test.com'], 1);
1211 // Contact C with birth-date at '07-07-2005', but not included in any group
1212 $this->individualCreate(['birth_date' => '20050707', 'email' => 'ghi@test.com'], 2);
1214 // create regular group and add Contact B to it
1215 $groupID = $this->groupCreate();
1216 $this->callAPISuccess('GroupContact', 'Create', [
1217 'group_id' => $groupID,
1218 'contact_id' => $contactID2,
1221 // create smart group which will contain all Male contacts
1222 $smartGroupParams = ['form_values' => ['gender_id' => 1]];
1223 $smartGroupID = $this->smartGroupCreate(
1226 'name' => 'new_smart_group',
1227 'title' => 'New Smart Group',
1228 'parents' => [$groupID => 1],
1232 $actionScheduleParams = [
1233 'name' => 'sched_contact_bday_yesterday',
1234 'title' => 'sched_contact_bday_yesterday',
1235 'absolute_date' => '',
1236 'body_html' => '<p>you look like you were born yesterday!</p>',
1237 'body_text' => 'you look like you were born yesterday!',
1240 'end_frequency_interval' => '',
1241 'end_frequency_unit' => '',
1242 'entity_status' => 1,
1243 'entity_value' => 'birth_date',
1245 'group_id' => $groupID,
1249 'msg_template_id' => '',
1251 'recipient_listing' => '',
1252 'recipient_manual' => '',
1253 'record_activity' => 1,
1254 'repetition_frequency_interval' => '',
1255 'repetition_frequency_unit' => '',
1256 'start_action_condition' => 'after',
1257 'start_action_date' => 'date_field',
1258 'start_action_offset' => '1',
1259 'start_action_unit' => 'day',
1260 'subject' => 'subject sched_contact_bday_yesterday',
1263 // Create schedule reminder where parent group ($groupID) is selectd to limit recipients,
1264 // which contain a individual contact - $contactID2 and is parent to smart group.
1265 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
1266 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1267 $this->assertCronRuns([
1269 // On the birthday, no email.
1270 'time' => '2005-07-07 01:00:00',
1274 // The next day, send an email.
1275 'time' => '2005-07-08 20:00:00',
1286 $this->groupDelete($smartGroupID);
1287 $this->groupDelete($groupID);
1291 * Test end date email sent.
1293 * For contacts/members which match schedule based on join date,
1294 * an email should be sent.
1296 * @throws \API_Exception
1297 * @throws \Civi\API\Exception\UnauthorizedException
1299 public function testMembershipJoinDateNonMatch() {
1300 $this->createMembershipFromFixture('rolling_membership', '', ['email' => 'test-member@example.com']);
1301 // Add an alternative membership type, and only send messages for that type
1302 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', []);
1303 $this->assertInternalType('numeric', $extraMembershipType->id
);
1304 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_membership_join_2week']);
1305 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1306 $actionScheduleDao->entity_value
= $extraMembershipType->id
;
1307 $actionScheduleDao->save();
1309 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1310 $this->assertCronRuns([
1312 // After the 2-week mark, don't send email because we have different membership type.
1313 'time' => '2012-03-29 01:00:00',
1320 * Test that the first and SECOND notifications are sent out.
1322 * @throws \CRM_Core_Exception
1324 public function testMembershipEndDateRepeat() {
1325 // creates membership with end_date = 20120615
1326 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1327 $this->callAPISuccess('Email', 'create', [
1328 'contact_id' => $membership->contact_id
,
1329 'email' => 'test-member@example.com',
1331 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1333 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
1334 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1335 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1337 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1338 $this->assertCronRuns([
1340 // After the 1-month mark, no email
1341 'time' => '2012-07-15 01:00:00',
1345 // After the 2-month mark, send an email.
1346 'time' => '2012-08-15 01:00:00',
1347 'recipients' => [['test-member@example.com']],
1350 // 4 weeks after first email send first repeat
1351 'time' => '2012-09-12 01:00:00',
1352 'recipients' => [['test-member@example.com']],
1355 // 1 week after first repeat send nothing
1356 // There was a bug where the first repeat went out and then
1357 // it would keep going out every cron run. This is to check that's
1359 'time' => '2012-09-19 01:00:00',
1363 // 4 weeks after first repeat send second repeat
1364 'time' => '2012-10-10 01:00:00',
1365 'recipients' => [['test-member@example.com']],
1368 // 4 months after membership end, send nothing
1369 'time' => '2012-10-15 01:00:00',
1373 // 5 months after membership end, send nothing
1374 'time' => '2012-11-15 01:00:00',
1381 * Test behaviour when date changes.
1383 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1387 * @throws \CRM_Core_Exception
1389 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
1390 // creates membership with end_date = 20120615
1391 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1392 $this->callAPISuccess('Email', 'create', [
1393 'contact_id' => $membership->contact_id
,
1394 'email' => 'test-member@example.com',
1396 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1398 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
1399 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1400 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1401 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1402 $this->assertCronRuns([
1404 // After the 2-week mark, send an email.
1405 'time' => '2012-08-15 01:00:00',
1406 'recipients' => [['test-member@example.com']],
1410 // Extend membership - reminder should NOT go out.
1411 $this->callAPISuccess('membership', 'create', ['id' => $membership->id
, 'end_date' => '2014-01-01']);
1412 $this->assertCronRuns([
1414 // After the 2-week mark, send an email.
1415 'time' => '2012-09-12 01:00:00',
1422 * Test membership end date email sends.
1424 * For contacts/members which match schedule based on end date,
1425 * an email should be sent.
1427 * @throws \CRM_Core_Exception
1429 public function testMembershipEndDateMatch() {
1430 // creates membership with end_date = 20120615
1431 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1432 $this->callAPISuccess('Email', 'create', [
1433 'contact_id' => $membership->contact_id
,
1434 'email' => 'test-member@example.com',
1436 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1438 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
1439 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1440 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1441 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1443 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1444 $this->assertCronRuns([
1446 // Before the 2-week mark, no email.
1447 'time' => '2012-05-31 01:00:00',
1451 // After the 2-week mark, send an email.
1452 'time' => '2012-06-01 01:00:00',
1453 'recipients' => [['test-member@example.com']],
1456 // After the email is sent, another one is not sent
1457 'time' => '2012-06-01 02:00:00',
1462 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1463 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1464 $membership->end_date
= '2012-09-15';
1465 $membership->save();
1467 //change the email id of chosen membership contact to assert
1468 //recipient of not the previously sent mail but the new one
1469 $result = $this->callAPISuccess('Email', 'create', [
1471 'contact_id' => $membership->contact_id
,
1472 'email' => 'member2@example.com',
1474 $this->assertAPISuccess($result);
1476 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1477 $this->assertCronRuns([
1479 // Before the 2-week mark, no email
1480 'time' => '2012-08-31 01:00:00',
1484 // After the 2-week mark, send an email
1485 'time' => '2012-09-01 01:00:00',
1486 'recipients' => [['member2@example.com']],
1489 // After the email is sent, another one is not sent
1490 'time' => '2012-09-01 02:00:00',
1495 $membership->end_date
= '2012-12-15';
1496 $membership->save();
1497 // end_date=2012-12-15 ; schedule is 2 weeks before end_date
1498 $this->assertCronRuns([
1500 // Before the 2-week mark, no email
1501 'time' => '2012-11-30 01:00:00',
1505 // After the 2-week mark, send an email
1506 'time' => '2012-12-01 01:00:00',
1507 'recipients' => [['member2@example.com']],
1510 // After the email is sent, another one is not sent
1511 'time' => '2012-12-01 02:00:00',
1519 * @param array $contactFixture
1520 * @param int $membershipTypeId
1522 * @return array|NULL|object
1523 * @throws \CRM_Core_Exception
1525 public function createMembershipAndContact($contactFixture, $membershipTypeId) {
1526 $result = $this->callAPISuccess('contact', 'create', $contactFixture);
1527 $contact = $result['values'][$result['id']];
1530 'contact_id' => $contact['id'],
1531 'membership_type_id' => $membershipTypeId,
1532 'owner_membership_id' => 'NULL',
1534 $params = array_merge($this->fixtures
['rolling_membership'], $params);
1535 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $params);
1536 $this->assertInternalType('numeric', $membership->id
);
1541 * This test is very similar to testMembershipEndDateMatch, but it adds
1542 * another contact because there was a bug in
1543 * RecipientBuilder::buildRelFirstPass where it was only sending the
1544 * reminder for the first contact returned in a query for renewed
1545 * memberships. Other contacts wouldn't get the mail.
1547 * @throws \CRM_Core_Exception
1549 public function testMultipleMembershipEndDateMatch() {
1550 $membershipTypeId = $this->membershipTypeCreate($this->fixtures
['rolling_membership']['membership_type_id']);
1551 $membershipOne = $this->createMembershipAndContact($this->fixtures
['contact'], $membershipTypeId);
1552 $membershipTwo = $this->createMembershipAndContact($this->fixtures
['contact_2'], $membershipTypeId);
1553 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
1554 $actionSchedule['entity_value'] = $membershipTypeId;
1555 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1556 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1558 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1559 $this->assertCronRuns([
1561 // Before the 2-week mark, no email.
1562 'time' => '2012-05-31 01:00:00',
1566 // After the 2-week mark, send emails.
1567 'time' => '2012-06-01 01:00:00',
1569 ['test-member@example.com'],
1570 ['test-contact-2@example.com'],
1574 // After the email is sent, another one is not sent
1575 'time' => '2012-06-01 02:00:00',
1580 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1581 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1582 $membershipOne->end_date
= '2012-09-15';
1583 $membershipOne->save();
1584 $membershipTwo->end_date
= '2012-09-15';
1585 $membershipTwo->save();
1587 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1588 $this->assertCronRuns([
1590 // Before the 2-week mark, no email
1591 'time' => '2012-08-31 01:00:00',
1595 // After the 2-week mark, send an email
1596 'time' => '2012-09-01 01:00:00',
1598 ['test-member@example.com'],
1599 ['test-contact-2@example.com'],
1603 // After the email is sent, another one is not sent
1604 'time' => '2012-06-01 02:00:00',
1611 * Test membership end date email.
1613 * For contacts/members which match schedule based on end date,
1614 * an email should be sent.
1616 * @throws \CRM_Core_Exception
1618 public function testMembershipEndDateNoMatch() {
1619 // creates membership with end_date = 20120615
1620 $membership = $this->createMembershipFromFixture('rolling_membership', 'Grace');
1621 $this->callAPISuccess('Email', 'create', [
1622 'contact_id' => $membership->contact_id
,
1623 'email' => 'test-member@example.com',
1625 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1627 $actionSchedule = $this->fixtures
['sched_membership_end_2month'];
1628 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1629 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1630 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1632 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1633 $this->assertCronRuns([
1635 // Before the 2-week mark, no email.
1636 'time' => '2012-05-31 01:00:00',
1640 // After the 2-week mark, no email
1641 'time' => '2013-05-01 01:00:00',
1648 * @throws \CRM_Core_Exception
1650 public function testContactBirthDateNoAnniv() {
1651 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1652 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1653 $actionSchedule = $this->fixtures
['sched_contact_bday_yesterday'];
1654 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1655 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1656 $this->assertCronRuns([
1658 // On the birthday, no email.
1659 'time' => '2005-07-07 01:00:00',
1663 // The next day, send an email.
1664 'time' => '2005-07-08 20:00:00',
1665 'recipients' => [['test-bday@example.com']],
1671 * @throws \CRM_Core_Exception
1673 public function testContactBirthDateAnniversary() {
1674 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1675 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1676 $actionSchedule = $this->fixtures
['sched_contact_bday_anniv'];
1677 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1678 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1679 $this->assertCronRuns([
1681 // On some random day, no email.
1682 'time' => '2014-03-07 01:00:00',
1686 // On the eve of their 9th birthday, send an email.
1687 'time' => '2014-07-06 20:00:00',
1688 'recipients' => [['test-bday@example.com']],
1694 * @throws \CRM_Core_Exception
1696 public function testContactCustomDateNoAnniversary() {
1698 'title' => 'Test_Group',
1699 'name' => 'test_group',
1700 'extends' => ['Individual'],
1701 'style' => 'Inline',
1702 'is_multiple' => FALSE,
1705 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1707 'label' => 'Graduation',
1708 'data_type' => 'Date',
1709 'html_type' => 'Select Date',
1710 'custom_group_id' => $createGroup['id'],
1712 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1713 $contactParams = $this->fixtures
['contact'];
1714 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1715 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1716 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1717 $actionSchedule = $this->fixtures
['sched_contact_grad_tomorrow'];
1718 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1719 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1720 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1721 $this->assertCronRuns([
1723 // On some random day, no email.
1724 'time' => '2014-03-07 01:00:00',
1728 // On the eve of their graduation, send an email.
1729 'time' => '2013-12-15 20:00:00',
1730 'recipients' => [['test-member@example.com']],
1733 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
1737 * @throws \CRM_Core_Exception
1739 public function testContactCreatedNoAnniversary() {
1740 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1741 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1742 $this->createScheduleFromFixtures('sched_contact_created_yesterday');
1743 $this->assertCronRuns([
1745 // On the date created, no email.
1746 'time' => $contact['values'][$contact['id']]['created_date'],
1750 // The next day, send an email.
1751 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1752 'recipients' => [['test-bday@example.com']],
1758 * Test the impact of changing the anniversary.
1760 * @throws \CRM_Core_Exception
1762 public function testContactModifiedAnniversary() {
1763 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1764 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1765 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', ['id' => $contact['id'], 'return' => 'modified_date']);
1766 $actionSchedule = $this->fixtures
['sched_contact_mod_anniv'];
1767 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1768 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1769 $this->assertCronRuns([
1771 // On some random day, no email.
1772 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1776 // On the eve of 3 years after they were modified, send an email.
1777 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1778 'recipients' => [['test-bday@example.com']],
1784 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
1786 * @throws \API_Exception
1787 * @throws \CRM_Core_Exception
1788 * @throws \Civi\API\Exception\UnauthorizedException
1790 public function testMembershipLimitToNone() {
1791 // creates membership with end_date = 20120615
1792 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current');
1793 $result = $this->callAPISuccess('Email', 'create', [
1794 'contact_id' => $membership->contact_id
,
1795 'email' => 'member@example.com',
1797 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1798 $this->callAPISuccess('contact', 'create', ['email' => 'b@c.com', 'contact_type' => 'Individual']);
1800 $this->assertAPISuccess($result);
1802 $actionSchedule = $this->fixtures
['sched_membership_end_limit_to_none'];
1803 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1804 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1805 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1807 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1808 $this->assertCronRuns([
1810 // Before the 2-week mark, no email.
1811 'time' => '2012-05-31 01:00:00',
1812 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1819 * Test handling of reference date for memberships.
1821 * @throws \API_Exception
1822 * @throws \CRM_Core_Exception
1823 * @throws \Civi\API\Exception\UnauthorizedException
1825 public function testMembershipWithReferenceDate() {
1826 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1827 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1829 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1830 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1831 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1832 $this->assertInternalType('numeric', $actionScheduleDao->id
);
1834 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1835 $this->assertCronRuns([
1837 // After the 2-week mark, send an email
1838 'time' => '2012-03-29 01:00:00',
1839 'recipients' => [['member@example.com']],
1842 // After the 2-week 1day mark, don't send an email
1843 'time' => '2012-03-30 01:00:00',
1848 //check if reference date is set to membership's join date
1849 //as per the action_start_date chosen for current schedule reminder
1850 $this->assertEquals('2012-03-15 00:00:00',
1851 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id
, 'reference_date', 'contact_id')
1854 //change current membership join date that may signifies as membership renewal activity
1855 $membership->join_date
= '2012-03-29';
1856 $membership->save();
1858 $this->assertCronRuns([
1860 // After the 13 days of the changed join date 2012-03-29, don't send an email
1861 'time' => '2012-04-11 01:00:00',
1865 // After the 2-week of the changed join date 2012-03-29, send an email
1866 'time' => '2012-04-12 01:00:00',
1867 'recipients' => [['member@example.com']],
1870 $this->assertCronRuns([
1872 // It should not re-send on the same day
1873 'time' => '2012-04-12 01:00:00',
1880 * Test multiple membership reminder.
1882 * @throws \API_Exception
1883 * @throws \CRM_Core_Exception
1884 * @throws \Civi\API\Exception\UnauthorizedException
1886 public function testMembershipOnMultipleReminder() {
1887 $membership = $this->createMembershipFromFixture('rolling_membership', 'Current', ['email' => 'member@example.com']);
1888 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
1890 // Send email 2 weeks before end_date
1891 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week'];
1892 // Send email on end_date/expiry date
1893 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date'];
1894 // Send email 1 day after end_date/grace period
1895 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date'];
1896 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id
;
1897 foreach (['actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter'] as $value) {
1898 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
1899 $this->assertInternalType('numeric', $
$value->id
);
1902 $this->assertCronRuns(
1905 // 1day 2weeks before membership end date(MED), don't send mail
1906 'time' => '2012-05-31 01:00:00',
1910 // 2 weeks before MED, send an email
1911 'time' => '2012-06-01 01:00:00',
1912 'recipients' => [['member@example.com']],
1915 // 1day before MED, don't send mail
1916 'time' => '2012-06-14 01:00:00',
1920 // On MED, send an email
1921 'time' => '2012-06-15 00:00:00',
1922 'recipients' => [['member@example.com']],
1925 // After 1day of MED, send an email
1926 'time' => '2012-06-16 01:00:00',
1927 'recipients' => [['member@example.com']],
1930 // After 1day 1min of MED, don't send an email
1931 'time' => '2012-06-17 00:01:00',
1937 // Assert the timestamp as of when the emails of respective three reminders as configured
1938 // 2 weeks before, on and 1 day after MED, are sent
1939 $this->assertApproxEquals(
1940 strtotime('2012-06-01 01:00:00'),
1941 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1942 // Variation in test execution time.
1945 $this->assertApproxEquals(
1946 strtotime('2012-06-15 00:00:00'),
1947 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1948 // Variation in test execution time.
1951 $this->assertApproxEquals(
1952 strtotime('2012-06-16 01:00:00'),
1953 strtotime(CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE)),
1954 // Variation in test execution time.
1958 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1959 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1960 $membership->end_date
= '2012-06-20';
1961 $membership->save();
1963 $this->callAPISuccess('Contact', 'get', ['id' => $membership->contact_id
]);
1964 $this->assertCronRuns(
1967 // 1day 2weeks before membership end date(MED), don't send mail
1968 'time' => '2012-06-05 01:00:00',
1972 // 2 weeks before MED, send an email
1973 'time' => '2012-06-06 01:00:00',
1974 'recipients' => [['member@example.com']],
1977 // 1day before MED, don't send mail
1978 'time' => '2012-06-19 01:00:00',
1982 // On MED, send an email
1983 'time' => '2012-06-20 00:00:00',
1984 'recipients' => [['member@example.com']],
1987 // After 1day of MED, send an email
1988 'time' => '2012-06-21 01:00:00',
1989 'recipients' => [['member@example.com']],
1992 // After 1day 1min of MED, don't send an email
1993 'time' => '2012-07-21 00:01:00',
2000 * Test reminders sent on custom data anniversary.
2002 * @throws \CRM_Core_Exception
2004 public function testContactCustomDate_Anniversary() {
2006 'title' => 'Test_Group now',
2007 'name' => 'test_group_now',
2008 'extends' => ['Individual'],
2009 'style' => 'Inline',
2010 'is_multiple' => FALSE,
2013 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
2015 'label' => 'Graduation',
2016 'data_type' => 'Date',
2017 'html_type' => 'Select Date',
2018 'custom_group_id' => $createGroup['id'],
2020 $createField = $this->callAPISuccess('custom_field', 'create', $field);
2022 $contactParams = $this->fixtures
['contact'];
2023 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
2024 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
2025 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
2026 $actionSchedule = $this->fixtures
['sched_contact_grad_anniv'];
2027 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
2028 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2029 $this->assertTrue(is_numeric($actionScheduleDao->id
));
2030 $this->assertCronRuns([
2032 // On some random day, no email.
2033 'time' => '2014-03-07 01:00:00',
2037 // A week after their 5th anniversary of graduation, send an email.
2038 'time' => '2018-12-23 20:00:00',
2039 'recipients' => [['test-member@example.com']],
2042 $this->callAPISuccess('custom_group', 'delete', ['id' => $createGroup['id']]);
2046 * Test sched reminder set via registration date.
2048 * @throws \CRM_Core_Exception
2050 public function testEventTypeRegistrationDate() {
2051 $contact = $this->individualCreate(['email' => 'test-event@example.com']);
2052 //Add it as a participant to an event ending registration - 7 days from now.
2054 'start_date' => date('Ymd', strtotime('-5 day')),
2055 'end_date' => date('Ymd', strtotime('+7 day')),
2056 'registration_start_date' => date('Ymd', strtotime('-5 day')),
2057 'registration_end_date' => date('Ymd', strtotime('+7 day')),
2059 $event = $this->eventCreate($params);
2060 $this->participantCreate(['contact_id' => $contact, 'event_id' => $event['id']]);
2062 //Create a scheduled reminder to send email 7 days before registration date.
2063 $actionSchedule = $this->fixtures
['sched_eventtype_start_1week_before'];
2064 $actionSchedule['start_action_offset'] = 7;
2065 $actionSchedule['start_action_unit'] = 'day';
2066 $actionSchedule['start_action_date'] = 'registration_end_date';
2067 $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id'];
2068 $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', [
2070 'name' => 'Attended',
2072 $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2073 //Run the cron and verify if an email was sent.
2074 $this->assertCronRuns([
2076 'time' => date('Y-m-d'),
2077 'recipients' => [['test-event@example.com']],
2083 'email' => 'test-event2@example.com',
2085 $contact2 = $this->individualCreate($contactParams);
2086 //Create an event with registration end date = 2 week from now.
2087 $params['end_date'] = date('Ymd', strtotime('+2 week'));
2088 $params['registration_end_date'] = date('Ymd', strtotime('+2 week'));
2089 $event2 = $this->eventCreate($params);
2090 $this->participantCreate(['contact_id' => $contact2, 'event_id' => $event2['id']]);
2092 //Assert there is no reminder sent to the contact.
2093 $this->assertCronRuns([
2095 'time' => date('Y-m-d'),
2100 //Modify the sched reminder to be sent 2 week from registration end date.
2101 $this->callAPISuccess('action_schedule', 'create', [
2102 'id' => $actionSched['id'],
2103 'start_action_offset' => 2,
2104 'start_action_unit' => 'week',
2107 //Contact should receive the reminder now.
2108 $this->assertCronRuns([
2110 'time' => date('Y-m-d'),
2111 'recipients' => [['test-event2@example.com']],
2117 * Test sched reminder set via start date.
2119 * @throws \CRM_Core_Exception
2121 public function testEventTypeStartDate() {
2122 // Create event+participant with start_date = 20120315, end_date = 20120615.
2123 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2124 $this->callAPISuccess('Email', 'create', [
2125 'contact_id' => $participant->contact_id
,
2126 'email' => 'test-event@example.com',
2128 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2130 $actionSchedule = $this->fixtures
['sched_eventtype_start_1week_before'];
2131 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2132 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2134 //echo "CREATED\n"; ob_flush(); sleep(20);
2136 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
2137 $this->assertCronRuns([
2140 'time' => '2012-03-02 01:00:00',
2145 'time' => '2012-03-08 01:00:00',
2146 'recipients' => [['test-event@example.com']],
2149 // And then nothing else
2150 'time' => '2012-03-16 01:00:00',
2157 * Test schedule on event end date.
2159 * @throws \CRM_Core_Exception
2161 public function testEventTypeEndDateRepeat() {
2162 // Create event+participant with start_date = 20120315, end_date = 20120615.
2163 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], ['status_id' => 2]));
2164 $this->callAPISuccess('Email', 'create', [
2165 'contact_id' => $participant->contact_id
,
2166 'email' => 'test-event@example.com',
2168 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $participant->contact_id
]));
2170 $actionSchedule = $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'];
2171 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
2172 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
2174 $this->assertCronRuns([
2177 'time' => '2012-08-13 01:00:00',
2181 // After the 2-month mark, send an email.
2182 'time' => '2012-08-16 01:00:00',
2183 'recipients' => [['test-event@example.com']],
2186 // After 2 months and 1 week, don't repeat yet.
2187 'time' => '2012-08-23 02:00:00',
2191 // After 2 months and 2 weeks
2192 'time' => '2012-08-30 02:00:00',
2193 'recipients' => [['test-event@example.com']],
2196 // After 2 months and 4 week
2197 'time' => '2012-09-13 02:00:00',
2198 'recipients' => [['test-event@example.com']],
2201 // After 2 months and 6 weeks
2202 'time' => '2012-09-27 01:00:00',
2208 // TODO // function testMembershipEndDate_NonMatch() { }
2209 // TODO // function testEventTypeStartDate_Match() { }
2210 // TODO // function testEventTypeEndDate_Match() { }
2211 // TODO // function testEventNameStartDate_Match() { }
2212 // TODO // function testEventNameEndDate_Match() { }
2215 * Run a series of cron jobs and make an assertion about email deliveries.
2217 * @param array $cronRuns
2218 * array specifying when to run cron and what messages to expect; each item is an array with keys:
2219 * - time: string, e.g. '2012-06-15 21:00:01'
2220 * - recipients: array(array(string)), list of email addresses which should receive messages
2222 * @throws \CRM_Core_Exception
2224 public function assertCronRuns($cronRuns) {
2225 foreach ($cronRuns as $cronRun) {
2226 CRM_Utils_Time
::setTime($cronRun['time']);
2227 $this->callAPISuccess('job', 'send_reminder', []);
2228 $this->mut
->assertRecipients($cronRun['recipients']);
2229 if (array_key_exists('subjects', $cronRun)) {
2230 $this->mut
->assertSubjects($cronRun['subjects']);
2232 $this->mut
->clearMessages();
2238 * (DAO_Name => array(int)) List of items to garbage-collect during tearDown
2240 private $_testObjects;
2243 * Sets up the fixture, for example, opens a network connection.
2245 * This method is called before a test is executed.
2247 protected function _setUp() {
2248 $this->_testObjects
= [];
2252 * Tears down the fixture, for example, closes a network connection.
2254 * This method is called after a test is executed.
2256 protected function _tearDown() {
2258 $this->deleteTestObjects();
2262 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
2263 * created entities and provides for brainless cleanup.
2265 * @see CRM_Core_DAO::createTestObject
2268 * @param array $params
2269 * @param int $numObjects
2270 * @param bool $createOnly
2272 * @return array|NULL|object
2274 public function createTestObject($daoName, $params = [], $numObjects = 1, $createOnly = FALSE) {
2275 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
2276 if (is_array($objects)) {
2277 $this->registerTestObjects($objects);
2280 $this->registerTestObjects([$objects]);
2286 * @param array $objects
2287 * DAO or BAO objects.
2289 public function registerTestObjects($objects) {
2290 //if (is_object($objects)) {
2291 // $objects = array($objects);
2293 foreach ($objects as $object) {
2294 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
2295 $this->_testObjects
[$daoName][] = $object->id
;
2299 public function deleteTestObjects() {
2300 // Note: You might argue that the FK relations between test
2301 // objects could make this problematic; however, it should
2302 // behave intuitively as long as we mentally split our
2303 // test-objects between the "manual/primary records"
2304 // and the "automatic/secondary records"
2305 foreach ($this->_testObjects
as $daoName => $daoIds) {
2306 foreach ($daoIds as $daoId) {
2307 CRM_Core_DAO
::deleteTestObjects($daoName, ['id' => $daoId]);
2310 $this->_testObjects
= [];
2314 * Test that the various repetition units work correctly.
2317 public function testRepetitionFrequencyUnit() {
2318 $membershipTypeParams = [
2319 'duration_interval' => '1',
2320 'duration_unit' => 'year',
2322 'period_type' => 'rolling',
2324 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
2325 $interval_units = ['hour', 'day', 'week', 'month', 'year'];
2326 foreach ($interval_units as $interval_unit) {
2327 $membershipEndDate = DateTime
::createFromFormat('Y-m-d H:i:s', "2013-03-15 00:00:00");
2329 'contact_type' => 'Individual',
2330 'first_name' => 'Test',
2331 'last_name' => "Interval $interval_unit",
2334 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2335 $this->assertTrue(is_numeric($contact->id
));
2337 'contact_id' => $contact->id
,
2339 'email' => "test-member-{$interval_unit}@example.com",
2340 'location_type_id' => 1,
2342 $email = $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2343 $this->assertTrue(is_numeric($email->id
));
2344 $membershipParams = [
2345 'membership_type_id' => $membershipType->id
,
2346 'contact_id' => $contact->id
,
2347 'join_date' => '20120315',
2348 'start_date' => '20120315',
2349 'end_date' => '20130315',
2353 $membershipParams['status-id'] = 1;
2354 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2355 $actionScheduleParams = $this->fixtures
['sched_on_membership_end_date_repeat_interval'];
2356 $actionScheduleParams['entity_value'] = $membershipType->id
;
2357 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
2358 $actionScheduleParams['repetition_frequency_interval'] = 2;
2359 $actionSchedule = CRM_Core_BAO_ActionSchedule
::add($actionScheduleParams);
2360 $this->assertTrue(is_numeric($actionSchedule->id
));
2361 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
2362 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
2363 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
2366 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
2370 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
2371 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2374 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
2378 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
2379 'recipients' => [["test-member-{$interval_unit}@example.com"]],
2382 $this->assertCronRuns($cronRuns);
2383 $actionSchedule->delete();
2384 $membership->delete();
2389 * Inherited members without permission to edit the main member contact should
2390 * not get reminders.
2392 * However, just because a contact inherits one membership doesn't mean
2393 * reminders for other memberships should be suppressed.
2397 * @throws \CRM_Core_Exception
2399 public function testInheritedMembershipPermissions() {
2400 // Set up common parameters for memberships.
2401 $membershipParams = $this->fixtures
['rolling_membership'];
2402 $membershipParams['status_id'] = 1;
2404 $membershipParams['membership_type_id']['relationship_type_id'] = 1;
2405 $membershipParams['membership_type_id']['relationship_direction'] = 'b_a';
2406 $membershipType1 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2408 // We'll create a new membership type that can be held at the same time as
2410 $membershipParams['membership_type_id']['relationship_type_id'] = 'NULL';
2411 $membershipParams['membership_type_id']['relationship_direction'] = 'NULL';
2412 $membershipType2 = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipParams['membership_type_id']);
2414 // Create the parent membership and contact
2415 $membershipParams['membership_type_id'] = $membershipType1->id
;
2416 $mainMembership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2419 'contact_type' => 'Individual',
2420 'first_name' => 'Mom',
2421 'last_name' => 'Rel',
2424 $this->createTestObject('CRM_Contact_DAO_Contact', array_merge($contactParams, ['id' => $mainMembership->contact_id
]));
2427 'contact_id' => $mainMembership->contact_id
,
2428 'email' => 'test-member@example.com',
2429 'location_type_id' => 1,
2432 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2434 // Set up contacts and emails for the two children
2435 $contactParams['first_name'] = 'Favorite';
2436 $permChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2437 $emailParams['email'] = 'favorite@example.com';
2438 $emailParams['contact_id'] = $permChild->id
;
2439 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2441 $contactParams['first_name'] = 'Black Sheep';
2442 $nonPermChild = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
2443 $emailParams['email'] = 'black.sheep@example.com';
2444 $emailParams['contact_id'] = $nonPermChild->id
;
2445 $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
2447 // Each child gets a relationship, one with permission to edit the parent. This
2448 // will trigger inherited memberships for the first membership type
2450 'relationship_type_id' => 1,
2451 'contact_id_a' => $nonPermChild->id
,
2452 'contact_id_b' => $mainMembership->contact_id
,
2455 $this->callAPISuccess('relationship', 'create', $relParams);
2457 $relParams['contact_id_a'] = $permChild->id
;
2458 $relParams['is_permission_a_b'] = CRM_Contact_BAO_Relationship
::EDIT
;
2459 $this->callAPISuccess('relationship', 'create', $relParams);
2461 // Mom and Black Sheep get their own memberships of the second type.
2462 $membershipParams['membership_type_id'] = $membershipType2->id
;
2463 $membershipParams['owner_membership_id'] = 'NULL';
2464 $membershipParams['contact_id'] = $mainMembership->contact_id
;
2465 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2467 $membershipParams['contact_id'] = $nonPermChild->id
;
2468 $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
2470 // Test a reminder for the first membership type - that should exclude Black
2472 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
2473 $actionSchedule['entity_value'] = $membershipType1->id
;
2474 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2475 $this->assertTrue(is_numeric($actionScheduleDao->id
));
2477 $this->assertCronRuns([
2479 'time' => '2012-03-29 01:00:00',
2480 'recipients' => [['test-member@example.com'], ['favorite@example.com']],
2482 'subject sched_membership_join_2week (joined March 15th, 2012)',
2483 'subject sched_membership_join_2week (joined March 15th, 2012)',
2488 // Test a reminder for the second membership type - that should include
2490 $actionSchedule = $this->fixtures
['sched_membership_start_1week'];
2491 $actionSchedule['entity_value'] = $membershipType2->id
;
2492 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2493 $this->assertInternalType('numeric', $actionScheduleDao->id
);
2495 $this->assertCronRuns([
2497 'time' => '2012-03-22 01:00:00',
2498 'recipients' => [['test-member@example.com'], ['black.sheep@example.com']],
2500 'subject sched_membership_start_1week (joined March 15th, 2012)',
2501 'subject sched_membership_start_1week (joined March 15th, 2012)',
2508 * Modify the date time by the modify rule.
2510 * @param DateTime $origDateTime
2511 * @param string $modifyRule
2515 public function createModifiedDateTime($origDateTime, $modifyRule) {
2516 $newDateTime = clone($origDateTime);
2517 $newDateTime->modify($modifyRule);
2518 return $newDateTime;
2522 * Test absolute date handling for membership.
2524 * @throws \CRM_Core_Exception
2526 public function testMembershipScheduleWithAbsoluteDate() {
2527 $membership = $this->createMembershipFromFixture('rolling_membership', 'New', [
2528 'email' => 'test-member@example.com',
2529 'location_type_id' => 1,
2532 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], ['contact_id' => $membership->contact_id
]));
2533 $actionSchedule = $this->fixtures
['sched_membership_absolute_date'];
2534 $actionSchedule['entity_value'] = $membership->membership_type_id
;
2535 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
2536 $this->assertInternalType('numeric', $actionScheduleDao->id
);
2538 $this->assertCronRuns([
2540 // Before the 24-hour mark, no email
2541 'time' => '2012-06-13 04:00:00',
2546 // On absolute date set on 2012-06-14
2547 'time' => '2012-06-14 00:00:00',
2548 'recipients' => [['test-member@example.com']],
2549 'subjects' => ['subject sched_membership_absolute_date'],
2552 // Run cron 4 hours later; first message already sent
2553 'time' => '2012-06-14 04:00:00',
2561 * @param string $fixture
2562 * Key from $this->fixtures
2563 * @param string $status
2565 * @param array $emailParams
2567 * @return \CRM_Member_DAO_Membership
2568 * @throws \API_Exception
2569 * @throws \Civi\API\Exception\UnauthorizedException
2571 protected function createMembershipFromFixture($fixture, $status, $emailParams = []) {
2572 /* @var CRM_Member_DAO_Membership $membership */
2573 $membership = $this->createTestObject(
2574 'CRM_Member_DAO_Membership',
2575 array_merge($this->fixtures
[$fixture], ['status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', $status)])
2577 $this->assertInternalType('numeric', $membership->id
);
2579 Civi\Api4\Email
::create()->setCheckPermissions(FALSE)->setValues(array_merge([
2580 'contact_id' => $membership->contact_id
,
2581 'location_type_id' => 1,
2582 ], $emailParams))->execute();
2588 * Create action schedule from defined fixtures.
2590 * @param string $fixture
2592 protected function createScheduleFromFixtures($fixture) {
2593 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
[$fixture]);
2594 $this->assertInternalType('numeric', $actionScheduleDao->id
);