3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
{
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',
72 $this->fixtures
['participant'] = array(
76 'title' => 'Example Event',
77 'start_date' => '20120315',
78 'end_date' => '20120615',
80 'role_id' => '1', // Attendee.
81 'status_id' => '8', // No-show.
84 $this->fixtures
['phonecall'] = array(
86 'activity_type_id' => 2,
87 'activity_date_time' => '20120615100000',
88 'is_current_revision' => 1,
91 $this->fixtures
['contact'] = array(
93 'contact_type' => 'Individual',
94 'email' => 'test-member@example.com',
95 'gender_id' => 'Female',
97 $this->fixtures
['contact_birthdate'] = array(
99 'contact_type' => 'Individual',
100 'email' => 'test-bday@example.com',
101 'birth_date' => '20050707',
103 $this->fixtures
['sched_activity_1day'] = array(
104 'name' => 'One_Day_Phone_Call_Notice',
105 'title' => 'One Day Phone Call Notice',
107 'absolute_date' => NULL,
108 'body_html' => '<p>1-Day (non-repeating)</p>',
109 'body_text' => '1-Day (non-repeating)',
110 'end_action' => NULL,
112 'end_frequency_interval' => NULL,
113 'end_frequency_unit' => NULL,
114 'entity_status' => '1',
115 'entity_value' => '2',
120 'msg_template_id' => NULL,
122 'recipient_listing' => NULL,
123 'recipient_manual' => NULL,
124 'record_activity' => NULL,
125 'repetition_frequency_interval' => NULL,
126 'repetition_frequency_unit' => NULL,
127 'start_action_condition' => 'before',
128 'start_action_date' => 'activity_date_time',
129 'start_action_offset' => '1',
130 'start_action_unit' => 'day',
131 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
133 $this->fixtures
['sched_activity_1day_r'] = array(
134 'name' => 'One_Day_Phone_Call_Notice_R',
135 'title' => 'One Day Phone Call Notice R',
137 'absolute_date' => NULL,
138 'body_html' => '<p>1-Day (repeating)</p>',
139 'body_text' => '1-Day (repeating)',
140 'end_action' => 'after',
141 'end_date' => 'activity_date_time',
142 'end_frequency_interval' => '2',
143 'end_frequency_unit' => 'day',
144 'entity_status' => '1',
145 'entity_value' => '2',
150 'msg_template_id' => NULL,
152 'recipient_listing' => NULL,
153 'recipient_manual' => NULL,
154 'record_activity' => NULL,
155 'repetition_frequency_interval' => '6',
156 'repetition_frequency_unit' => 'hour',
157 'start_action_condition' => 'before',
158 'start_action_date' => 'activity_date_time',
159 'start_action_offset' => '1',
160 'start_action_unit' => 'day',
161 'subject' => '1-Day (repeating) (about {activity.activity_type})',
163 $this->fixtures
['sched_membership_join_2week'] = array(
164 'name' => 'sched_membership_join_2week',
165 'title' => 'sched_membership_join_2week',
166 'absolute_date' => '',
167 'body_html' => '<p>body sched_membership_join_2week</p>',
168 'body_text' => 'body sched_membership_join_2week',
171 'end_frequency_interval' => '',
172 'end_frequency_unit' => '',
173 'entity_status' => '',
174 'entity_value' => '',
179 'msg_template_id' => '',
181 'recipient_listing' => '',
182 'recipient_manual' => '',
183 'record_activity' => 1,
184 'repetition_frequency_interval' => '',
185 'repetition_frequency_unit' => '',
186 'start_action_condition' => 'after',
187 'start_action_date' => 'membership_join_date',
188 'start_action_offset' => '2',
189 'start_action_unit' => 'week',
190 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
192 $this->fixtures
['sched_membership_end_2week'] = array(
193 'name' => 'sched_membership_end_2week',
194 'title' => 'sched_membership_end_2week',
195 'absolute_date' => '',
196 'body_html' => '<p>body sched_membership_end_2week</p>',
197 'body_text' => 'body sched_membership_end_2week',
200 'end_frequency_interval' => '',
201 'end_frequency_unit' => '',
202 'entity_status' => '',
203 'entity_value' => '',
208 'msg_template_id' => '',
210 'recipient_listing' => '',
211 'recipient_manual' => '',
212 'record_activity' => 1,
213 'repetition_frequency_interval' => '',
214 'repetition_frequency_unit' => '',
215 'start_action_condition' => 'before',
216 'start_action_date' => 'membership_end_date',
217 'start_action_offset' => '2',
218 'start_action_unit' => 'week',
219 'subject' => 'subject sched_membership_end_2week',
221 $this->fixtures
['sched_on_membership_end_date'] = array(
222 'name' => 'sched_on_membership_end_date',
223 'title' => 'sched_on_membership_end_date',
224 'body_html' => '<p>Your membership expired today</p>',
225 'body_text' => 'Your membership expired today',
228 'record_activity' => 1,
229 'start_action_condition' => 'after',
230 'start_action_date' => 'membership_end_date',
231 'start_action_offset' => '0',
232 'start_action_unit' => 'hour',
233 'subject' => 'subject send reminder on membership_end_date',
235 $this->fixtures
['sched_after_1day_membership_end_date'] = array(
236 'name' => 'sched_after_1day_membership_end_date',
237 'title' => 'sched_after_1day_membership_end_date',
238 'body_html' => '<p>Your membership expired yesterday</p>',
239 'body_text' => 'Your membership expired yesterday',
242 'record_activity' => 1,
243 'start_action_condition' => 'after',
244 'start_action_date' => 'membership_end_date',
245 'start_action_offset' => '1',
246 'start_action_unit' => 'day',
247 'subject' => 'subject send reminder on membership_end_date',
250 $this->fixtures
['sched_membership_end_2month'] = array(
251 'name' => 'sched_membership_end_2month',
252 'title' => 'sched_membership_end_2month',
253 'absolute_date' => '',
254 'body_html' => '<p>body sched_membership_end_2month</p>',
255 'body_text' => 'body sched_membership_end_2month',
258 'end_frequency_interval' => '',
259 'end_frequency_unit' => '',
260 'entity_status' => '',
261 'entity_value' => '',
266 'msg_template_id' => '',
268 'recipient_listing' => '',
269 'recipient_manual' => '',
270 'record_activity' => 1,
271 'repetition_frequency_interval' => '',
272 'repetition_frequency_unit' => '',
273 'start_action_condition' => 'after',
274 'start_action_date' => 'membership_end_date',
275 'start_action_offset' => '2',
276 'start_action_unit' => 'month',
277 'subject' => 'subject sched_membership_end_2month',
280 $this->fixtures
['sched_contact_bday_yesterday'] = array(
281 'name' => 'sched_contact_bday_yesterday',
282 'title' => 'sched_contact_bday_yesterday',
283 'absolute_date' => '',
284 'body_html' => '<p>you look like you were born yesterday!</p>',
285 'body_text' => 'you look like you were born yesterday!',
288 'end_frequency_interval' => '',
289 'end_frequency_unit' => '',
290 'entity_status' => 1,
291 'entity_value' => 'birth_date',
296 'msg_template_id' => '',
298 'recipient_listing' => '',
299 'recipient_manual' => '',
300 'record_activity' => 1,
301 'repetition_frequency_interval' => '',
302 'repetition_frequency_unit' => '',
303 'start_action_condition' => 'after',
304 'start_action_date' => 'date_field',
305 'start_action_offset' => '1',
306 'start_action_unit' => 'day',
307 'subject' => 'subject sched_contact_bday_yesterday',
310 $this->fixtures
['sched_contact_bday_anniv'] = array(
311 'name' => 'sched_contact_bday_anniv',
312 'title' => 'sched_contact_bday_anniv',
313 'absolute_date' => '',
314 'body_html' => '<p>happy birthday!</p>',
315 'body_text' => 'happy birthday!',
318 'end_frequency_interval' => '',
319 'end_frequency_unit' => '',
320 'entity_status' => 2,
321 'entity_value' => 'birth_date',
326 'msg_template_id' => '',
328 'recipient_listing' => '',
329 'recipient_manual' => '',
330 'record_activity' => 1,
331 'repetition_frequency_interval' => '',
332 'repetition_frequency_unit' => '',
333 'start_action_condition' => 'before',
334 'start_action_date' => 'date_field',
335 'start_action_offset' => '1',
336 'start_action_unit' => 'day',
337 'subject' => 'subject sched_contact_bday_anniv',
340 $this->fixtures
['sched_contact_grad_tomorrow'] = array(
341 'name' => 'sched_contact_grad_tomorrow',
342 'title' => 'sched_contact_grad_tomorrow',
343 'absolute_date' => '',
344 'body_html' => '<p>congratulations on your graduation!</p>',
345 'body_text' => 'congratulations on your graduation!',
348 'end_frequency_interval' => '',
349 'end_frequency_unit' => '',
350 'entity_status' => 1,
355 'msg_template_id' => '',
357 'recipient_listing' => '',
358 'recipient_manual' => '',
359 'record_activity' => 1,
360 'repetition_frequency_interval' => '',
361 'repetition_frequency_unit' => '',
362 'start_action_condition' => 'before',
363 'start_action_date' => 'date_field',
364 'start_action_offset' => '1',
365 'start_action_unit' => 'day',
366 'subject' => 'subject sched_contact_grad_tomorrow',
369 $this->fixtures
['sched_contact_grad_anniv'] = array(
370 'name' => 'sched_contact_grad_anniv',
371 'title' => 'sched_contact_grad_anniv',
372 'absolute_date' => '',
373 'body_html' => '<p>dear alum, please send us money.</p>',
374 'body_text' => 'dear alum, please send us money.',
377 'end_frequency_interval' => '',
378 'end_frequency_unit' => '',
379 'entity_status' => 2,
384 'msg_template_id' => '',
386 'recipient_listing' => '',
387 'recipient_manual' => '',
388 'record_activity' => 1,
389 'repetition_frequency_interval' => '',
390 'repetition_frequency_unit' => '',
391 'start_action_condition' => 'after',
392 'start_action_date' => 'date_field',
393 'start_action_offset' => '1',
394 'start_action_unit' => 'week',
395 'subject' => 'subject sched_contact_grad_anniv',
398 $this->fixtures
['sched_contact_created_yesterday'] = array(
399 'name' => 'sched_contact_created_yesterday',
400 'title' => 'sched_contact_created_yesterday',
401 'absolute_date' => '',
402 'body_html' => '<p>Your contact was created yesterday</p>',
403 'body_text' => 'Your contact was created yesterday!',
406 'end_frequency_interval' => '',
407 'end_frequency_unit' => '',
408 'entity_status' => 1,
409 'entity_value' => 'created_date',
414 'msg_template_id' => '',
416 'recipient_listing' => '',
417 'recipient_manual' => '',
418 'record_activity' => 1,
419 'repetition_frequency_interval' => '',
420 'repetition_frequency_unit' => '',
421 'start_action_condition' => 'after',
422 'start_action_date' => 'date_field',
423 'start_action_offset' => '1',
424 'start_action_unit' => 'day',
425 'subject' => 'subject sched_contact_created_yesterday',
428 $this->fixtures
['sched_contact_mod_anniv'] = array(
429 'name' => 'sched_contact_mod_anniv',
430 'title' => 'sched_contact_mod_anniv',
431 'absolute_date' => '',
432 'body_html' => '<p>You last updated your data last year</p>',
433 'body_text' => 'Go update your stuff!',
436 'end_frequency_interval' => '',
437 'end_frequency_unit' => '',
438 'entity_status' => 2,
439 'entity_value' => 'modified_date',
444 'msg_template_id' => '',
446 'recipient_listing' => '',
447 'recipient_manual' => '',
448 'record_activity' => 1,
449 'repetition_frequency_interval' => '',
450 'repetition_frequency_unit' => '',
451 'start_action_condition' => 'before',
452 'start_action_date' => 'date_field',
453 'start_action_offset' => '1',
454 'start_action_unit' => 'day',
455 'subject' => 'subject sched_contact_mod_anniv',
458 $this->fixtures
['sched_eventtype_start_1week_before'] = array(
459 'name' => 'sched_eventtype_start_1week_before',
460 'title' => 'sched_eventtype_start_1week_before',
461 'absolute_date' => '',
462 'body_html' => '<p>body sched_eventtype_start_1week_before ({event.title})</p>',
463 'body_text' => 'body sched_eventtype_start_1week_before ({event.title})',
466 'end_frequency_interval' => '',
467 'end_frequency_unit' => '',
468 'entity_status' => '', // participant status id
469 'entity_value' => '', // event type id
473 'mapping_id' => 2, // event type
474 'msg_template_id' => '',
476 'recipient_listing' => '',
477 'recipient_manual' => '',
478 'record_activity' => 1,
479 'repetition_frequency_interval' => '',
480 'repetition_frequency_unit' => '',
481 'start_action_condition' => 'before',
482 'start_action_date' => 'event_start_date',
483 'start_action_offset' => '1',
484 'start_action_unit' => 'week',
485 'subject' => 'subject sched_eventtype_start_1week_before ({event.title})',
487 $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'] = array(
488 'name' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
489 'title' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
490 'absolute_date' => '',
491 'body_html' => '<p>body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}</p>',
492 'body_text' => 'body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
493 'end_action' => 'after',
494 'end_date' => 'event_end_date',
495 'end_frequency_interval' => '3',
496 'end_frequency_unit' => 'month',
497 'entity_status' => '', // participant status id
498 'entity_value' => '', // event type id
502 'mapping_id' => 2, // event type
503 'msg_template_id' => '',
505 'recipient_listing' => '',
506 'recipient_manual' => '',
507 'record_activity' => 1,
508 'repetition_frequency_interval' => '2',
509 'repetition_frequency_unit' => 'week',
510 'start_action_condition' => 'after',
511 'start_action_date' => 'event_end_date',
512 'start_action_offset' => '2',
513 'start_action_unit' => 'month',
514 'subject' => 'subject sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
517 $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'] = array(
518 'name' => 'sched_membership_end_2month',
519 'title' => 'sched_membership_end_2month',
520 'absolute_date' => '',
521 'body_html' => '<p>body sched_membership_end_2month</p>',
522 'body_text' => 'body sched_membership_end_2month',
524 'end_date' => 'membership_end_date',
525 'end_frequency_interval' => '4',
526 'end_frequency_unit' => 'month',
527 'entity_status' => '',
528 'entity_value' => '',
533 'msg_template_id' => '',
535 'recipient_listing' => '',
536 'recipient_manual' => '',
537 'record_activity' => 1,
538 'repetition_frequency_interval' => '4',
539 'repetition_frequency_unit' => 'week',
540 'start_action_condition' => 'after',
541 'start_action_date' => 'membership_end_date',
542 'start_action_offset' => '2',
543 'start_action_unit' => 'month',
544 'subject' => 'subject sched_membership_end_2month',
546 $this->fixtures
['sched_membership_end_limit_to_none'] = array(
547 'name' => 'limit to none',
548 'title' => 'limit to none',
549 'absolute_date' => '',
550 'body_html' => '<p>body sched_membership_end_2month</p>',
551 'body_text' => 'body sched_membership_end_2month',
554 'end_frequency_interval' => '4',
555 'end_frequency_unit' => 'month',
556 'entity_status' => '',
557 'entity_value' => '',
563 'msg_template_id' => '',
565 'recipient_listing' => '',
566 'recipient_manual' => '',
567 'record_activity' => 1,
568 'repetition_frequency_interval' => '4',
569 'repetition_frequency_unit' => 'week',
570 'start_action_condition' => 'after',
571 'start_action_date' => 'membership_end_date',
572 'start_action_offset' => '2',
573 'start_action_unit' => 'month',
574 'subject' => 'limit to none',
580 * Tears down the fixture, for example, closes a network connection.
582 * This method is called after a test is executed.
584 public function tearDown() {
587 $this->mut
->clearMessages();
590 $this->quickCleanup(array(
591 'civicrm_action_schedule',
592 'civicrm_action_log',
593 'civicrm_membership',
594 'civicrm_participant',
601 public function mailerExamples() {
604 $manyTokensTmpl = implode(';;', array(
605 '{contact.display_name}', // basic contact token
606 '{contact.gender}', // funny legacy contact token
607 '{contact.gender_id}', // funny legacy contact token
608 '{domain.name}', // domain token
609 '{activity.activity_type}', // action-scheduler token
611 $manyTokensExpected = 'test-member@example.com;;Female;;Female;;Second Domain;;Phone Call';
613 // In this example, we use a lot of tokens cutting across multiple components..
615 // Schedule definition.
617 'subject' => "subj $manyTokensTmpl",
618 'body_html' => "html $manyTokensTmpl",
619 'body_text' => "text $manyTokensTmpl",
621 // Assertions (regex).
623 'from_name' => "/^FIXME\$/",
624 'from_email' => "/^info@EXAMPLE.ORG\$/",
625 'subject' => "/^subj $manyTokensExpected\$/",
626 'body_html' => "/^html $manyTokensExpected\$/",
627 'body_text' => "/^text $manyTokensExpected\$/",
631 // In this example, we customize the from address.
633 // Schedule definition.
635 'from_name' => 'Bob',
636 'from_email' => 'bob@example.org',
638 // Assertions (regex).
640 'from_name' => "/^Bob\$/",
641 'from_email' => "/^bob@example.org\$/",
645 // In this example, we autoconvert HTML to text
647 // Schedule definition.
649 'body_html' => '<p>Hello & stuff.</p>',
652 // Assertions (regex).
654 'body_html' => '/^' . preg_quote('<p>Hello & stuff.</p>', '/') . '/',
655 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
659 // In this example, we autoconvert HTML to text
661 // Schedule definition.
664 'body_text' => 'Hello world',
666 // Assertions (regex).
668 'body_html' => '/^--UNDEFINED--$/',
669 'body_text' => '/^Hello world$/',
677 * This generates a single mailing through the scheduled-reminder
678 * system (using an activity-reminder as a baseline) and
679 * checks that the resulting message satisfies various
680 * regular expressions.
682 * @param array $schedule
683 * Values to set/override in the schedule.
684 * Ex: array('subject' => 'Hello, {contact.first_name}!').
685 * @param array $patterns
686 * A list of regexes to compare with the actual email.
687 * Ex: array('subject' => '/^Hello, Alice!/').
688 * Keys: subject, body_text, body_html, from_name, from_email.
689 * @dataProvider mailerExamples
691 public function testMailer($schedule, $patterns) {
692 $actionSchedule = array_merge($this->fixtures
['sched_activity_1day'], $schedule);
693 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
694 $this->assertTrue(is_numeric($actionScheduleDao->id
));
696 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
697 $this->assertTrue(is_numeric($activity->id
));
698 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
701 $source['contact_id'] = $contact['id'];
702 $source['activity_id'] = $activity->id
;
703 $source['record_type_id'] = 2;
704 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
705 $activityContact->save();
707 CRM_Utils_Time
::setTime('2012-06-14 15:00:00');
708 $this->callAPISuccess('job', 'send_reminder', array());
709 $this->mut
->assertRecipients(array(array('test-member@example.com')));
710 foreach ($this->mut
->getAllMessages('ezc') as $message) {
711 /** @var ezcMail $message */
713 $messageArray = array();
714 $messageArray['subject'] = $message->subject
;
715 $messageArray['from_name'] = $message->from
->name
;
716 $messageArray['from_email'] = $message->from
->email
;
717 $messageArray['body_text'] = '--UNDEFINED--';
718 $messageArray['body_html'] = '--UNDEFINED--';
720 foreach ($message->fetchParts() as $part) {
721 /** @var ezcMailText ezcMailText */
722 if ($part instanceof ezcMailText
&& $part->subType
== 'html') {
723 $messageArray['body_html'] = $part->text
;
725 if ($part instanceof ezcMailText
&& $part->subType
== 'plain') {
726 $messageArray['body_text'] = $part->text
;
730 foreach ($patterns as $field => $pattern) {
731 $this->assertRegExp($pattern, $messageArray[$field],
732 "Check that '$field'' matches regex. " . print_r(array('expected' => $patterns, 'actual' => $messageArray), 1));
735 $this->mut
->clearMessages();
739 public function testActivityDateTimeMatchNonRepeatableSchedule() {
740 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day']);
741 $this->assertTrue(is_numeric($actionScheduleDao->id
));
743 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
744 $this->assertTrue(is_numeric($activity->id
));
745 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
748 $source['contact_id'] = $contact['id'];
749 $source['activity_id'] = $activity->id
;
750 $source['record_type_id'] = 2;
751 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
752 $activityContact->save();
754 $this->assertCronRuns(array(
756 // Before the 24-hour mark, no email
757 'time' => '2012-06-14 04:00:00',
758 'recipients' => array(),
759 'subjects' => array(),
762 // After the 24-hour mark, an email
763 'time' => '2012-06-14 15:00:00',
764 'recipients' => array(array('test-member@example.com')),
765 'subjects' => array('1-Day (non-repeating) (about Phone Call)'),
768 // Run cron again; message already sent
770 'recipients' => array(),
775 public function testActivityDateTimeMatchRepeatableSchedule() {
776 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_activity_1day_r']);
777 $this->assertTrue(is_numeric($actionScheduleDao->id
));
779 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures
['phonecall']);
780 $this->assertTrue(is_numeric($activity->id
));
781 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures
['contact']);
784 $source['contact_id'] = $contact['id'];
785 $source['activity_id'] = $activity->id
;
786 $source['record_type_id'] = 2;
787 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
788 $activityContact->save();
790 $this->assertCronRuns(array(
792 // Before the 24-hour mark, no email
793 'time' => '012-06-14 04:00:00',
794 'recipients' => array(),
795 'subjects' => array(),
798 // After the 24-hour mark, an email
799 'time' => '2012-06-14 15:00:00',
800 'recipients' => array(array('test-member@example.com')),
801 'subjects' => array('1-Day (repeating) (about Phone Call)'),
804 // Run cron 4 hours later; first message already sent
805 'time' => '2012-06-14 20:00:00',
806 'recipients' => array(),
807 'subjects' => array(),
810 // Run cron 6 hours later; send second message.
811 'time' => '2012-06-14 21:00:01',
812 'recipients' => array(array('test-member@example.com')),
813 'subjects' => array('1-Day (repeating) (about Phone Call)'),
819 * For contacts/activities which don't match the schedule filter,
820 * an email should *not* be sent.
822 // TODO // function testActivityDateTime_NonMatch() { }
825 * For contacts/members which match schedule based on join date,
826 * an email should be sent.
828 public function testMembershipJoinDateMatch() {
829 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 1)));
830 $this->assertTrue(is_numeric($membership->id
));
831 $result = $this->callAPISuccess('Email', 'create', array(
832 'contact_id' => $membership->contact_id
,
833 'email' => 'test-member@example.com',
834 'location_type_id' => 1,
836 $this->assertAPISuccess($result);
838 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
839 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
840 $actionSchedule['entity_value'] = $membership->membership_type_id
;
841 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
842 $this->assertTrue(is_numeric($actionScheduleDao->id
));
844 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
845 $this->assertCronRuns(array(
847 // Before the 2-week mark, no email.
848 'time' => '2012-03-28 01:00:00',
849 'recipients' => array(),
850 'subjects' => array(),
853 // After the 2-week mark, send an email.
854 'time' => '2012-03-29 01:00:00',
855 'recipients' => array(array('test-member@example.com')),
856 'subjects' => array('subject sched_membership_join_2week (joined March 15th, 2012)'),
862 * Test end date email sent.
864 * For contacts/members which match schedule based on join date,
865 * an email should be sent.
867 public function testMembershipJoinDateNonMatch() {
868 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures
['rolling_membership']);
869 $this->assertTrue(is_numeric($membership->id
));
870 $result = $this->callAPISuccess('Email', 'create', array(
871 'contact_id' => $membership->contact_id
,
872 'location_type_id' => 1,
873 'email' => 'test-member@example.com',
876 // Add an alternative membership type, and only send messages for that type
877 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
878 $this->assertTrue(is_numeric($extraMembershipType->id
));
879 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($this->fixtures
['sched_membership_join_2week']);
880 $this->assertTrue(is_numeric($actionScheduleDao->id
));
881 $actionScheduleDao->entity_value
= $extraMembershipType->id
;
882 $actionScheduleDao->save();
884 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
885 $this->assertCronRuns(array(
887 // After the 2-week mark, don't send email because we have different membership type.
888 'time' => '2012-03-29 01:00:00',
889 'recipients' => array(),
895 * Test that the first and SECOND notifications are sent out.
897 public function testMembershipEndDateRepeat() {
898 // creates membership with end_date = 20120615
899 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
900 $result = $this->callAPISuccess('Email', 'create', array(
901 'contact_id' => $membership->contact_id
,
902 'email' => 'test-member@example.com',
904 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
906 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
907 $actionSchedule['entity_value'] = $membership->membership_type_id
;
908 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
910 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
911 $this->assertCronRuns(array(
913 // After the 2-week mark, send an email.
914 'time' => '2012-08-15 01:00:00',
915 'recipients' => array(array('test-member@example.com')),
918 // After the 2-week mark, send an email.
919 'time' => '2012-09-12 01:00:00',
920 'recipients' => array(array('test-member@example.com')),
926 * Test behaviour when date changes.
928 * Test that the first notification is sent but the second is NOT sent if the end date changes in
932 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
933 // creates membership with end_date = 20120615
934 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
935 $this->callAPISuccess('Email', 'create', array(
936 'contact_id' => $membership->contact_id
,
937 'email' => 'test-member@example.com',
939 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
941 $actionSchedule = $this->fixtures
['sched_membership_end_2month_repeat_twice_4_weeks'];
942 $actionSchedule['entity_value'] = $membership->membership_type_id
;
943 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
944 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
945 $this->assertCronRuns(array(
947 // After the 2-week mark, send an email.
948 'time' => '2012-08-15 01:00:00',
949 'recipients' => array(array('test-member@example.com')),
953 // Extend membership - reminder should NOT go out.
954 $this->callAPISuccess('membership', 'create', array('id' => $membership->id
, 'end_date' => '2014-01-01'));
955 $this->assertCronRuns(array(
957 // After the 2-week mark, send an email.
958 'time' => '2012-09-12 01:00:00',
959 'recipients' => array(),
965 * Test membership end date email sends.
967 * For contacts/members which match schedule based on end date,
968 * an email should be sent.
970 public function testMembershipEndDateMatch() {
971 // creates membership with end_date = 20120615
972 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
973 $this->assertTrue(is_numeric($membership->id
));
974 $this->callAPISuccess('Email', 'create', array(
975 'contact_id' => $membership->contact_id
,
976 'email' => 'test-member@example.com',
978 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
980 $actionSchedule = $this->fixtures
['sched_membership_end_2week'];
981 $actionSchedule['entity_value'] = $membership->membership_type_id
;
982 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
983 $this->assertTrue(is_numeric($actionScheduleDao->id
));
985 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
986 $this->assertCronRuns(array(
988 // Before the 2-week mark, no email.
989 'time' => '2012-05-31 01:00:00',
990 // 'time' => '2012-06-01 01:00:00',
991 // FIXME: Is this the right boundary?
992 'recipients' => array(),
995 // After the 2-week mark, send an email.
996 'time' => '2012-06-01 01:00:00',
997 'recipients' => array(array('test-member@example.com')),
1001 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1002 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1003 $membership->end_date
= '2012-09-15';
1004 $membership->save();
1006 //change the email id of chosen membership contact to assert
1007 //recipient of not the previously sent mail but the new one
1008 $result = $this->callAPISuccess('Email', 'create', array(
1010 'contact_id' => $membership->contact_id
,
1011 'email' => 'member2@example.com',
1013 $this->assertAPISuccess($result);
1015 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1016 $this->assertCronRuns(array(
1018 // Before the 2-week mark, no email
1019 'time' => '2012-08-31 01:00:00',
1020 'recipients' => array(),
1022 //array( // After the 2-week mark, send an email
1023 //'time' => '2012-09-01 01:00:00',
1024 //'recipients' => array(array('member2@example.com')),
1031 * Test membership end date email.
1033 * For contacts/members which match schedule based on end date,
1034 * an email should be sent.
1036 public function testMembershipEndDateNoMatch() {
1037 // creates membership with end_date = 20120615
1038 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 3)));
1039 $this->assertTrue(is_numeric($membership->id
));
1040 $result = $this->callAPISuccess('Email', 'create', array(
1041 'contact_id' => $membership->contact_id
,
1042 'email' => 'test-member@example.com',
1044 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1046 $actionSchedule = $this->fixtures
['sched_membership_end_2month'];
1047 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1048 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1049 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1051 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1052 $this->assertCronRuns(array(
1054 // Before the 2-week mark, no email.
1055 'time' => '2012-05-31 01:00:00',
1056 // 'time' => '2012-06-01 01:00:00',
1057 // FIXME: Is this the right boundary?
1058 'recipients' => array(),
1061 // After the 2-week mark, send an email.
1062 'time' => '2013-05-01 01:00:00',
1063 'recipients' => array(),
1068 public function testContactBirthDateNoAnniv() {
1069 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1070 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1071 $actionSchedule = $this->fixtures
['sched_contact_bday_yesterday'];
1072 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1073 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1074 $this->assertCronRuns(array(
1076 // On the birthday, no email.
1077 'time' => '2005-07-07 01:00:00',
1078 'recipients' => array(),
1081 // The next day, send an email.
1082 'time' => '2005-07-08 20:00:00',
1083 'recipients' => array(array('test-bday@example.com')),
1088 public function testContactBirthDateAnniversary() {
1089 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1090 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1091 $actionSchedule = $this->fixtures
['sched_contact_bday_anniv'];
1092 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1093 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1094 $this->assertCronRuns(array(
1096 // On some random day, no email.
1097 'time' => '2014-03-07 01:00:00',
1098 'recipients' => array(),
1101 // On the eve of their 9th birthday, send an email.
1102 'time' => '2014-07-06 20:00:00',
1103 'recipients' => array(array('test-bday@example.com')),
1108 public function testContactCustomDateNoAnniv() {
1110 'title' => 'Test_Group',
1111 'name' => 'test_group',
1112 'extends' => array('Individual'),
1113 'style' => 'Inline',
1114 'is_multiple' => FALSE,
1117 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1119 'label' => 'Graduation',
1120 'data_type' => 'Date',
1121 'html_type' => 'Select Date',
1122 'custom_group_id' => $createGroup['id'],
1124 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1125 $contactParams = $this->fixtures
['contact'];
1126 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1127 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1128 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1129 $actionSchedule = $this->fixtures
['sched_contact_grad_tomorrow'];
1130 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1131 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1132 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1133 $this->assertCronRuns(array(
1135 // On some random day, no email.
1136 'time' => '2014-03-07 01:00:00',
1137 'recipients' => array(),
1140 // On the eve of their graduation, send an email.
1141 'time' => '2013-12-15 20:00:00',
1142 'recipients' => array(array('test-member@example.com')),
1145 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1148 public function testContactCreatedNoAnniv() {
1149 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1150 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1151 $actionSchedule = $this->fixtures
['sched_contact_created_yesterday'];
1152 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1153 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1154 $this->assertCronRuns(array(
1156 // On the date created, no email.
1157 'time' => $contact['values'][$contact['id']]['created_date'],
1158 'recipients' => array(),
1161 // The next day, send an email.
1162 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1163 'recipients' => array(array('test-bday@example.com')),
1168 public function testContactModifiedAnniversary() {
1169 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures
['contact_birthdate']);
1170 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1171 $actionSchedule = $this->fixtures
['sched_contact_mod_anniv'];
1172 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1173 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1174 $this->assertCronRuns(array(
1176 // On some random day, no email.
1177 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1178 'recipients' => array(),
1181 // On the eve of 3 years after they were modified, send an email.
1182 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' +3 years -23 hours')),
1183 'recipients' => array(array('test-bday@example.com')),
1189 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
1191 public function testMembershipLimitToNone() {
1192 // creates membership with end_date = 20120615
1193 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
1195 $this->assertTrue(is_numeric($membership->id
));
1196 $result = $this->callAPISuccess('Email', 'create', array(
1197 'contact_id' => $membership->contact_id
,
1198 'email' => 'member@example.com',
1200 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1201 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
1203 $this->assertAPISuccess($result);
1205 $actionSchedule = $this->fixtures
['sched_membership_end_limit_to_none'];
1206 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1207 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1208 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1210 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1211 $this->assertCronRuns(array(
1213 // Before the 2-week mark, no email.
1214 'time' => '2012-05-31 01:00:00',
1215 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1216 'recipients' => array(),
1221 public function testMembership_referenceDate() {
1222 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
1224 $this->assertTrue(is_numeric($membership->id
));
1225 $result = $this->callAPISuccess('Email', 'create', array(
1226 'contact_id' => $membership->contact_id
,
1227 'email' => 'member@example.com',
1230 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1231 $this->assertAPISuccess($result);
1233 $actionSchedule = $this->fixtures
['sched_membership_join_2week'];
1234 $actionSchedule['entity_value'] = $membership->membership_type_id
;
1235 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1236 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1238 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1239 $this->assertCronRuns(array(
1241 // After the 2-week mark, send an email
1242 'time' => '2012-03-29 01:00:00',
1243 'recipients' => array(array('member@example.com')),
1246 // After the 2-week 1day mark, don't send an email
1247 'time' => '2012-03-30 01:00:00',
1248 'recipients' => array(),
1252 //check if reference date is set to membership's join date
1253 //as per the action_start_date chosen for current schedule reminder
1254 $this->assertEquals('2012-03-15',
1255 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id
, 'reference_date', 'contact_id')
1258 //change current membership join date that may signifies as membership renewal activity
1259 $membership->join_date
= '2012-03-29';
1260 $membership->save();
1262 $this->assertCronRuns(array(
1264 // After the 13 days of the changed join date 2012-03-29, don't send an email
1265 'time' => '2012-04-11 01:00:00',
1266 'recipients' => array(),
1269 // After the 2-week of the changed join date 2012-03-29, send an email
1270 'time' => '2012-04-12 01:00:00',
1271 'recipients' => array(array('member@example.com')),
1276 public function testMembershipOnMultipleReminder() {
1277 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures
['rolling_membership'], array('status_id' => 2)));
1279 $this->assertTrue(is_numeric($membership->id
));
1280 $result = $this->callAPISuccess('Email', 'create', array(
1281 'contact_id' => $membership->contact_id
,
1282 'email' => 'member@example.com',
1284 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $membership->contact_id
)));
1285 $this->assertAPISuccess($result);
1287 $actionScheduleBefore = $this->fixtures
['sched_membership_end_2week']; // Send email 2 weeks before end_date
1288 $actionScheduleOn = $this->fixtures
['sched_on_membership_end_date']; // Send email on end_date/expiry date
1289 $actionScheduleAfter = $this->fixtures
['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
1290 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id
;
1291 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
1292 $
$value = CRM_Core_BAO_ActionSchedule
::add($
$value);
1293 $this->assertTrue(is_numeric($
$value->id
));
1296 $this->assertCronRuns(
1299 // 1day 2weeks before membership end date(MED), don't send mail
1300 'time' => '2012-05-31 01:00:00',
1301 'recipients' => array(),
1304 // 2 weeks before MED, send an email
1305 'time' => '2012-06-01 01:00:00',
1306 'recipients' => array(array('member@example.com')),
1309 // 1day before MED, don't send mail
1310 'time' => '2012-06-14 01:00:00',
1311 'recipients' => array(),
1314 // On MED, send an email
1315 'time' => '2012-06-15 00:00:00',
1316 'recipients' => array(array('member@example.com')),
1319 // After 1day of MED, send an email
1320 'time' => '2012-06-16 01:00:00',
1321 'recipients' => array(array('member@example.com')),
1324 // After 1day 1min of MED, don't send an email
1325 'time' => '2012-06-17 00:01:00',
1326 'recipients' => array(),
1331 // Assert the timestamp as of when the emails of respective three reminders as configured
1332 // 2 weeks before, on and 1 day after MED, are sent
1333 $this->assertEquals('2012-06-01 01:00:00',
1334 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id
, 'action_date_time', 'action_schedule_id', TRUE));
1335 $this->assertEquals('2012-06-15 00:00:00',
1336 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id
, 'action_date_time', 'action_schedule_id', TRUE));
1337 $this->assertEquals('2012-06-16 01:00:00',
1338 CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id
, 'action_date_time', 'action_schedule_id', TRUE));
1340 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1341 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1342 $membership->end_date
= '2012-06-20';
1343 $membership->save();
1345 $result = $this->callAPISuccess('Contact', 'get', array('id' => $membership->contact_id
));
1346 $this->assertCronRuns(
1349 // 1day 2weeks before membership end date(MED), don't send mail
1350 'time' => '2012-06-05 01:00:00',
1351 'recipients' => array(),
1354 // 2 weeks before MED, send an email
1355 'time' => '2012-06-06 01:00:00',
1356 'recipients' => array(array('member@example.com')),
1359 // 1day before MED, don't send mail
1360 'time' => '2012-06-19 01:00:00',
1361 'recipients' => array(),
1364 // On MED, send an email
1365 'time' => '2012-06-20 00:00:00',
1366 'recipients' => array(array('member@example.com')),
1369 // After 1day of MED, send an email
1370 'time' => '2012-06-21 01:00:00',
1371 'recipients' => array(array('member@example.com')),
1374 // After 1day 1min of MED, don't send an email
1375 'time' => '2012-07-21 00:01:00',
1376 'recipients' => array(),
1381 public function testContactCustomDate_Anniv() {
1383 'title' => 'Test_Group now',
1384 'name' => 'test_group_now',
1385 'extends' => array('Individual'),
1386 'style' => 'Inline',
1387 'is_multiple' => FALSE,
1390 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1392 'label' => 'Graduation',
1393 'data_type' => 'Date',
1394 'html_type' => 'Select Date',
1395 'custom_group_id' => $createGroup['id'],
1397 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1399 $contactParams = $this->fixtures
['contact'];
1400 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1401 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1402 $this->_testObjects
['CRM_Contact_DAO_Contact'][] = $contact['id'];
1403 $actionSchedule = $this->fixtures
['sched_contact_grad_anniv'];
1404 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1405 $actionScheduleDao = CRM_Core_BAO_ActionSchedule
::add($actionSchedule);
1406 $this->assertTrue(is_numeric($actionScheduleDao->id
));
1407 $this->assertCronRuns(array(
1409 // On some random day, no email.
1410 'time' => '2014-03-07 01:00:00',
1411 'recipients' => array(),
1414 // A week after their 5th anniversary of graduation, send an email.
1415 'time' => '2018-12-23 20:00:00',
1416 'recipients' => array(array('test-member@example.com')),
1419 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1422 public function testEventTypeStartDate() {
1423 // Create event+participant with start_date = 20120315, end_date = 20120615.
1424 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], array('status_id' => 2)));
1425 $this->callAPISuccess('Email', 'create', array(
1426 'contact_id' => $participant->contact_id
,
1427 'email' => 'test-event@example.com',
1429 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $participant->contact_id
)));
1431 $actionSchedule = $this->fixtures
['sched_eventtype_start_1week_before'];
1432 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
1433 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1435 //echo "CREATED\n"; ob_flush(); sleep(20);
1437 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1438 $this->assertCronRuns(array(
1441 'time' => '2012-03-02 01:00:00',
1442 'recipients' => array(),
1446 'time' => '2012-03-08 01:00:00',
1447 'recipients' => array(array('test-event@example.com')),
1450 // And then nothing else
1451 'time' => '2012-03-16 01:00:00',
1452 'recipients' => array(),
1457 public function testEventTypeEndDateRepeat() {
1458 // Create event+participant with start_date = 20120315, end_date = 20120615.
1459 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures
['participant'], array('status_id' => 2)));
1460 $this->callAPISuccess('Email', 'create', array(
1461 'contact_id' => $participant->contact_id
,
1462 'email' => 'test-event@example.com',
1464 $c = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures
['contact'], array('contact_id' => $participant->contact_id
)));
1466 $actionSchedule = $this->fixtures
['sched_eventtype_end_2month_repeat_twice_2_weeks'];
1467 $actionSchedule['entity_value'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $participant->event_id
, 'event_type_id');
1468 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1470 $this->assertCronRuns(array(
1473 'time' => '2012-08-13 01:00:00',
1474 'recipients' => array(),
1477 // After the 2-month mark, send an email.
1478 'time' => '2012-08-16 01:00:00',
1479 'recipients' => array(array('test-event@example.com')),
1482 // After 2 months and 1 week, don't repeat yet.
1483 'time' => '2012-08-23 02:00:00',
1484 'recipients' => array(),
1487 // After 2 months and 2 weeks
1488 'time' => '2012-08-30 02:00:00',
1489 'recipients' => array(array('test-event@example.com')),
1492 // After 2 months and 4 week
1493 'time' => '2012-09-13 02:00:00',
1494 'recipients' => array(array('test-event@example.com')),
1497 // After 2 months and 6 weeks
1498 'time' => '2012-09-27 01:00:00',
1499 'recipients' => array(),
1504 // TODO // function testMembershipEndDate_NonMatch() { }
1505 // TODO // function testEventTypeStartDate_Match() { }
1506 // TODO // function testEventTypeEndDate_Match() { }
1507 // TODO // function testEventNameStartDate_Match() { }
1508 // TODO // function testEventNameEndDate_Match() { }
1511 * Run a series of cron jobs and make an assertion about email deliveries.
1513 * @param array $cronRuns
1514 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1515 * - time: string, e.g. '2012-06-15 21:00:01'
1516 * - recipients: array(array(string)), list of email addresses which should receive messages
1518 public function assertCronRuns($cronRuns) {
1519 foreach ($cronRuns as $cronRun) {
1520 CRM_Utils_Time
::setTime($cronRun['time']);
1521 $this->callAPISuccess('job', 'send_reminder', array());
1522 $this->mut
->assertRecipients($cronRun['recipients']);
1523 if (array_key_exists('subjects', $cronRun)) {
1524 $this->mut
->assertSubjects($cronRun['subjects']);
1526 $this->mut
->clearMessages();
1531 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1533 private $_testObjects;
1536 * Sets up the fixture, for example, opens a network connection.
1538 * This method is called before a test is executed.
1540 protected function _setUp() {
1541 $this->_testObjects
= array();
1545 * Tears down the fixture, for example, closes a network connection.
1547 * This method is called after a test is executed.
1549 protected function _tearDown() {
1551 $this->deleteTestObjects();
1555 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
1556 * created entities and provides for brainless cleanup.
1558 * @see CRM_Core_DAO::createTestObject
1561 * @param array $params
1562 * @param int $numObjects
1563 * @param bool $createOnly
1565 * @return array|NULL|object
1567 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
1568 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
1569 if (is_array($objects)) {
1570 $this->registerTestObjects($objects);
1573 $this->registerTestObjects(array($objects));
1579 * @param array $objects
1580 * DAO or BAO objects.
1582 public function registerTestObjects($objects) {
1583 //if (is_object($objects)) {
1584 // $objects = array($objects);
1586 foreach ($objects as $object) {
1587 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1588 $this->_testObjects
[$daoName][] = $object->id
;
1592 public function deleteTestObjects() {
1593 // Note: You might argue that the FK relations between test
1594 // objects could make this problematic; however, it should
1595 // behave intuitively as long as we mentally split our
1596 // test-objects between the "manual/primary records"
1597 // and the "automatic/secondary records"
1598 foreach ($this->_testObjects
as $daoName => $daoIds) {
1599 foreach ($daoIds as $daoId) {
1600 CRM_Core_DAO
::deleteTestObjects($daoName, array('id' => $daoId));
1603 $this->_testObjects
= array();