3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class CRM_Core_BAO_ActionScheduleTest
34 class CRM_Core_BAO_ActionScheduleTest
extends CiviUnitTestCase
{
37 * @var object see CiviTest/CiviMailUtils
41 public function setUp() {
44 require_once 'CiviTest/CiviMailUtils.php';
45 $this->mut
= new CiviMailUtils($this, TRUE);
47 $this->fixtures
['rolling_membership'] = array(
48 'membership_type_id' => array(
49 'period_type' => 'rolling',
50 'duration_unit' => 'month',
51 'duration_interval' => '3',
54 'join_date' => '20120315',
55 'start_date' => '20120315',
56 'end_date' => '20120615',
60 $this->fixtures
['rolling_membership_past'] = array(
61 'membership_type_id' => array(
62 'period_type' => 'rolling',
63 'duration_unit' => 'month',
64 'duration_interval' => '3',
67 'join_date' => '20100310',
68 'start_date' => '20100310',
69 'end_date' => '20100610',
70 'is_override' => 'NULL',
73 $this->fixtures
['phonecall'] = array(
75 'activity_type_id' => 2,
76 'activity_date_time' => '20120615100000',
77 'is_current_revision' => 1,
80 $this->fixtures
['contact'] = array(
82 'contact_type' => 'Individual',
83 'email' => 'test-member@example.com',
85 $this->fixtures
['contact_birthdate'] = array(
87 'contact_type' => 'Individual',
88 'email' => 'test-bday@example.com',
89 'birth_date' => '20050707',
91 $this->fixtures
['sched_activity_1day'] = array(
92 'name' => 'One_Day_Phone_Call_Notice',
93 'title' => 'One Day Phone Call Notice',
95 'absolute_date' => NULL,
96 'body_html' => '<p>1-Day (non-repeating)</p>',
97 'body_text' => '1-Day (non-repeating)',
100 'end_frequency_interval' => NULL,
101 'end_frequency_unit' => NULL,
102 'entity_status' => '1',
103 'entity_value' => '2',
108 'msg_template_id' => NULL,
110 'recipient_listing' => NULL,
111 'recipient_manual' => NULL,
112 'record_activity' => NULL,
113 'repetition_frequency_interval' => NULL,
114 'repetition_frequency_unit' => NULL,
115 'start_action_condition' => 'before',
116 'start_action_date' => 'activity_date_time',
117 'start_action_offset' => '1',
118 'start_action_unit' => 'day',
119 'subject' => '1-Day (non-repeating)',
121 $this->fixtures
['sched_activity_1day_r'] = array(
122 'name' => 'One_Day_Phone_Call_Notice_R',
123 'title' => 'One Day Phone Call Notice R',
125 'absolute_date' => NULL,
126 'body_html' => '<p>1-Day (repeating)</p>',
127 'body_text' => '1-Day (repeating)',
128 'end_action' => 'after',
129 'end_date' => 'activity_date_time',
130 'end_frequency_interval' => '2',
131 'end_frequency_unit' => 'day',
132 'entity_status' => '1',
133 'entity_value' => '2',
138 'msg_template_id' => NULL,
140 'recipient_listing' => NULL,
141 'recipient_manual' => NULL,
142 'record_activity' => NULL,
143 'repetition_frequency_interval' => '6',
144 'repetition_frequency_unit' => 'hour',
145 'start_action_condition' => 'before',
146 'start_action_date' => 'activity_date_time',
147 'start_action_offset' => '1',
148 'start_action_unit' => 'day',
149 'subject' => '1-Day (repeating)',
151 $this->fixtures
['sched_membership_join_2week'] = array(
152 'name' => 'sched_membership_join_2week',
153 'title' => 'sched_membership_join_2week',
154 'absolute_date' => '',
155 'body_html' => '<p>body sched_membership_join_2week</p>',
156 'body_text' => 'body sched_membership_join_2week',
159 'end_frequency_interval' => '',
160 'end_frequency_unit' => '',
161 'entity_status' => '',
162 'entity_value' => '',
167 'msg_template_id' => '',
169 'recipient_listing' => '',
170 'recipient_manual' => '',
171 'record_activity' => 1,
172 'repetition_frequency_interval' => '',
173 'repetition_frequency_unit' => '',
174 'start_action_condition' => 'after',
175 'start_action_date' => 'membership_join_date',
176 'start_action_offset' => '2',
177 'start_action_unit' => 'week',
178 'subject' => 'subject sched_membership_join_2week',
180 $this->fixtures
['sched_membership_end_2week'] = array(
181 'name' => 'sched_membership_end_2week',
182 'title' => 'sched_membership_end_2week',
183 'absolute_date' => '',
184 'body_html' => '<p>body sched_membership_end_2week</p>',
185 'body_text' => 'body sched_membership_end_2week',
188 'end_frequency_interval' => '',
189 'end_frequency_unit' => '',
190 'entity_status' => '',
191 'entity_value' => '',
196 'msg_template_id' => '',
198 'recipient_listing' => '',
199 'recipient_manual' => '',
200 'record_activity' => 1,
201 'repetition_frequency_interval' => '',
202 'repetition_frequency_unit' => '',
203 'start_action_condition' => 'before',
204 'start_action_date' => 'membership_end_date',
205 'start_action_offset' => '2',
206 'start_action_unit' => 'week',
207 'subject' => 'subject sched_membership_end_2week',
209 $this->fixtures
['sched_on_membership_end_date'] = array(
210 'name' => 'sched_on_membership_end_date',
211 'title' => 'sched_on_membership_end_date',
212 'body_html' => '<p>Your membership expired today</p>',
213 'body_text' => 'Your membership expired today',
216 'record_activity' => 1,
217 'start_action_condition' => 'after',
218 'start_action_date' => 'membership_end_date',
219 'start_action_offset' => '0',
220 'start_action_unit' => 'hour',
221 'subject' => 'subject send reminder on membership_end_date',
223 $this->fixtures
['sched_after_1day_membership_end_date'] = array(
224 'name' => 'sched_after_1day_membership_end_date',
225 'title' => 'sched_after_1day_membership_end_date',
226 'body_html' => '<p>Your membership expired yesterday</p>',
227 'body_text' => 'Your membership expired yesterday',
230 'record_activity' => 1,
231 'start_action_condition' => 'after',
232 'start_action_date' => 'membership_end_date',
233 'start_action_offset' => '1',
234 'start_action_unit' => 'day',
235 'subject' => 'subject send reminder on membership_end_date',
238 $this->fixtures
['sched_membership_end_2month'] = array(
239 'name' => 'sched_membership_end_2month',
240 'title' => 'sched_membership_end_2month',
241 'absolute_date' => '',
242 'body_html' => '<p>body sched_membership_end_2month</p>',
243 'body_text' => 'body sched_membership_end_2month',
246 'end_frequency_interval' => '',
247 'end_frequency_unit' => '',
248 'entity_status' => '',
249 'entity_value' => '',
254 'msg_template_id' => '',
256 'recipient_listing' => '',
257 'recipient_manual' => '',
258 'record_activity' => 1,
259 'repetition_frequency_interval' => '',
260 'repetition_frequency_unit' => '',
261 'start_action_condition' => 'after',
262 'start_action_date' => 'membership_end_date',
263 'start_action_offset' => '2',
264 'start_action_unit' => 'month',
265 'subject' => 'subject sched_membership_end_2month',
268 $this->fixtures
['sched_contact_bday_yesterday'] = array(
269 'name' => 'sched_contact_bday_yesterday',
270 'title' => 'sched_contact_bday_yesterday',
271 'absolute_date' => '',
272 'body_html' => '<p>you look like you were born yesterday!</p>',
273 'body_text' => 'you look like you were born yesterday!',
276 'end_frequency_interval' => '',
277 'end_frequency_unit' => '',
278 'entity_status' => 1,
279 'entity_value' => 'birth_date',
284 'msg_template_id' => '',
286 'recipient_listing' => '',
287 'recipient_manual' => '',
288 'record_activity' => 1,
289 'repetition_frequency_interval' => '',
290 'repetition_frequency_unit' => '',
291 'start_action_condition' => 'after',
292 'start_action_date' => 'date_field',
293 'start_action_offset' => '1',
294 'start_action_unit' => 'day',
295 'subject' => 'subject sched_contact_bday_yesterday',
298 $this->fixtures
['sched_contact_bday_anniv'] = array(
299 'name' => 'sched_contact_bday_anniv',
300 'title' => 'sched_contact_bday_anniv',
301 'absolute_date' => '',
302 'body_html' => '<p>happy birthday!</p>',
303 'body_text' => 'happy birthday!',
306 'end_frequency_interval' => '',
307 'end_frequency_unit' => '',
308 'entity_status' => 2,
309 'entity_value' => 'birth_date',
314 'msg_template_id' => '',
316 'recipient_listing' => '',
317 'recipient_manual' => '',
318 'record_activity' => 1,
319 'repetition_frequency_interval' => '',
320 'repetition_frequency_unit' => '',
321 'start_action_condition' => 'before',
322 'start_action_date' => 'date_field',
323 'start_action_offset' => '1',
324 'start_action_unit' => 'day',
325 'subject' => 'subject sched_contact_bday_anniv',
328 $this->fixtures
['sched_contact_grad_tomorrow'] = array(
329 'name' => 'sched_contact_grad_tomorrow',
330 'title' => 'sched_contact_grad_tomorrow',
331 'absolute_date' => '',
332 'body_html' => '<p>congratulations on your graduation!</p>',
333 'body_text' => 'congratulations on your graduation!',
336 'end_frequency_interval' => '',
337 'end_frequency_unit' => '',
338 'entity_status' => 1,
343 'msg_template_id' => '',
345 'recipient_listing' => '',
346 'recipient_manual' => '',
347 'record_activity' => 1,
348 'repetition_frequency_interval' => '',
349 'repetition_frequency_unit' => '',
350 'start_action_condition' => 'before',
351 'start_action_date' => 'date_field',
352 'start_action_offset' => '1',
353 'start_action_unit' => 'day',
354 'subject' => 'subject sched_contact_grad_tomorrow',
357 $this->fixtures
['sched_contact_grad_anniv'] = array(
358 'name' => 'sched_contact_grad_anniv',
359 'title' => 'sched_contact_grad_anniv',
360 'absolute_date' => '',
361 'body_html' => '<p>dear alum, please send us money.</p>',
362 'body_text' => 'dear alum, please send us money.',
365 'end_frequency_interval' => '',
366 'end_frequency_unit' => '',
367 'entity_status' => 2,
372 'msg_template_id' => '',
374 'recipient_listing' => '',
375 'recipient_manual' => '',
376 'record_activity' => 1,
377 'repetition_frequency_interval' => '',
378 'repetition_frequency_unit' => '',
379 'start_action_condition' => 'after',
380 'start_action_date' => 'date_field',
381 'start_action_offset' => '1',
382 'start_action_unit' => 'week',
383 'subject' => 'subject sched_contact_grad_anniv',
386 $this->fixtures
['sched_contact_created_yesterday'] = array(
387 'name' => 'sched_contact_created_yesterday',
388 'title' => 'sched_contact_created_yesterday',
389 'absolute_date' => '',
390 'body_html' => '<p>Your contact was created yesterday</p>',
391 'body_text' => 'Your contact was created yesterday!',
394 'end_frequency_interval' => '',
395 'end_frequency_unit' => '',
396 'entity_status' => 1,
397 'entity_value' => 'created_date',
402 'msg_template_id' => '',
404 'recipient_listing' => '',
405 'recipient_manual' => '',
406 'record_activity' => 1,
407 'repetition_frequency_interval' => '',
408 'repetition_frequency_unit' => '',
409 'start_action_condition' => 'after',
410 'start_action_date' => 'date_field',
411 'start_action_offset' => '1',
412 'start_action_unit' => 'day',
413 'subject' => 'subject sched_contact_created_yesterday',
416 $this->fixtures
['sched_contact_mod_anniv'] = array(
417 'name' => 'sched_contact_mod_anniv',
418 'title' => 'sched_contact_mod_anniv',
419 'absolute_date' => '',
420 'body_html' => '<p>You last updated your data last year</p>',
421 'body_text' => 'Go update your stuff!',
424 'end_frequency_interval' => '',
425 'end_frequency_unit' => '',
426 'entity_status' => 2,
427 'entity_value' => 'modified_date',
432 'msg_template_id' => '',
434 'recipient_listing' => '',
435 'recipient_manual' => '',
436 'record_activity' => 1,
437 'repetition_frequency_interval' => '',
438 'repetition_frequency_unit' => '',
439 'start_action_condition' => 'before',
440 'start_action_date' => 'date_field',
441 'start_action_offset' => '1',
442 'start_action_unit' => 'day',
443 'subject' => 'subject sched_contact_mod_anniv',
446 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = array(
447 'name' => 'sched_membership_end_2month',
448 'title' => 'sched_membership_end_2month',
449 'absolute_date' => '',
450 'body_html' => '<p>body sched_membership_end_2month</p>',
451 'body_text' => 'body sched_membership_end_2month',
453 'end_date' => 'membership_end_date',
454 'end_frequency_interval' => '4',
455 'end_frequency_unit' => 'month',
456 'entity_status' => '',
457 'entity_value' => '',
462 'msg_template_id' => '',
464 'recipient_listing' => '',
465 'recipient_manual' => '',
466 'record_activity' => 1,
467 'repetition_frequency_interval' => '4',
468 'repetition_frequency_unit' => 'week',
469 'start_action_condition' => 'after',
470 'start_action_date' => 'membership_end_date',
471 'start_action_offset' => '2',
472 'start_action_unit' => 'month',
473 'subject' => 'subject sched_membership_end_2month',
475 $this->fixtures
['sched_membership_end_limit_to_none'] = array(
476 'name' => 'limit to none',
477 'title' => 'limit to none',
478 'absolute_date' => '',
479 'body_html' => '<p>body sched_membership_end_2month</p>',
480 'body_text' => 'body sched_membership_end_2month',
483 'end_frequency_interval' => '4',
484 'end_frequency_unit' => 'month',
485 'entity_status' => '',
486 'entity_value' => '',
492 'msg_template_id' => '',
494 'recipient_listing' => '',
495 'recipient_manual' => '',
496 'record_activity' => 1,
497 'repetition_frequency_interval' => '4',
498 'repetition_frequency_unit' => 'week',
499 'start_action_condition' => 'after',
500 'start_action_date' => 'membership_end_date',
501 'start_action_offset' => '2',
502 'start_action_unit' => 'month',
503 'subject' => 'limit to none',
509 * Tears down the fixture, for example, closes a network connection.
511 * This method is called after a test is executed.
513 public function tearDown() {
516 $this->mut
->clearMessages();
519 $this->quickCleanup(array(
520 'civicrm_action_schedule',
521 'civicrm_action_log',
522 'civicrm_membership',
528 public function testActivityDateTimeMatchNonRepeatableSchedule() {
529 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day']);
530 $this->assertTrue(is_numeric($actionScheduleDao->id
));
532 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
533 $this->assertTrue(is_numeric($activity->id
));
534 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
537 $source['contact_id'] = $contact['id'];
538 $source['activity_id'] = $activity->id
;
539 $source['record_type_id'] = 2;
540 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
541 $activityContact->save();
543 $this->assertCronRuns(array(
545 // Before the 24-hour mark, no email
546 'time' => '2012-06-14 04:00:00',
547 'recipients' => array(),
550 // After the 24-hour mark, an email
551 'time' => '2012-06-14 15:00:00',
552 'recipients' => array(array('test-member@example.com')),
555 // Run cron again; message already sent
557 'recipients' => array(),
562 public function testActivityDateTimeMatchRepeatableSchedule() {
563 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day_r']);
564 $this->assertTrue(is_numeric($actionScheduleDao->id
));
566 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
567 $this->assertTrue(is_numeric($activity->id
));
568 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
571 $source['contact_id'] = $contact['id'];
572 $source['activity_id'] = $activity->id
;
573 $source['record_type_id'] = 2;
574 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
575 $activityContact->save();
577 $this->assertCronRuns(array(
579 // Before the 24-hour mark, no email
580 'time' => '012-06-14 04:00:00',
581 'recipients' => array(),
584 // After the 24-hour mark, an email
585 'time' => '2012-06-14 15:00:00',
586 'recipients' => array(array('test-member@example.com')),
589 // Run cron 4 hours later; first message already sent
590 'time' => '2012-06-14 20:00:00',
591 'recipients' => array(),
594 // Run cron 6 hours later; send second message.
595 'time' => '2012-06-14 21:00:01',
596 'recipients' => array(array('test-member@example.com')),
602 * For contacts/activities which don't match the schedule filter,
603 * an email should *not* be sent.
605 // TODO // function testActivityDateTime_NonMatch() { }
608 * For contacts/members which match schedule based on join date,
609 * an email should be sent.
611 public function testMembershipJoinDateMatch() {
612 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 1)));
613 $this->assertTrue(is_numeric($membership->id
));
614 $result = $this->callAPISuccess('Email', 'create', array(
615 'contact_id' => $membership->contact_id
,
616 'email' => 'test-member@example.com',
617 'location_type_id' => 1,
619 $this->assertAPISuccess($result);
621 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
622 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
623 $actionSchedule['entity_value'] = $membership->membership_type_id
;
624 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
625 $this->assertTrue(is_numeric($actionScheduleDao->id
));
627 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
628 $this->assertCronRuns(array(
630 // Before the 2-week mark, no email.
631 'time' => '2012-03-28 01:00:00',
632 'recipients' => array(),
635 // After the 2-week mark, send an email.
636 'time' => '2012-03-29 01:00:00',
637 'recipients' => array(array('test-member@example.com')),
643 * Test end date email sent.
645 * For contacts/members which match schedule based on join date,
646 * an email should be sent.
648 public function testMembershipJoinDateNonMatch() {
649 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures
['rolling_membership']);
650 $this->assertTrue(is_numeric($membership->id
));
651 $result = $this->callAPISuccess('Email', 'create', array(
652 'contact_id' => $membership->contact_id
,
653 'location_type_id' => 1,
654 'email' => 'test-member@example.com',
657 // Add an alternative membership type, and only send messages for that type
658 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
659 $this->assertTrue(is_numeric($extraMembershipType->id
));
660 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_membership_join_2week']);
661 $this->assertTrue(is_numeric($actionScheduleDao->id
));
662 $actionScheduleDao->entity_value
= $extraMembershipType->id
;
663 $actionScheduleDao->save();
665 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
666 $this->assertCronRuns(array(
668 // After the 2-week mark, don't send email because we have different membership type.
669 'time' => '2012-03-29 01:00:00',
670 'recipients' => array(),
676 * Test that the first and SECOND notifications are sent out.
678 public function testMembershipEndDateRepeat() {
679 // creates membership with end_date = 20120615
680 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
681 $result = $this->callAPISuccess('Email', 'create', array(
682 'contact_id' => $membership->contact_id
,
683 'email' => 'test-member@example.com',
685 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
687 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
688 $actionSchedule['entity_value'] = $membership->membership_type_id
;
689 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
691 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
692 $this->assertCronRuns(array(
694 // After the 2-week mark, send an email.
695 'time' => '2012-08-15 01:00:00',
696 'recipients' => array(array('test-member@example.com')),
699 // After the 2-week mark, send an email.
700 'time' => '2012-09-12 01:00:00',
701 'recipients' => array(array('test-member@example.com')),
707 * Test behaviour when date changes.
709 * Test that the first notification is sent but the second is NOT sent if the end date changes in
713 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
714 // creates membership with end_date = 20120615
715 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
716 $this->callAPISuccess('Email', 'create', array(
717 'contact_id' => $membership->contact_id
,
718 'email' => 'test-member@example.com',
720 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
722 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
723 $actionSchedule['entity_value'] = $membership->membership_type_id
;
724 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
725 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
726 $this->assertCronRuns(array(
728 // After the 2-week mark, send an email.
729 'time' => '2012-08-15 01:00:00',
730 'recipients' => array(array('test-member@example.com')),
734 // Extend membership - reminder should NOT go out.
735 $this->callAPISuccess('membership', 'create', array('id' => $membership->id
, 'end_date' => '2014-01-01'));
736 $this->assertCronRuns(array(
738 // After the 2-week mark, send an email.
739 'time' => '2012-09-12 01:00:00',
740 'recipients' => array(),
746 * Test membership end date email sends.
748 * For contacts/members which match schedule based on end date,
749 * an email should be sent.
751 public function testMembershipEndDateMatch() {
752 // creates membership with end_date = 20120615
753 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
754 $this->assertTrue(is_numeric($membership->id
));
755 $this->callAPISuccess('Email', 'create', array(
756 'contact_id' => $membership->contact_id
,
757 'email' => 'test-member@example.com',
759 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
761 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
762 $actionSchedule['entity_value'] = $membership->membership_type_id
;
763 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
764 $this->assertTrue(is_numeric($actionScheduleDao->id
));
766 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
767 $this->assertCronRuns(array(
769 // Before the 2-week mark, no email.
770 'time' => '2012-05-31 01:00:00',
771 // 'time' => '2012-06-01 01:00:00',
772 // FIXME: Is this the right boundary?
773 'recipients' => array(),
776 // After the 2-week mark, send an email.
777 'time' => '2012-06-01 01:00:00',
778 'recipients' => array(array('test-member@example.com')),
782 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
783 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
784 $membership->end_date
= '2012-09-15';
787 //change the email id of chosen membership contact to assert
788 //recipient of not the previously sent mail but the new one
789 $result = $this->callAPISuccess('Email', 'create', array(
791 'contact_id' => $membership->contact_id
,
792 'email' => 'member2@example.com',
794 $this->assertAPISuccess($result);
796 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
797 $this->assertCronRuns(array(
799 // Before the 2-week mark, no email
800 'time' => '2012-08-31 01:00:00',
801 'recipients' => array(),
803 //array( // After the 2-week mark, send an email
804 //'time' => '2012-09-01 01:00:00',
805 //'recipients' => array(array('member2@example.com')),
812 * Test membership end date email.
814 * For contacts/members which match schedule based on end date,
815 * an email should be sent.
817 public function testMembershipEndDateNoMatch() {
818 // creates membership with end_date = 20120615
819 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 3)));
820 $this->assertTrue(is_numeric($membership->id
));
821 $result = $this->callAPISuccess('Email', 'create', array(
822 'contact_id' => $membership->contact_id
,
823 'email' => 'test-member@example.com',
825 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
827 $actionSchedule = $this->fixtures
['sched_membership_end_2month'];
828 $actionSchedule['entity_value'] = $membership->membership_type_id
;
829 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
830 $this->assertTrue(is_numeric($actionScheduleDao->id
));
832 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
833 $this->assertCronRuns(array(
835 // Before the 2-week mark, no email.
836 'time' => '2012-05-31 01:00:00',
837 // 'time' => '2012-06-01 01:00:00',
838 // FIXME: Is this the right boundary?
839 'recipients' => array(),
842 // After the 2-week mark, send an email.
843 'time' => '2013-05-01 01:00:00',
844 'recipients' => array(),
849 public function testContactBirthDateNoAnniv() {
850 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
851 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
852 $actionSchedule = $this->fixtures
['sched_contact_bday_yesterday'];
853 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
854 $this->assertTrue(is_numeric($actionScheduleDao->id
));
855 $this->assertCronRuns(array(
857 // On the birthday, no email.
858 'time' => '2005-07-07 01:00:00',
859 'recipients' => array(),
862 // The next day, send an email.
863 'time' => '2005-07-08 20:00:00',
864 'recipients' => array(array('test-bday@example.com')),
869 public function testContactBirthDateAnniversary() {
870 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
871 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
872 $actionSchedule = $this->fixtures
['sched_contact_bday_anniv'];
873 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
874 $this->assertTrue(is_numeric($actionScheduleDao->id
));
875 $this->assertCronRuns(array(
877 // On some random day, no email.
878 'time' => '2014-03-07 01:00:00',
879 'recipients' => array(),
882 // On the eve of their 9th birthday, send an email.
883 'time' => '2014-07-06 20:00:00',
884 'recipients' => array(array('test-bday@example.com')),
889 public function testContactCustomDateNoAnniv() {
891 'title' => 'Test_Group',
892 'name' => 'test_group',
893 'extends' => array('Individual'),
895 'is_multiple' => FALSE,
898 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
900 'label' => 'Graduation',
901 'data_type' => 'Date',
902 'html_type' => 'Select Date',
903 'custom_group_id' => $createGroup['id'],
905 $createField = $this->callAPISuccess('custom_field', 'create', $field);
906 $contactParams = $this->fixtures
['contact'];
907 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
908 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
909 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
910 $actionSchedule = $this->fixtures
['sched_contact_grad_tomorrow'];
911 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
912 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
913 $this->assertTrue(is_numeric($actionScheduleDao->id
));
914 $this->assertCronRuns(array(
916 // On some random day, no email.
917 'time' => '2014-03-07 01:00:00',
918 'recipients' => array(),
921 // On the eve of their graduation, send an email.
922 'time' => '2013-12-15 20:00:00',
923 'recipients' => array(array('test-member@example.com')),
926 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
929 public function testContactCreatedNoAnniv() {
930 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
931 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
932 $actionSchedule = $this->fixtures
['sched_contact_created_yesterday'];
933 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
934 $this->assertTrue(is_numeric($actionScheduleDao->id
));
935 $this->assertCronRuns(array(
937 // On the date created, no email.
938 'time' => $contact['values'][$contact['id']]['created_date'],
939 'recipients' => array(),
942 // The next day, send an email.
943 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
944 'recipients' => array(array('test-bday@example.com')),
949 public function testContactModifiedAnniversary() {
950 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
951 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
952 $actionSchedule = $this->fixtures
['sched_contact_mod_anniv'];
953 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
954 $this->assertTrue(is_numeric($actionScheduleDao->id
));
955 $this->assertCronRuns(array(
957 // On some random day, no email.
958 'time' => '2014-03-07 01:00:00',
959 'recipients' => array(),
962 // On the eve of 3 years after they were modified, send an email.
963 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' +3 years -1 day')),
964 'recipients' => array(array('test-bday@example.com')),
970 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
972 public function testMembershipLimitToNone() {
973 // creates membership with end_date = 20120615
974 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
976 $this->assertTrue(is_numeric($membership->id
));
977 $result = $this->callAPISuccess('Email', 'create', array(
978 'contact_id' => $membership->contact_id
,
979 'email' => 'member@example.com',
981 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
982 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
984 $this->assertAPISuccess($result);
986 $actionSchedule = $this->fixtures
['sched_membership_end_limit_to_none'];
987 $actionSchedule['entity_value'] = $membership->membership_type_id
;
988 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
989 $this->assertTrue(is_numeric($actionScheduleDao->id
));
991 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
992 $this->assertCronRuns(array(
994 // Before the 2-week mark, no email.
995 'time' => '2012-05-31 01:00:00',
996 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
997 'recipients' => array(),
1002 public function testMembership_referenceDate() {
1003 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
1005 $this->assertTrue(is_numeric($membership->id
));
1006 $this->callAPISuccess('Email', 'create', array(
1007 'contact_id' => $membership->contact_id
,
1008 'email' => 'member@example.com',
1011 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1012 $this->assertAPISuccess($result);
1014 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1015 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1016 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1017 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1019 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1020 $this->assertCronRuns(array(
1022 // After the 2-week mark, send an email
1023 'time' => '2012-03-29 01:00:00',
1024 'recipients' => array(array('member@example.com')),
1027 // After the 2-week 1day mark, don't send an email
1028 'time' => '2012-03-30 01:00:00',
1029 'recipients' => array(),
1033 //check if reference date is set to membership's join date
1034 //as per the action_start_date chosen for current schedule reminder
1035 $this->assertEquals('2012-03-15',
1036 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id
, 'reference_date', 'contact_id')
1039 //change current membership join date that may signifies as memberhip renewal activity
1040 $membership->join_date
= '2012-03-29';
1041 $membership->save();
1043 $this->assertCronRuns(array(
1045 // After the 13 days of the changed join date 2012-03-29, don't send an email
1046 'time' => '2012-04-11 01:00:00',
1047 'recipients' => array(),
1050 // After the 2-week of the changed join date 2012-03-29, send an email
1051 'time' => '2012-04-12 01:00:00',
1052 'recipients' => array(array('member@example.com')),
1055 $this->assertCronRuns(array(
1057 // It should not re-send on the same day
1058 'time' => '2012-04-12 01:00:00',
1059 'recipients' => array(),
1064 public function testMembershipOnMultipleReminder() {
1065 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
1067 $this->assertTrue(is_numeric($membership->id
));
1068 $result = $this->callAPISuccess('Email', 'create', array(
1069 'contact_id' => $membership->contact_id
,
1070 'email' => 'member@example.com',
1072 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1073 $this->assertAPISuccess($result);
1075 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week']; // Send email 2 weeks before end_date
1076 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date']; // Send email on end_date/expiry date
1077 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
1078 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id
;
1079 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
1080 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
1081 $this->assertTrue(is_numeric($
$value->id
));
1084 $this->assertCronRuns(
1087 // 1day 2weeks before membership end date(MED), don't send mail
1088 'time' => '2012-05-31 01:00:00',
1089 'recipients' => array(),
1092 // 2 weeks before MED, send an email
1093 'time' => '2012-06-01 01:00:00',
1094 'recipients' => array(array('member@example.com')),
1097 // 1day before MED, don't send mail
1098 'time' => '2012-06-14 01:00:00',
1099 'recipients' => array(),
1102 // On MED, send an email
1103 'time' => '2012-06-15 00:00:00',
1104 'recipients' => array(array('member@example.com')),
1107 // After 1day of MED, send an email
1108 'time' => '2012-06-16 01:00:00',
1109 'recipients' => array(array('member@example.com')),
1112 // After 1day 1min of MED, don't send an email
1113 'time' => '2012-06-17 00:01:00',
1114 'recipients' => array(),
1119 // Assert the timestamp as of when the emails of respective three reminders as configured
1120 // 2 weeks before, on and 1 day after MED, are sent
1121 $this->assertEquals('2012-06-01 01:00:00',
1122 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE));
1123 $this->assertEquals('2012-06-15 00:00:00',
1124 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE));
1125 $this->assertEquals('2012-06-16 01:00:00',
1126 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE));
1128 //extend MED to 2 weeks after the current MED (that may signifies as memberhip renewal activity)
1129 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1130 $membership->end_date
= '2012-06-20';
1131 $membership->save();
1133 $result = $this->callAPISuccess('Contact', 'get', array('id' => $membership->contact_id
));
1134 $this->assertCronRuns(
1137 // 1day 2weeks before membership end date(MED), don't send mail
1138 'time' => '2012-06-05 01:00:00',
1139 'recipients' => array(),
1142 // 2 weeks before MED, send an email
1143 'time' => '2012-06-06 01:00:00',
1144 'recipients' => array(array('member@example.com')),
1147 // 1day before MED, don't send mail
1148 'time' => '2012-06-19 01:00:00',
1149 'recipients' => array(),
1152 // On MED, send an email
1153 'time' => '2012-06-20 00:00:00',
1154 'recipients' => array(array('member@example.com')),
1157 // After 1day of MED, send an email
1158 'time' => '2012-06-21 01:00:00',
1159 'recipients' => array(array('member@example.com')),
1162 // After 1day 1min of MED, don't send an email
1163 'time' => '2012-07-21 00:01:00',
1164 'recipients' => array(),
1169 public function testContactCustomDate_Anniv() {
1171 'title' => 'Test_Group now',
1172 'name' => 'test_group_now',
1173 'extends' => array('Individual'),
1174 'style' => 'Inline',
1175 'is_multiple' => FALSE,
1178 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1180 'label' => 'Graduation',
1181 'data_type' => 'Date',
1182 'html_type' => 'Select Date',
1183 'custom_group_id' => $createGroup['id'],
1185 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1187 $contactParams = $this->fixtures
['contact'];
1188 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1189 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1190 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1191 $actionSchedule = $this->fixtures
['sched_contact_grad_anniv'];
1192 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1193 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1194 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1195 $this->assertCronRuns(array(
1197 // On some random day, no email.
1198 'time' => '2014-03-07 01:00:00',
1199 'recipients' => array(),
1202 // A week after their 5th anniversary of graduation, send an email.
1203 'time' => '2018-12-23 20:00:00',
1204 'recipients' => array(array('test-member@example.com')),
1207 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1210 // TODO // function testMembershipEndDate_NonMatch() { }
1211 // TODO // function testEventTypeStartDate_Match() { }
1212 // TODO // function testEventTypeEndDate_Match() { }
1213 // TODO // function testEventNameStartDate_Match() { }
1214 // TODO // function testEventNameEndDate_Match() { }
1217 * Run a series of cron jobs and make an assertion about email deliveries.
1219 * @param array $cronRuns
1220 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1221 * - time: string, e.g. '2012-06-15 21:00:01'
1222 * - recipients: array(array(string)), list of email addresses which should receive messages
1224 public function assertCronRuns($cronRuns) {
1225 foreach ($cronRuns as $cronRun) {
1226 CRM_Utils_Time
::setTime($cronRun['time']);
1227 $this->callAPISuccess('job', 'send_reminder', array());
1228 $this->mut
->assertRecipients($cronRun['recipients']);
1229 $this->mut
->clearMessages();
1234 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1236 private $_testObjects;
1239 * Sets up the fixture, for example, opens a network connection.
1241 * This method is called before a test is executed.
1243 protected function _setUp() {
1244 $this->_testObjects
= array();
1248 * Tears down the fixture, for example, closes a network connection.
1250 * This method is called after a test is executed.
1252 protected function _tearDown() {
1254 $this->deleteTestObjects();
1258 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
1259 * created entities and provides for brainless cleanup.
1261 * @see CRM_Core_DAO::createTestObject
1264 * @param array $params
1265 * @param int $numObjects
1266 * @param bool $createOnly
1268 * @return array|NULL|object
1270 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
1271 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
1272 if (is_array($objects)) {
1273 $this->registerTestObjects($objects);
1276 $this->registerTestObjects(array($objects));
1282 * @param array $objects
1283 * DAO or BAO objects.
1285 public function registerTestObjects($objects) {
1286 //if (is_object($objects)) {
1287 // $objects = array($objects);
1289 foreach ($objects as $object) {
1290 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1291 $this->_testObjects
[$daoName][] = $object->id
;
1295 public function deleteTestObjects() {
1296 // Note: You might argue that the FK relations between test
1297 // objects could make this problematic; however, it should
1298 // behave intuitively as long as we mentally split our
1299 // test-objects between the "manual/primary records"
1300 // and the "automatic/secondary records"
1301 foreach ($this->_testObjects
as $daoName => $daoIds) {
1302 foreach ($daoIds as $daoId) {
1303 CRM_Core_DAO
::deleteTestObjects($daoName, array('id' => $daoId));
1306 $this->_testObjects
= array();