commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / CRM / Core / BAO / ActionScheduleTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 object see CiviTest/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)',
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)',
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',
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 ),
549 array(
550 // After the 24-hour mark, an email
551 'time' => '2012-06-14 15:00:00',
552 'recipients' => array(array('test-member@example.com')),
553 ),
554 array(
555 // Run cron again; message already sent
556 'time' => '',
557 'recipients' => array(),
558 ),
559 ));
560 }
561
562 public function testActivityDateTimeMatchRepeatableSchedule() {
563 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day_r']);
564 $this->assertTrue(is_numeric($actionScheduleDao->id));
565
566 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
567 $this->assertTrue(is_numeric($activity->id));
568 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
569 $activity->save();
570
571 $source['contact_id'] = $contact['id'];
572 $source['activity_id'] = $activity->id;
573 $source['record_type_id'] = 2;
574 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
575 $activityContact->save();
576
577 $this->assertCronRuns(array(
578 array(
579 // Before the 24-hour mark, no email
580 'time' => '012-06-14 04:00:00',
581 'recipients' => array(),
582 ),
583 array(
584 // After the 24-hour mark, an email
585 'time' => '2012-06-14 15:00:00',
586 'recipients' => array(array('test-member@example.com')),
587 ),
588 array(
589 // Run cron 4 hours later; first message already sent
590 'time' => '2012-06-14 20:00:00',
591 'recipients' => array(),
592 ),
593 array(
594 // Run cron 6 hours later; send second message.
595 'time' => '2012-06-14 21:00:01',
596 'recipients' => array(array('test-member@example.com')),
597 ),
598 ));
599 }
600
601 /**
602 * For contacts/activities which don't match the schedule filter,
603 * an email should *not* be sent.
604 */
605 // TODO // function testActivityDateTime_NonMatch() { }
606
607 /**
608 * For contacts/members which match schedule based on join date,
609 * an email should be sent.
610 */
611 public function testMembershipJoinDateMatch() {
612 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1)));
613 $this->assertTrue(is_numeric($membership->id));
614 $result = $this->callAPISuccess('Email', 'create', array(
615 'contact_id' => $membership->contact_id,
616 'email' => 'test-member@example.com',
617 'location_type_id' => 1,
618 ));
619 $this->assertAPISuccess($result);
620
621 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
622 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
623 $actionSchedule['entity_value'] = $membership->membership_type_id;
624 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
625 $this->assertTrue(is_numeric($actionScheduleDao->id));
626
627 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
628 $this->assertCronRuns(array(
629 array(
630 // Before the 2-week mark, no email.
631 'time' => '2012-03-28 01:00:00',
632 'recipients' => array(),
633 ),
634 array(
635 // After the 2-week mark, send an email.
636 'time' => '2012-03-29 01:00:00',
637 'recipients' => array(array('test-member@example.com')),
638 ),
639 ));
640 }
641
642 /**
643 * Test end date email sent.
644 *
645 * For contacts/members which match schedule based on join date,
646 * an email should be sent.
647 */
648 public function testMembershipJoinDateNonMatch() {
649 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures['rolling_membership']);
650 $this->assertTrue(is_numeric($membership->id));
651 $result = $this->callAPISuccess('Email', 'create', array(
652 'contact_id' => $membership->contact_id,
653 'location_type_id' => 1,
654 'email' => 'test-member@example.com',
655 ));
656
657 // Add an alternative membership type, and only send messages for that type
658 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
659 $this->assertTrue(is_numeric($extraMembershipType->id));
660 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_membership_join_2week']);
661 $this->assertTrue(is_numeric($actionScheduleDao->id));
662 $actionScheduleDao->entity_value = $extraMembershipType->id;
663 $actionScheduleDao->save();
664
665 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
666 $this->assertCronRuns(array(
667 array(
668 // After the 2-week mark, don't send email because we have different membership type.
669 'time' => '2012-03-29 01:00:00',
670 'recipients' => array(),
671 ),
672 ));
673 }
674
675 /**
676 * Test that the first and SECOND notifications are sent out.
677 */
678 public function testMembershipEndDateRepeat() {
679 // creates membership with end_date = 20120615
680 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
681 $result = $this->callAPISuccess('Email', 'create', array(
682 'contact_id' => $membership->contact_id,
683 'email' => 'test-member@example.com',
684 ));
685 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
686
687 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
688 $actionSchedule['entity_value'] = $membership->membership_type_id;
689 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
690
691 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
692 $this->assertCronRuns(array(
693 array(
694 // After the 2-week mark, send an email.
695 'time' => '2012-08-15 01:00:00',
696 'recipients' => array(array('test-member@example.com')),
697 ),
698 array(
699 // After the 2-week mark, send an email.
700 'time' => '2012-09-12 01:00:00',
701 'recipients' => array(array('test-member@example.com')),
702 ),
703 ));
704 }
705
706 /**
707 * Test behaviour when date changes.
708 *
709 * Test that the first notification is sent but the second is NOT sent if the end date changes in
710 * between
711 * see CRM-15376
712 */
713 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
714 // creates membership with end_date = 20120615
715 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
716 $this->callAPISuccess('Email', 'create', array(
717 'contact_id' => $membership->contact_id,
718 'email' => 'test-member@example.com',
719 ));
720 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
721
722 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
723 $actionSchedule['entity_value'] = $membership->membership_type_id;
724 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
725 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
726 $this->assertCronRuns(array(
727 array(
728 // After the 2-week mark, send an email.
729 'time' => '2012-08-15 01:00:00',
730 'recipients' => array(array('test-member@example.com')),
731 ),
732 ));
733
734 // Extend membership - reminder should NOT go out.
735 $this->callAPISuccess('membership', 'create', array('id' => $membership->id, 'end_date' => '2014-01-01'));
736 $this->assertCronRuns(array(
737 array(
738 // After the 2-week mark, send an email.
739 'time' => '2012-09-12 01:00:00',
740 'recipients' => array(),
741 ),
742 ));
743 }
744
745 /**
746 * Test membership end date email sends.
747 *
748 * For contacts/members which match schedule based on end date,
749 * an email should be sent.
750 */
751 public function testMembershipEndDateMatch() {
752 // creates membership with end_date = 20120615
753 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
754 $this->assertTrue(is_numeric($membership->id));
755 $this->callAPISuccess('Email', 'create', array(
756 'contact_id' => $membership->contact_id,
757 'email' => 'test-member@example.com',
758 ));
759 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
760
761 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
762 $actionSchedule['entity_value'] = $membership->membership_type_id;
763 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
764 $this->assertTrue(is_numeric($actionScheduleDao->id));
765
766 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
767 $this->assertCronRuns(array(
768 array(
769 // Before the 2-week mark, no email.
770 'time' => '2012-05-31 01:00:00',
771 // 'time' => '2012-06-01 01:00:00',
772 // FIXME: Is this the right boundary?
773 'recipients' => array(),
774 ),
775 array(
776 // After the 2-week mark, send an email.
777 'time' => '2012-06-01 01:00:00',
778 'recipients' => array(array('test-member@example.com')),
779 ),
780 ));
781
782 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
783 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
784 $membership->end_date = '2012-09-15';
785 $membership->save();
786
787 //change the email id of chosen membership contact to assert
788 //recipient of not the previously sent mail but the new one
789 $result = $this->callAPISuccess('Email', 'create', array(
790 'is_primary' => 1,
791 'contact_id' => $membership->contact_id,
792 'email' => 'member2@example.com',
793 ));
794 $this->assertAPISuccess($result);
795
796 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
797 $this->assertCronRuns(array(
798 array(
799 // Before the 2-week mark, no email
800 'time' => '2012-08-31 01:00:00',
801 'recipients' => array(),
802 ),
803 //array( // After the 2-week mark, send an email
804 //'time' => '2012-09-01 01:00:00',
805 //'recipients' => array(array('member2@example.com')),
806 //),
807 ));
808 }
809
810
811 /**
812 * Test membership end date email.
813 *
814 * For contacts/members which match schedule based on end date,
815 * an email should be sent.
816 */
817 public function testMembershipEndDateNoMatch() {
818 // creates membership with end_date = 20120615
819 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 3)));
820 $this->assertTrue(is_numeric($membership->id));
821 $result = $this->callAPISuccess('Email', 'create', array(
822 'contact_id' => $membership->contact_id,
823 'email' => 'test-member@example.com',
824 ));
825 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
826
827 $actionSchedule = $this->fixtures['sched_membership_end_2month'];
828 $actionSchedule['entity_value'] = $membership->membership_type_id;
829 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
830 $this->assertTrue(is_numeric($actionScheduleDao->id));
831
832 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
833 $this->assertCronRuns(array(
834 array(
835 // Before the 2-week mark, no email.
836 'time' => '2012-05-31 01:00:00',
837 // 'time' => '2012-06-01 01:00:00',
838 // FIXME: Is this the right boundary?
839 'recipients' => array(),
840 ),
841 array(
842 // After the 2-week mark, send an email.
843 'time' => '2013-05-01 01:00:00',
844 'recipients' => array(),
845 ),
846 ));
847 }
848
849 public function testContactBirthDateNoAnniv() {
850 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
851 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
852 $actionSchedule = $this->fixtures['sched_contact_bday_yesterday'];
853 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
854 $this->assertTrue(is_numeric($actionScheduleDao->id));
855 $this->assertCronRuns(array(
856 array(
857 // On the birthday, no email.
858 'time' => '2005-07-07 01:00:00',
859 'recipients' => array(),
860 ),
861 array(
862 // The next day, send an email.
863 'time' => '2005-07-08 20:00:00',
864 'recipients' => array(array('test-bday@example.com')),
865 ),
866 ));
867 }
868
869 public function testContactBirthDateAnniversary() {
870 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
871 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
872 $actionSchedule = $this->fixtures['sched_contact_bday_anniv'];
873 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
874 $this->assertTrue(is_numeric($actionScheduleDao->id));
875 $this->assertCronRuns(array(
876 array(
877 // On some random day, no email.
878 'time' => '2014-03-07 01:00:00',
879 'recipients' => array(),
880 ),
881 array(
882 // On the eve of their 9th birthday, send an email.
883 'time' => '2014-07-06 20:00:00',
884 'recipients' => array(array('test-bday@example.com')),
885 ),
886 ));
887 }
888
889 public function testContactCustomDateNoAnniv() {
890 $group = array(
891 'title' => 'Test_Group',
892 'name' => 'test_group',
893 'extends' => array('Individual'),
894 'style' => 'Inline',
895 'is_multiple' => FALSE,
896 'is_active' => 1,
897 );
898 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
899 $field = array(
900 'label' => 'Graduation',
901 'data_type' => 'Date',
902 'html_type' => 'Select Date',
903 'custom_group_id' => $createGroup['id'],
904 );
905 $createField = $this->callAPISuccess('custom_field', 'create', $field);
906 $contactParams = $this->fixtures['contact'];
907 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
908 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
909 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
910 $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow'];
911 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
912 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
913 $this->assertTrue(is_numeric($actionScheduleDao->id));
914 $this->assertCronRuns(array(
915 array(
916 // On some random day, no email.
917 'time' => '2014-03-07 01:00:00',
918 'recipients' => array(),
919 ),
920 array(
921 // On the eve of their graduation, send an email.
922 'time' => '2013-12-15 20:00:00',
923 'recipients' => array(array('test-member@example.com')),
924 ),
925 ));
926 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
927 }
928
929 public function testContactCreatedNoAnniv() {
930 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
931 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
932 $actionSchedule = $this->fixtures['sched_contact_created_yesterday'];
933 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
934 $this->assertTrue(is_numeric($actionScheduleDao->id));
935 $this->assertCronRuns(array(
936 array(
937 // On the date created, no email.
938 'time' => $contact['values'][$contact['id']]['created_date'],
939 'recipients' => array(),
940 ),
941 array(
942 // The next day, send an email.
943 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
944 'recipients' => array(array('test-bday@example.com')),
945 ),
946 ));
947 }
948
949 public function testContactModifiedAnniversary() {
950 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
951 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
952 $actionSchedule = $this->fixtures['sched_contact_mod_anniv'];
953 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
954 $this->assertTrue(is_numeric($actionScheduleDao->id));
955 $this->assertCronRuns(array(
956 array(
957 // On some random day, no email.
958 'time' => '2014-03-07 01:00:00',
959 'recipients' => array(),
960 ),
961 array(
962 // On the eve of 3 years after they were modified, send an email.
963 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' +3 years -1 day')),
964 'recipients' => array(array('test-bday@example.com')),
965 ),
966 ));
967 }
968
969 /**
970 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
971 */
972 public function testMembershipLimitToNone() {
973 // creates membership with end_date = 20120615
974 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
975
976 $this->assertTrue(is_numeric($membership->id));
977 $result = $this->callAPISuccess('Email', 'create', array(
978 'contact_id' => $membership->contact_id,
979 'email' => 'member@example.com',
980 ));
981 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
982 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
983
984 $this->assertAPISuccess($result);
985
986 $actionSchedule = $this->fixtures['sched_membership_end_limit_to_none'];
987 $actionSchedule['entity_value'] = $membership->membership_type_id;
988 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
989 $this->assertTrue(is_numeric($actionScheduleDao->id));
990
991 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
992 $this->assertCronRuns(array(
993 array(
994 // Before the 2-week mark, no email.
995 'time' => '2012-05-31 01:00:00',
996 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
997 'recipients' => array(),
998 ),
999 ));
1000 }
1001
1002 public function testMembership_referenceDate() {
1003 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1004
1005 $this->assertTrue(is_numeric($membership->id));
1006 $this->callAPISuccess('Email', 'create', array(
1007 'contact_id' => $membership->contact_id,
1008 'email' => 'member@example.com',
1009 ));
1010
1011 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1012 $this->assertAPISuccess($result);
1013
1014 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
1015 $actionSchedule['entity_value'] = $membership->membership_type_id;
1016 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1017 $this->assertTrue(is_numeric($actionScheduleDao->id));
1018
1019 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1020 $this->assertCronRuns(array(
1021 array(
1022 // After the 2-week mark, send an email
1023 'time' => '2012-03-29 01:00:00',
1024 'recipients' => array(array('member@example.com')),
1025 ),
1026 array(
1027 // After the 2-week 1day mark, don't send an email
1028 'time' => '2012-03-30 01:00:00',
1029 'recipients' => array(),
1030 ),
1031 ));
1032
1033 //check if reference date is set to membership's join date
1034 //as per the action_start_date chosen for current schedule reminder
1035 $this->assertEquals('2012-03-15',
1036 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id')
1037 );
1038
1039 //change current membership join date that may signifies as memberhip renewal activity
1040 $membership->join_date = '2012-03-29';
1041 $membership->save();
1042
1043 $this->assertCronRuns(array(
1044 array(
1045 // After the 13 days of the changed join date 2012-03-29, don't send an email
1046 'time' => '2012-04-11 01:00:00',
1047 'recipients' => array(),
1048 ),
1049 array(
1050 // After the 2-week of the changed join date 2012-03-29, send an email
1051 'time' => '2012-04-12 01:00:00',
1052 'recipients' => array(array('member@example.com')),
1053 ),
1054 ));
1055 $this->assertCronRuns(array(
1056 array(
1057 // It should not re-send on the same day
1058 'time' => '2012-04-12 01:00:00',
1059 'recipients' => array(),
1060 ),
1061 ));
1062 }
1063
1064 public function testMembershipOnMultipleReminder() {
1065 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1066
1067 $this->assertTrue(is_numeric($membership->id));
1068 $result = $this->callAPISuccess('Email', 'create', array(
1069 'contact_id' => $membership->contact_id,
1070 'email' => 'member@example.com',
1071 ));
1072 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1073 $this->assertAPISuccess($result);
1074
1075 $actionScheduleBefore = $this->fixtures['sched_membership_end_2week']; // Send email 2 weeks before end_date
1076 $actionScheduleOn = $this->fixtures['sched_on_membership_end_date']; // Send email on end_date/expiry date
1077 $actionScheduleAfter = $this->fixtures['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
1078 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id;
1079 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
1080 $$value = CRM_Core_BAO_ActionSchedule::add($$value);
1081 $this->assertTrue(is_numeric($$value->id));
1082 }
1083
1084 $this->assertCronRuns(
1085 array(
1086 array(
1087 // 1day 2weeks before membership end date(MED), don't send mail
1088 'time' => '2012-05-31 01:00:00',
1089 'recipients' => array(),
1090 ),
1091 array(
1092 // 2 weeks before MED, send an email
1093 'time' => '2012-06-01 01:00:00',
1094 'recipients' => array(array('member@example.com')),
1095 ),
1096 array(
1097 // 1day before MED, don't send mail
1098 'time' => '2012-06-14 01:00:00',
1099 'recipients' => array(),
1100 ),
1101 array(
1102 // On MED, send an email
1103 'time' => '2012-06-15 00:00:00',
1104 'recipients' => array(array('member@example.com')),
1105 ),
1106 array(
1107 // After 1day of MED, send an email
1108 'time' => '2012-06-16 01:00:00',
1109 'recipients' => array(array('member@example.com')),
1110 ),
1111 array(
1112 // After 1day 1min of MED, don't send an email
1113 'time' => '2012-06-17 00:01:00',
1114 'recipients' => array(),
1115 ),
1116 )
1117 );
1118
1119 // Assert the timestamp as of when the emails of respective three reminders as configured
1120 // 2 weeks before, on and 1 day after MED, are sent
1121 $this->assertEquals('2012-06-01 01:00:00',
1122 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id, 'action_date_time', 'action_schedule_id', TRUE));
1123 $this->assertEquals('2012-06-15 00:00:00',
1124 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id, 'action_date_time', 'action_schedule_id', TRUE));
1125 $this->assertEquals('2012-06-16 01:00:00',
1126 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id, 'action_date_time', 'action_schedule_id', TRUE));
1127
1128 //extend MED to 2 weeks after the current MED (that may signifies as memberhip renewal activity)
1129 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1130 $membership->end_date = '2012-06-20';
1131 $membership->save();
1132
1133 $result = $this->callAPISuccess('Contact', 'get', array('id' => $membership->contact_id));
1134 $this->assertCronRuns(
1135 array(
1136 array(
1137 // 1day 2weeks before membership end date(MED), don't send mail
1138 'time' => '2012-06-05 01:00:00',
1139 'recipients' => array(),
1140 ),
1141 array(
1142 // 2 weeks before MED, send an email
1143 'time' => '2012-06-06 01:00:00',
1144 'recipients' => array(array('member@example.com')),
1145 ),
1146 array(
1147 // 1day before MED, don't send mail
1148 'time' => '2012-06-19 01:00:00',
1149 'recipients' => array(),
1150 ),
1151 array(
1152 // On MED, send an email
1153 'time' => '2012-06-20 00:00:00',
1154 'recipients' => array(array('member@example.com')),
1155 ),
1156 array(
1157 // After 1day of MED, send an email
1158 'time' => '2012-06-21 01:00:00',
1159 'recipients' => array(array('member@example.com')),
1160 ),
1161 array(
1162 // After 1day 1min of MED, don't send an email
1163 'time' => '2012-07-21 00:01:00',
1164 'recipients' => array(),
1165 ),
1166 ));
1167 }
1168
1169 public function testContactCustomDate_Anniv() {
1170 $group = array(
1171 'title' => 'Test_Group now',
1172 'name' => 'test_group_now',
1173 'extends' => array('Individual'),
1174 'style' => 'Inline',
1175 'is_multiple' => FALSE,
1176 'is_active' => 1,
1177 );
1178 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1179 $field = array(
1180 'label' => 'Graduation',
1181 'data_type' => 'Date',
1182 'html_type' => 'Select Date',
1183 'custom_group_id' => $createGroup['id'],
1184 );
1185 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1186
1187 $contactParams = $this->fixtures['contact'];
1188 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1189 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1190 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1191 $actionSchedule = $this->fixtures['sched_contact_grad_anniv'];
1192 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1193 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1194 $this->assertTrue(is_numeric($actionScheduleDao->id));
1195 $this->assertCronRuns(array(
1196 array(
1197 // On some random day, no email.
1198 'time' => '2014-03-07 01:00:00',
1199 'recipients' => array(),
1200 ),
1201 array(
1202 // A week after their 5th anniversary of graduation, send an email.
1203 'time' => '2018-12-23 20:00:00',
1204 'recipients' => array(array('test-member@example.com')),
1205 ),
1206 ));
1207 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1208 }
1209
1210 // TODO // function testMembershipEndDate_NonMatch() { }
1211 // TODO // function testEventTypeStartDate_Match() { }
1212 // TODO // function testEventTypeEndDate_Match() { }
1213 // TODO // function testEventNameStartDate_Match() { }
1214 // TODO // function testEventNameEndDate_Match() { }
1215
1216 /**
1217 * Run a series of cron jobs and make an assertion about email deliveries.
1218 *
1219 * @param array $cronRuns
1220 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1221 * - time: string, e.g. '2012-06-15 21:00:01'
1222 * - recipients: array(array(string)), list of email addresses which should receive messages
1223 */
1224 public function assertCronRuns($cronRuns) {
1225 foreach ($cronRuns as $cronRun) {
1226 CRM_Utils_Time::setTime($cronRun['time']);
1227 $this->callAPISuccess('job', 'send_reminder', array());
1228 $this->mut->assertRecipients($cronRun['recipients']);
1229 $this->mut->clearMessages();
1230 }
1231 }
1232
1233 /**
1234 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1235 */
1236 private $_testObjects;
1237
1238 /**
1239 * Sets up the fixture, for example, opens a network connection.
1240 *
1241 * This method is called before a test is executed.
1242 */
1243 protected function _setUp() {
1244 $this->_testObjects = array();
1245 }
1246
1247 /**
1248 * Tears down the fixture, for example, closes a network connection.
1249 *
1250 * This method is called after a test is executed.
1251 */
1252 protected function _tearDown() {
1253 parent::tearDown();
1254 $this->deleteTestObjects();
1255 }
1256
1257 /**
1258 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
1259 * created entities and provides for brainless cleanup.
1260 *
1261 * @see CRM_Core_DAO::createTestObject
1262 *
1263 * @param $daoName
1264 * @param array $params
1265 * @param int $numObjects
1266 * @param bool $createOnly
1267 *
1268 * @return array|NULL|object
1269 */
1270 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
1271 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
1272 if (is_array($objects)) {
1273 $this->registerTestObjects($objects);
1274 }
1275 else {
1276 $this->registerTestObjects(array($objects));
1277 }
1278 return $objects;
1279 }
1280
1281 /**
1282 * @param array $objects
1283 * DAO or BAO objects.
1284 */
1285 public function registerTestObjects($objects) {
1286 //if (is_object($objects)) {
1287 // $objects = array($objects);
1288 //}
1289 foreach ($objects as $object) {
1290 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1291 $this->_testObjects[$daoName][] = $object->id;
1292 }
1293 }
1294
1295 public function deleteTestObjects() {
1296 // Note: You might argue that the FK relations between test
1297 // objects could make this problematic; however, it should
1298 // behave intuitively as long as we mentally split our
1299 // test-objects between the "manual/primary records"
1300 // and the "automatic/secondary records"
1301 foreach ($this->_testObjects as $daoName => $daoIds) {
1302 foreach ($daoIds as $daoId) {
1303 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
1304 }
1305 }
1306 $this->_testObjects = array();
1307 }
1308
1309 }