CRM-13244 - ActionScheduleTest - Use some tokens
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / ActionScheduleTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31 /**
32 * Class CRM_Core_BAO_ActionScheduleTest
33 */
34 class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
35
36 /**
37 * @var CiviMailUtils
38 */
39 public $mut;
40
41 public function setUp() {
42 parent::setUp();
43
44 require_once 'CiviTest/CiviMailUtils.php';
45 $this->mut = new CiviMailUtils($this, TRUE);
46
47 $this->fixtures['rolling_membership'] = array(
48 'membership_type_id' => array(
49 'period_type' => 'rolling',
50 'duration_unit' => 'month',
51 'duration_interval' => '3',
52 'is_active' => 1,
53 ),
54 'join_date' => '20120315',
55 'start_date' => '20120315',
56 'end_date' => '20120615',
57 'is_override' => 0,
58 );
59
60 $this->fixtures['rolling_membership_past'] = array(
61 'membership_type_id' => array(
62 'period_type' => 'rolling',
63 'duration_unit' => 'month',
64 'duration_interval' => '3',
65 'is_active' => 1,
66 ),
67 'join_date' => '20100310',
68 'start_date' => '20100310',
69 'end_date' => '20100610',
70 'is_override' => 'NULL',
71 );
72
73 $this->fixtures['phonecall'] = array(
74 'status_id' => 1,
75 'activity_type_id' => 2,
76 'activity_date_time' => '20120615100000',
77 'is_current_revision' => 1,
78 'is_deleted' => 0,
79 );
80 $this->fixtures['contact'] = array(
81 'is_deceased' => 0,
82 'contact_type' => 'Individual',
83 'email' => 'test-member@example.com',
84 );
85 $this->fixtures['contact_birthdate'] = array(
86 'is_deceased' => 0,
87 'contact_type' => 'Individual',
88 'email' => 'test-bday@example.com',
89 'birth_date' => '20050707',
90 );
91 $this->fixtures['sched_activity_1day'] = array(
92 'name' => 'One_Day_Phone_Call_Notice',
93 'title' => 'One Day Phone Call Notice',
94 'limit_to' => '1',
95 'absolute_date' => NULL,
96 'body_html' => '<p>1-Day (non-repeating)</p>',
97 'body_text' => '1-Day (non-repeating)',
98 'end_action' => NULL,
99 'end_date' => NULL,
100 'end_frequency_interval' => NULL,
101 'end_frequency_unit' => NULL,
102 'entity_status' => '1',
103 'entity_value' => '2',
104 'group_id' => NULL,
105 'is_active' => '1',
106 'is_repeat' => '0',
107 'mapping_id' => '1',
108 'msg_template_id' => NULL,
109 'recipient' => '2',
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) (about {activity.activity_type})',
120 );
121 $this->fixtures['sched_activity_1day_r'] = array(
122 'name' => 'One_Day_Phone_Call_Notice_R',
123 'title' => 'One Day Phone Call Notice R',
124 'limit_to' => 1,
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',
134 'group_id' => NULL,
135 'is_active' => '1',
136 'is_repeat' => '1',
137 'mapping_id' => '1',
138 'msg_template_id' => NULL,
139 'recipient' => '2',
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) (about {activity.activity_type})',
150 );
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',
157 'end_action' => '',
158 'end_date' => '',
159 'end_frequency_interval' => '',
160 'end_frequency_unit' => '',
161 'entity_status' => '',
162 'entity_value' => '',
163 'group_id' => '',
164 'is_active' => 1,
165 'is_repeat' => '0',
166 'mapping_id' => 4,
167 'msg_template_id' => '',
168 'recipient' => '',
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 (joined {membership.join_date})',
179 );
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',
186 'end_action' => '',
187 'end_date' => '',
188 'end_frequency_interval' => '',
189 'end_frequency_unit' => '',
190 'entity_status' => '',
191 'entity_value' => '',
192 'group_id' => '',
193 'is_active' => 1,
194 'is_repeat' => '0',
195 'mapping_id' => 4,
196 'msg_template_id' => '',
197 'recipient' => '',
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',
208 );
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',
214 'is_active' => 1,
215 'mapping_id' => 4,
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',
222 );
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',
228 'is_active' => 1,
229 'mapping_id' => 4,
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',
236 );
237
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',
244 'end_action' => '',
245 'end_date' => '',
246 'end_frequency_interval' => '',
247 'end_frequency_unit' => '',
248 'entity_status' => '',
249 'entity_value' => '',
250 'group_id' => '',
251 'is_active' => 1,
252 'is_repeat' => '0',
253 'mapping_id' => 4,
254 'msg_template_id' => '',
255 'recipient' => '',
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',
266 );
267
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!',
274 'end_action' => '',
275 'end_date' => '',
276 'end_frequency_interval' => '',
277 'end_frequency_unit' => '',
278 'entity_status' => 1,
279 'entity_value' => 'birth_date',
280 'group_id' => '',
281 'is_active' => 1,
282 'is_repeat' => '0',
283 'mapping_id' => 6,
284 'msg_template_id' => '',
285 'recipient' => '',
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',
296 );
297
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!',
304 'end_action' => '',
305 'end_date' => '',
306 'end_frequency_interval' => '',
307 'end_frequency_unit' => '',
308 'entity_status' => 2,
309 'entity_value' => 'birth_date',
310 'group_id' => '',
311 'is_active' => 1,
312 'is_repeat' => '0',
313 'mapping_id' => 6,
314 'msg_template_id' => '',
315 'recipient' => '',
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',
326 );
327
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!',
334 'end_action' => '',
335 'end_date' => '',
336 'end_frequency_interval' => '',
337 'end_frequency_unit' => '',
338 'entity_status' => 1,
339 'group_id' => '',
340 'is_active' => 1,
341 'is_repeat' => '0',
342 'mapping_id' => 6,
343 'msg_template_id' => '',
344 'recipient' => '',
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',
355 );
356
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.',
363 'end_action' => '',
364 'end_date' => '',
365 'end_frequency_interval' => '',
366 'end_frequency_unit' => '',
367 'entity_status' => 2,
368 'group_id' => '',
369 'is_active' => 1,
370 'is_repeat' => '0',
371 'mapping_id' => 6,
372 'msg_template_id' => '',
373 'recipient' => '',
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',
384 );
385
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!',
392 'end_action' => '',
393 'end_date' => '',
394 'end_frequency_interval' => '',
395 'end_frequency_unit' => '',
396 'entity_status' => 1,
397 'entity_value' => 'created_date',
398 'group_id' => '',
399 'is_active' => 1,
400 'is_repeat' => '0',
401 'mapping_id' => 6,
402 'msg_template_id' => '',
403 'recipient' => '',
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',
414 );
415
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!',
422 'end_action' => '',
423 'end_date' => '',
424 'end_frequency_interval' => '',
425 'end_frequency_unit' => '',
426 'entity_status' => 2,
427 'entity_value' => 'modified_date',
428 'group_id' => '',
429 'is_active' => 1,
430 'is_repeat' => '0',
431 'mapping_id' => 6,
432 'msg_template_id' => '',
433 'recipient' => '',
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',
444 );
445
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',
452 'end_action' => '',
453 'end_date' => 'membership_end_date',
454 'end_frequency_interval' => '4',
455 'end_frequency_unit' => 'month',
456 'entity_status' => '',
457 'entity_value' => '',
458 'group_id' => '',
459 'is_active' => 1,
460 'is_repeat' => '1',
461 'mapping_id' => 4,
462 'msg_template_id' => '',
463 'recipient' => '',
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',
474 );
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',
481 'end_action' => '',
482 'end_date' => '',
483 'end_frequency_interval' => '4',
484 'end_frequency_unit' => 'month',
485 'entity_status' => '',
486 'entity_value' => '',
487 'limit_to' => 0,
488 'group_id' => '',
489 'is_active' => 1,
490 'is_repeat' => '1',
491 'mapping_id' => 4,
492 'msg_template_id' => '',
493 'recipient' => '',
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',
504 );
505 $this->_setUp();
506 }
507
508 /**
509 * Tears down the fixture, for example, closes a network connection.
510 *
511 * This method is called after a test is executed.
512 */
513 public function tearDown() {
514 parent::tearDown();
515
516 $this->mut->clearMessages();
517 $this->mut->stop();
518 unset($this->mut);
519 $this->quickCleanup(array(
520 'civicrm_action_schedule',
521 'civicrm_action_log',
522 'civicrm_membership',
523 'civicrm_email',
524 ));
525 $this->_tearDown();
526 }
527
528 public function testActivityDateTimeMatchNonRepeatableSchedule() {
529 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day']);
530 $this->assertTrue(is_numeric($actionScheduleDao->id));
531
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']);
535 $activity->save();
536
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();
542
543 $this->assertCronRuns(array(
544 array(
545 // Before the 24-hour mark, no email
546 'time' => '2012-06-14 04:00:00',
547 'recipients' => array(),
548 'subjects' => array(),
549 ),
550 array(
551 // After the 24-hour mark, an email
552 'time' => '2012-06-14 15:00:00',
553 'recipients' => array(array('test-member@example.com')),
554 'subjects' => array('1-Day (non-repeating) (about Phone Call)'),
555 ),
556 array(
557 // Run cron again; message already sent
558 'time' => '',
559 'recipients' => array(),
560 ),
561 ));
562 }
563
564 public function testActivityDateTimeMatchRepeatableSchedule() {
565 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day_r']);
566 $this->assertTrue(is_numeric($actionScheduleDao->id));
567
568 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
569 $this->assertTrue(is_numeric($activity->id));
570 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
571 $activity->save();
572
573 $source['contact_id'] = $contact['id'];
574 $source['activity_id'] = $activity->id;
575 $source['record_type_id'] = 2;
576 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
577 $activityContact->save();
578
579 $this->assertCronRuns(array(
580 array(
581 // Before the 24-hour mark, no email
582 'time' => '012-06-14 04:00:00',
583 'recipients' => array(),
584 'subjects' => array(),
585 ),
586 array(
587 // After the 24-hour mark, an email
588 'time' => '2012-06-14 15:00:00',
589 'recipients' => array(array('test-member@example.com')),
590 'subjects' => array('1-Day (repeating) (about Phone Call)'),
591 ),
592 array(
593 // Run cron 4 hours later; first message already sent
594 'time' => '2012-06-14 20:00:00',
595 'recipients' => array(),
596 'subjects' => array(),
597 ),
598 array(
599 // Run cron 6 hours later; send second message.
600 'time' => '2012-06-14 21:00:01',
601 'recipients' => array(array('test-member@example.com')),
602 'subjects' => array('1-Day (repeating) (about Phone Call)'),
603 ),
604 ));
605 }
606
607 /**
608 * For contacts/activities which don't match the schedule filter,
609 * an email should *not* be sent.
610 */
611 // TODO // function testActivityDateTime_NonMatch() { }
612
613 /**
614 * For contacts/members which match schedule based on join date,
615 * an email should be sent.
616 */
617 public function testMembershipJoinDateMatch() {
618 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1)));
619 $this->assertTrue(is_numeric($membership->id));
620 $result = $this->callAPISuccess('Email', 'create', array(
621 'contact_id' => $membership->contact_id,
622 'email' => 'test-member@example.com',
623 'location_type_id' => 1,
624 ));
625 $this->assertAPISuccess($result);
626
627 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
628 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
629 $actionSchedule['entity_value'] = $membership->membership_type_id;
630 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
631 $this->assertTrue(is_numeric($actionScheduleDao->id));
632
633 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
634 $this->assertCronRuns(array(
635 array(
636 // Before the 2-week mark, no email.
637 'time' => '2012-03-28 01:00:00',
638 'recipients' => array(),
639 'subjects' => array(),
640 ),
641 array(
642 // After the 2-week mark, send an email.
643 'time' => '2012-03-29 01:00:00',
644 'recipients' => array(array('test-member@example.com')),
645 'subjects' => array('subject sched_membership_join_2week (joined March 15th, 2012)'),
646 ),
647 ));
648 }
649
650 /**
651 * Test end date email sent.
652 *
653 * For contacts/members which match schedule based on join date,
654 * an email should be sent.
655 */
656 public function testMembershipJoinDateNonMatch() {
657 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures['rolling_membership']);
658 $this->assertTrue(is_numeric($membership->id));
659 $result = $this->callAPISuccess('Email', 'create', array(
660 'contact_id' => $membership->contact_id,
661 'location_type_id' => 1,
662 'email' => 'test-member@example.com',
663 ));
664
665 // Add an alternative membership type, and only send messages for that type
666 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
667 $this->assertTrue(is_numeric($extraMembershipType->id));
668 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_membership_join_2week']);
669 $this->assertTrue(is_numeric($actionScheduleDao->id));
670 $actionScheduleDao->entity_value = $extraMembershipType->id;
671 $actionScheduleDao->save();
672
673 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
674 $this->assertCronRuns(array(
675 array(
676 // After the 2-week mark, don't send email because we have different membership type.
677 'time' => '2012-03-29 01:00:00',
678 'recipients' => array(),
679 ),
680 ));
681 }
682
683 /**
684 * Test that the first and SECOND notifications are sent out.
685 */
686 public function testMembershipEndDateRepeat() {
687 // creates membership with end_date = 20120615
688 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
689 $result = $this->callAPISuccess('Email', 'create', array(
690 'contact_id' => $membership->contact_id,
691 'email' => 'test-member@example.com',
692 ));
693 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
694
695 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
696 $actionSchedule['entity_value'] = $membership->membership_type_id;
697 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
698
699 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
700 $this->assertCronRuns(array(
701 array(
702 // After the 2-week mark, send an email.
703 'time' => '2012-08-15 01:00:00',
704 'recipients' => array(array('test-member@example.com')),
705 ),
706 array(
707 // After the 2-week mark, send an email.
708 'time' => '2012-09-12 01:00:00',
709 'recipients' => array(array('test-member@example.com')),
710 ),
711 ));
712 }
713
714 /**
715 * Test behaviour when date changes.
716 *
717 * Test that the first notification is sent but the second is NOT sent if the end date changes in
718 * between
719 * see CRM-15376
720 */
721 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
722 // creates membership with end_date = 20120615
723 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
724 $this->callAPISuccess('Email', 'create', array(
725 'contact_id' => $membership->contact_id,
726 'email' => 'test-member@example.com',
727 ));
728 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
729
730 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
731 $actionSchedule['entity_value'] = $membership->membership_type_id;
732 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
733 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
734 $this->assertCronRuns(array(
735 array(
736 // After the 2-week mark, send an email.
737 'time' => '2012-08-15 01:00:00',
738 'recipients' => array(array('test-member@example.com')),
739 ),
740 ));
741
742 // Extend membership - reminder should NOT go out.
743 $this->callAPISuccess('membership', 'create', array('id' => $membership->id, 'end_date' => '2014-01-01'));
744 $this->assertCronRuns(array(
745 array(
746 // After the 2-week mark, send an email.
747 'time' => '2012-09-12 01:00:00',
748 'recipients' => array(),
749 ),
750 ));
751 }
752
753 /**
754 * Test membership end date email sends.
755 *
756 * For contacts/members which match schedule based on end date,
757 * an email should be sent.
758 */
759 public function testMembershipEndDateMatch() {
760 // creates membership with end_date = 20120615
761 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
762 $this->assertTrue(is_numeric($membership->id));
763 $this->callAPISuccess('Email', 'create', array(
764 'contact_id' => $membership->contact_id,
765 'email' => 'test-member@example.com',
766 ));
767 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
768
769 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
770 $actionSchedule['entity_value'] = $membership->membership_type_id;
771 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
772 $this->assertTrue(is_numeric($actionScheduleDao->id));
773
774 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
775 $this->assertCronRuns(array(
776 array(
777 // Before the 2-week mark, no email.
778 'time' => '2012-05-31 01:00:00',
779 // 'time' => '2012-06-01 01:00:00',
780 // FIXME: Is this the right boundary?
781 'recipients' => array(),
782 ),
783 array(
784 // After the 2-week mark, send an email.
785 'time' => '2012-06-01 01:00:00',
786 'recipients' => array(array('test-member@example.com')),
787 ),
788 ));
789
790 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
791 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
792 $membership->end_date = '2012-09-15';
793 $membership->save();
794
795 //change the email id of chosen membership contact to assert
796 //recipient of not the previously sent mail but the new one
797 $result = $this->callAPISuccess('Email', 'create', array(
798 'is_primary' => 1,
799 'contact_id' => $membership->contact_id,
800 'email' => 'member2@example.com',
801 ));
802 $this->assertAPISuccess($result);
803
804 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
805 $this->assertCronRuns(array(
806 array(
807 // Before the 2-week mark, no email
808 'time' => '2012-08-31 01:00:00',
809 'recipients' => array(),
810 ),
811 //array( // After the 2-week mark, send an email
812 //'time' => '2012-09-01 01:00:00',
813 //'recipients' => array(array('member2@example.com')),
814 //),
815 ));
816 }
817
818
819 /**
820 * Test membership end date email.
821 *
822 * For contacts/members which match schedule based on end date,
823 * an email should be sent.
824 */
825 public function testMembershipEndDateNoMatch() {
826 // creates membership with end_date = 20120615
827 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 3)));
828 $this->assertTrue(is_numeric($membership->id));
829 $result = $this->callAPISuccess('Email', 'create', array(
830 'contact_id' => $membership->contact_id,
831 'email' => 'test-member@example.com',
832 ));
833 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
834
835 $actionSchedule = $this->fixtures['sched_membership_end_2month'];
836 $actionSchedule['entity_value'] = $membership->membership_type_id;
837 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
838 $this->assertTrue(is_numeric($actionScheduleDao->id));
839
840 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
841 $this->assertCronRuns(array(
842 array(
843 // Before the 2-week mark, no email.
844 'time' => '2012-05-31 01:00:00',
845 // 'time' => '2012-06-01 01:00:00',
846 // FIXME: Is this the right boundary?
847 'recipients' => array(),
848 ),
849 array(
850 // After the 2-week mark, send an email.
851 'time' => '2013-05-01 01:00:00',
852 'recipients' => array(),
853 ),
854 ));
855 }
856
857 public function testContactBirthDateNoAnniv() {
858 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
859 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
860 $actionSchedule = $this->fixtures['sched_contact_bday_yesterday'];
861 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
862 $this->assertTrue(is_numeric($actionScheduleDao->id));
863 $this->assertCronRuns(array(
864 array(
865 // On the birthday, no email.
866 'time' => '2005-07-07 01:00:00',
867 'recipients' => array(),
868 ),
869 array(
870 // The next day, send an email.
871 'time' => '2005-07-08 20:00:00',
872 'recipients' => array(array('test-bday@example.com')),
873 ),
874 ));
875 }
876
877 public function testContactBirthDateAnniversary() {
878 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
879 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
880 $actionSchedule = $this->fixtures['sched_contact_bday_anniv'];
881 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
882 $this->assertTrue(is_numeric($actionScheduleDao->id));
883 $this->assertCronRuns(array(
884 array(
885 // On some random day, no email.
886 'time' => '2014-03-07 01:00:00',
887 'recipients' => array(),
888 ),
889 array(
890 // On the eve of their 9th birthday, send an email.
891 'time' => '2014-07-06 20:00:00',
892 'recipients' => array(array('test-bday@example.com')),
893 ),
894 ));
895 }
896
897 public function testContactCustomDateNoAnniv() {
898 $group = array(
899 'title' => 'Test_Group',
900 'name' => 'test_group',
901 'extends' => array('Individual'),
902 'style' => 'Inline',
903 'is_multiple' => FALSE,
904 'is_active' => 1,
905 );
906 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
907 $field = array(
908 'label' => 'Graduation',
909 'data_type' => 'Date',
910 'html_type' => 'Select Date',
911 'custom_group_id' => $createGroup['id'],
912 );
913 $createField = $this->callAPISuccess('custom_field', 'create', $field);
914 $contactParams = $this->fixtures['contact'];
915 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
916 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
917 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
918 $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow'];
919 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
920 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
921 $this->assertTrue(is_numeric($actionScheduleDao->id));
922 $this->assertCronRuns(array(
923 array(
924 // On some random day, no email.
925 'time' => '2014-03-07 01:00:00',
926 'recipients' => array(),
927 ),
928 array(
929 // On the eve of their graduation, send an email.
930 'time' => '2013-12-15 20:00:00',
931 'recipients' => array(array('test-member@example.com')),
932 ),
933 ));
934 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
935 }
936
937 public function testContactCreatedNoAnniv() {
938 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
939 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
940 $actionSchedule = $this->fixtures['sched_contact_created_yesterday'];
941 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
942 $this->assertTrue(is_numeric($actionScheduleDao->id));
943 $this->assertCronRuns(array(
944 array(
945 // On the date created, no email.
946 'time' => $contact['values'][$contact['id']]['created_date'],
947 'recipients' => array(),
948 ),
949 array(
950 // The next day, send an email.
951 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
952 'recipients' => array(array('test-bday@example.com')),
953 ),
954 ));
955 }
956
957 public function testContactModifiedAnniversary() {
958 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
959 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
960 $actionSchedule = $this->fixtures['sched_contact_mod_anniv'];
961 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
962 $this->assertTrue(is_numeric($actionScheduleDao->id));
963 $this->assertCronRuns(array(
964 array(
965 // On some random day, no email.
966 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
967 'recipients' => array(),
968 ),
969 array(
970 // On the eve of 3 years after they were modified, send an email.
971 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' +3 years -23 hours')),
972 'recipients' => array(array('test-bday@example.com')),
973 ),
974 ));
975 }
976
977 /**
978 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
979 */
980 public function testMembershipLimitToNone() {
981 // creates membership with end_date = 20120615
982 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
983
984 $this->assertTrue(is_numeric($membership->id));
985 $result = $this->callAPISuccess('Email', 'create', array(
986 'contact_id' => $membership->contact_id,
987 'email' => 'member@example.com',
988 ));
989 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
990 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
991
992 $this->assertAPISuccess($result);
993
994 $actionSchedule = $this->fixtures['sched_membership_end_limit_to_none'];
995 $actionSchedule['entity_value'] = $membership->membership_type_id;
996 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
997 $this->assertTrue(is_numeric($actionScheduleDao->id));
998
999 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1000 $this->assertCronRuns(array(
1001 array(
1002 // Before the 2-week mark, no email.
1003 'time' => '2012-05-31 01:00:00',
1004 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1005 'recipients' => array(),
1006 ),
1007 ));
1008 }
1009
1010 public function testMembership_referenceDate() {
1011 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1012
1013 $this->assertTrue(is_numeric($membership->id));
1014 $result = $this->callAPISuccess('Email', 'create', array(
1015 'contact_id' => $membership->contact_id,
1016 'email' => 'member@example.com',
1017 ));
1018
1019 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1020 $this->assertAPISuccess($result);
1021
1022 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
1023 $actionSchedule['entity_value'] = $membership->membership_type_id;
1024 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1025 $this->assertTrue(is_numeric($actionScheduleDao->id));
1026
1027 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1028 $this->assertCronRuns(array(
1029 array(
1030 // After the 2-week mark, send an email
1031 'time' => '2012-03-29 01:00:00',
1032 'recipients' => array(array('member@example.com')),
1033 ),
1034 array(
1035 // After the 2-week 1day mark, don't send an email
1036 'time' => '2012-03-30 01:00:00',
1037 'recipients' => array(),
1038 ),
1039 ));
1040
1041 //check if reference date is set to membership's join date
1042 //as per the action_start_date chosen for current schedule reminder
1043 $this->assertEquals('2012-03-15',
1044 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id')
1045 );
1046
1047 //change current membership join date that may signifies as membership renewal activity
1048 $membership->join_date = '2012-03-29';
1049 $membership->save();
1050
1051 $this->assertCronRuns(array(
1052 array(
1053 // After the 13 days of the changed join date 2012-03-29, don't send an email
1054 'time' => '2012-04-11 01:00:00',
1055 'recipients' => array(),
1056 ),
1057 array(
1058 // After the 2-week of the changed join date 2012-03-29, send an email
1059 'time' => '2012-04-12 01:00:00',
1060 'recipients' => array(array('member@example.com')),
1061 ),
1062 ));
1063 }
1064
1065 public function testMembershipOnMultipleReminder() {
1066 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1067
1068 $this->assertTrue(is_numeric($membership->id));
1069 $result = $this->callAPISuccess('Email', 'create', array(
1070 'contact_id' => $membership->contact_id,
1071 'email' => 'member@example.com',
1072 ));
1073 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1074 $this->assertAPISuccess($result);
1075
1076 $actionScheduleBefore = $this->fixtures['sched_membership_end_2week']; // Send email 2 weeks before end_date
1077 $actionScheduleOn = $this->fixtures['sched_on_membership_end_date']; // Send email on end_date/expiry date
1078 $actionScheduleAfter = $this->fixtures['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
1079 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id;
1080 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
1081 $$value = CRM_Core_BAO_ActionSchedule::add($$value);
1082 $this->assertTrue(is_numeric($$value->id));
1083 }
1084
1085 $this->assertCronRuns(
1086 array(
1087 array(
1088 // 1day 2weeks before membership end date(MED), don't send mail
1089 'time' => '2012-05-31 01:00:00',
1090 'recipients' => array(),
1091 ),
1092 array(
1093 // 2 weeks before MED, send an email
1094 'time' => '2012-06-01 01:00:00',
1095 'recipients' => array(array('member@example.com')),
1096 ),
1097 array(
1098 // 1day before MED, don't send mail
1099 'time' => '2012-06-14 01:00:00',
1100 'recipients' => array(),
1101 ),
1102 array(
1103 // On MED, send an email
1104 'time' => '2012-06-15 00:00:00',
1105 'recipients' => array(array('member@example.com')),
1106 ),
1107 array(
1108 // After 1day of MED, send an email
1109 'time' => '2012-06-16 01:00:00',
1110 'recipients' => array(array('member@example.com')),
1111 ),
1112 array(
1113 // After 1day 1min of MED, don't send an email
1114 'time' => '2012-06-17 00:01:00',
1115 'recipients' => array(),
1116 ),
1117 )
1118 );
1119
1120 // Assert the timestamp as of when the emails of respective three reminders as configured
1121 // 2 weeks before, on and 1 day after MED, are sent
1122 $this->assertEquals('2012-06-01 01:00:00',
1123 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id, 'action_date_time', 'action_schedule_id', TRUE));
1124 $this->assertEquals('2012-06-15 00:00:00',
1125 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id, 'action_date_time', 'action_schedule_id', TRUE));
1126 $this->assertEquals('2012-06-16 01:00:00',
1127 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id, 'action_date_time', 'action_schedule_id', TRUE));
1128
1129 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1130 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1131 $membership->end_date = '2012-06-20';
1132 $membership->save();
1133
1134 $result = $this->callAPISuccess('Contact', 'get', array('id' => $membership->contact_id));
1135 $this->assertCronRuns(
1136 array(
1137 array(
1138 // 1day 2weeks before membership end date(MED), don't send mail
1139 'time' => '2012-06-05 01:00:00',
1140 'recipients' => array(),
1141 ),
1142 array(
1143 // 2 weeks before MED, send an email
1144 'time' => '2012-06-06 01:00:00',
1145 'recipients' => array(array('member@example.com')),
1146 ),
1147 array(
1148 // 1day before MED, don't send mail
1149 'time' => '2012-06-19 01:00:00',
1150 'recipients' => array(),
1151 ),
1152 array(
1153 // On MED, send an email
1154 'time' => '2012-06-20 00:00:00',
1155 'recipients' => array(array('member@example.com')),
1156 ),
1157 array(
1158 // After 1day of MED, send an email
1159 'time' => '2012-06-21 01:00:00',
1160 'recipients' => array(array('member@example.com')),
1161 ),
1162 array(
1163 // After 1day 1min of MED, don't send an email
1164 'time' => '2012-07-21 00:01:00',
1165 'recipients' => array(),
1166 ),
1167 ));
1168 }
1169
1170 public function testContactCustomDate_Anniv() {
1171 $group = array(
1172 'title' => 'Test_Group now',
1173 'name' => 'test_group_now',
1174 'extends' => array('Individual'),
1175 'style' => 'Inline',
1176 'is_multiple' => FALSE,
1177 'is_active' => 1,
1178 );
1179 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1180 $field = array(
1181 'label' => 'Graduation',
1182 'data_type' => 'Date',
1183 'html_type' => 'Select Date',
1184 'custom_group_id' => $createGroup['id'],
1185 );
1186 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1187
1188 $contactParams = $this->fixtures['contact'];
1189 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1190 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1191 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1192 $actionSchedule = $this->fixtures['sched_contact_grad_anniv'];
1193 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1194 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1195 $this->assertTrue(is_numeric($actionScheduleDao->id));
1196 $this->assertCronRuns(array(
1197 array(
1198 // On some random day, no email.
1199 'time' => '2014-03-07 01:00:00',
1200 'recipients' => array(),
1201 ),
1202 array(
1203 // A week after their 5th anniversary of graduation, send an email.
1204 'time' => '2018-12-23 20:00:00',
1205 'recipients' => array(array('test-member@example.com')),
1206 ),
1207 ));
1208 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1209 }
1210
1211 // TODO // function testMembershipEndDate_NonMatch() { }
1212 // TODO // function testEventTypeStartDate_Match() { }
1213 // TODO // function testEventTypeEndDate_Match() { }
1214 // TODO // function testEventNameStartDate_Match() { }
1215 // TODO // function testEventNameEndDate_Match() { }
1216
1217 /**
1218 * Run a series of cron jobs and make an assertion about email deliveries.
1219 *
1220 * @param array $cronRuns
1221 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1222 * - time: string, e.g. '2012-06-15 21:00:01'
1223 * - recipients: array(array(string)), list of email addresses which should receive messages
1224 */
1225 public function assertCronRuns($cronRuns) {
1226 foreach ($cronRuns as $cronRun) {
1227 CRM_Utils_Time::setTime($cronRun['time']);
1228 $this->callAPISuccess('job', 'send_reminder', array());
1229 $this->mut->assertRecipients($cronRun['recipients']);
1230 if (array_key_exists('subjects', $cronRun)) {
1231 $this->mut->assertSubjects($cronRun['subjects']);
1232 }
1233 $this->mut->clearMessages();
1234 }
1235 }
1236
1237 /**
1238 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1239 */
1240 private $_testObjects;
1241
1242 /**
1243 * Sets up the fixture, for example, opens a network connection.
1244 *
1245 * This method is called before a test is executed.
1246 */
1247 protected function _setUp() {
1248 $this->_testObjects = array();
1249 }
1250
1251 /**
1252 * Tears down the fixture, for example, closes a network connection.
1253 *
1254 * This method is called after a test is executed.
1255 */
1256 protected function _tearDown() {
1257 parent::tearDown();
1258 $this->deleteTestObjects();
1259 }
1260
1261 /**
1262 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
1263 * created entities and provides for brainless cleanup.
1264 *
1265 * @see CRM_Core_DAO::createTestObject
1266 *
1267 * @param $daoName
1268 * @param array $params
1269 * @param int $numObjects
1270 * @param bool $createOnly
1271 *
1272 * @return array|NULL|object
1273 */
1274 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
1275 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
1276 if (is_array($objects)) {
1277 $this->registerTestObjects($objects);
1278 }
1279 else {
1280 $this->registerTestObjects(array($objects));
1281 }
1282 return $objects;
1283 }
1284
1285 /**
1286 * @param array $objects
1287 * DAO or BAO objects.
1288 */
1289 public function registerTestObjects($objects) {
1290 //if (is_object($objects)) {
1291 // $objects = array($objects);
1292 //}
1293 foreach ($objects as $object) {
1294 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1295 $this->_testObjects[$daoName][] = $object->id;
1296 }
1297 }
1298
1299 public function deleteTestObjects() {
1300 // Note: You might argue that the FK relations between test
1301 // objects could make this problematic; however, it should
1302 // behave intuitively as long as we mentally split our
1303 // test-objects between the "manual/primary records"
1304 // and the "automatic/secondary records"
1305 foreach ($this->_testObjects as $daoName => $daoIds) {
1306 foreach ($daoIds as $daoId) {
1307 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
1308 }
1309 }
1310 $this->_testObjects = array();
1311 }
1312
1313 }