7e7fb8d6f6a450fa72726ca42a3fdee50069a12d
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / ActionScheduleTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * Class CRM_Core_BAO_ActionScheduleTest
30 * @group ActionSchedule
31 * @group headless
32 *
33 * There are additional tests for some specific entities in other classes:
34 * @see CRM_Activity_ActionMappingTest
35 * @see CRM_Contribute_ActionMapping_ByTypeTest
36 */
37 class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
38
39 /**
40 * @var CiviMailUtils
41 */
42 public $mut;
43
44 public function setUp() {
45 parent::setUp();
46
47 $this->mut = new CiviMailUtils($this, TRUE);
48
49 $this->fixtures['rolling_membership'] = array(
50 'membership_type_id' => array(
51 'period_type' => 'rolling',
52 'duration_unit' => 'month',
53 'duration_interval' => '3',
54 'is_active' => 1,
55 ),
56 'join_date' => '20120315',
57 'start_date' => '20120315',
58 'end_date' => '20120615',
59 'is_override' => 0,
60 );
61
62 $this->fixtures['rolling_membership_past'] = array(
63 'membership_type_id' => array(
64 'period_type' => 'rolling',
65 'duration_unit' => 'month',
66 'duration_interval' => '3',
67 'is_active' => 1,
68 ),
69 'join_date' => '20100310',
70 'start_date' => '20100310',
71 'end_date' => '20100610',
72 'is_override' => 'NULL',
73 );
74 $this->fixtures['participant'] = array(
75 'event_id' => array(
76 'is_active' => 1,
77 'is_template' => 0,
78 'title' => 'Example Event',
79 'start_date' => '20120315',
80 'end_date' => '20120615',
81 ),
82 'role_id' => '1', // Attendee.
83 'status_id' => '8', // No-show.
84 );
85
86 $this->fixtures['phonecall'] = array(
87 'status_id' => 1,
88 'activity_type_id' => 2,
89 'activity_date_time' => '20120615100000',
90 'is_current_revision' => 1,
91 'is_deleted' => 0,
92 );
93 $this->fixtures['contact'] = array(
94 'is_deceased' => 0,
95 'contact_type' => 'Individual',
96 'email' => 'test-member@example.com',
97 'gender_id' => 'Female',
98 'first_name' => 'Churmondleia',
99 'last_name' => 'Ōtākou',
100 );
101 $this->fixtures['contact_birthdate'] = array(
102 'is_deceased' => 0,
103 'contact_type' => 'Individual',
104 'email' => 'test-bday@example.com',
105 'birth_date' => '20050707',
106 );
107 $this->fixtures['sched_activity_1day'] = array(
108 'name' => 'One_Day_Phone_Call_Notice',
109 'title' => 'One Day Phone Call Notice',
110 'limit_to' => '1',
111 'absolute_date' => NULL,
112 'body_html' => '<p>1-Day (non-repeating) (for {activity.subject})</p>',
113 'body_text' => '1-Day (non-repeating) (for {activity.subject})',
114 'end_action' => NULL,
115 'end_date' => NULL,
116 'end_frequency_interval' => NULL,
117 'end_frequency_unit' => NULL,
118 'entity_status' => '1',
119 'entity_value' => '2',
120 'group_id' => NULL,
121 'is_active' => '1',
122 'is_repeat' => '0',
123 'mapping_id' => '1',
124 'msg_template_id' => NULL,
125 'recipient' => '2',
126 'recipient_listing' => NULL,
127 'recipient_manual' => NULL,
128 'record_activity' => 1,
129 'repetition_frequency_interval' => NULL,
130 'repetition_frequency_unit' => NULL,
131 'start_action_condition' => 'before',
132 'start_action_date' => 'activity_date_time',
133 'start_action_offset' => '1',
134 'start_action_unit' => 'day',
135 'subject' => '1-Day (non-repeating) (about {activity.activity_type})',
136 );
137 $this->fixtures['sched_activity_1day_r'] = array(
138 'name' => 'One_Day_Phone_Call_Notice_R',
139 'title' => 'One Day Phone Call Notice R',
140 'limit_to' => 1,
141 'absolute_date' => NULL,
142 'body_html' => '<p>1-Day (repeating)</p>',
143 'body_text' => '1-Day (repeating)',
144 'end_action' => 'after',
145 'end_date' => 'activity_date_time',
146 'end_frequency_interval' => '2',
147 'end_frequency_unit' => 'day',
148 'entity_status' => '1',
149 'entity_value' => '2',
150 'group_id' => NULL,
151 'is_active' => '1',
152 'is_repeat' => '1',
153 'mapping_id' => '1',
154 'msg_template_id' => NULL,
155 'recipient' => '2',
156 'recipient_listing' => NULL,
157 'recipient_manual' => NULL,
158 'record_activity' => NULL,
159 'repetition_frequency_interval' => '6',
160 'repetition_frequency_unit' => 'hour',
161 'start_action_condition' => 'before',
162 'start_action_date' => 'activity_date_time',
163 'start_action_offset' => '1',
164 'start_action_unit' => 'day',
165 'subject' => '1-Day (repeating) (about {activity.activity_type})',
166 );
167 $this->fixtures['sched_activity_1day_r_on_abs_date'] = array(
168 'name' => 'One_Day_Phone_Call_Notice_R',
169 'title' => 'One Day Phone Call Notice R',
170 'limit_to' => 1,
171 'absolute_date' => CRM_Utils_Date::processDate('20120614100000'),
172 'body_html' => '<p>1-Day (repeating)</p>',
173 'body_text' => '1-Day (repeating)',
174 'entity_status' => '1',
175 'entity_value' => '2',
176 'group_id' => NULL,
177 'is_active' => '1',
178 'is_repeat' => '1',
179 'mapping_id' => '1',
180 'msg_template_id' => NULL,
181 'recipient' => '2',
182 'recipient_listing' => NULL,
183 'recipient_manual' => NULL,
184 'record_activity' => NULL,
185 'repetition_frequency_interval' => '6',
186 'repetition_frequency_unit' => 'hour',
187 'end_action' => 'after',
188 'end_date' => 'activity_date_time',
189 'end_frequency_interval' => '2',
190 'end_frequency_unit' => 'day',
191 'start_action_condition' => '',
192 'start_action_date' => '',
193 'start_action_offset' => '',
194 'start_action_unit' => '',
195 'subject' => '1-Day (repeating) (about {activity.activity_type})',
196 );
197 $this->fixtures['sched_membership_join_2week'] = array(
198 'name' => 'sched_membership_join_2week',
199 'title' => 'sched_membership_join_2week',
200 'absolute_date' => '',
201 'body_html' => '<p>body sched_membership_join_2week</p>',
202 'body_text' => 'body sched_membership_join_2week',
203 'end_action' => '',
204 'end_date' => '',
205 'end_frequency_interval' => '',
206 'end_frequency_unit' => '',
207 'entity_status' => '',
208 'entity_value' => '',
209 'group_id' => '',
210 'is_active' => 1,
211 'is_repeat' => '0',
212 'mapping_id' => 4,
213 'msg_template_id' => '',
214 'recipient' => '',
215 'recipient_listing' => '',
216 'recipient_manual' => '',
217 'record_activity' => 1,
218 'repetition_frequency_interval' => '',
219 'repetition_frequency_unit' => '',
220 'start_action_condition' => 'after',
221 'start_action_date' => 'membership_join_date',
222 'start_action_offset' => '2',
223 'start_action_unit' => 'week',
224 'subject' => 'subject sched_membership_join_2week (joined {membership.join_date})',
225 );
226 $this->fixtures['sched_membership_end_2week'] = array(
227 'name' => 'sched_membership_end_2week',
228 'title' => 'sched_membership_end_2week',
229 'absolute_date' => '',
230 'body_html' => '<p>body sched_membership_end_2week</p>',
231 'body_text' => 'body sched_membership_end_2week',
232 'end_action' => '',
233 'end_date' => '',
234 'end_frequency_interval' => '',
235 'end_frequency_unit' => '',
236 'entity_status' => '',
237 'entity_value' => '',
238 'group_id' => '',
239 'is_active' => 1,
240 'is_repeat' => '0',
241 'mapping_id' => 4,
242 'msg_template_id' => '',
243 'recipient' => '',
244 'recipient_listing' => '',
245 'recipient_manual' => '',
246 'record_activity' => 1,
247 'repetition_frequency_interval' => '',
248 'repetition_frequency_unit' => '',
249 'start_action_condition' => 'before',
250 'start_action_date' => 'membership_end_date',
251 'start_action_offset' => '2',
252 'start_action_unit' => 'week',
253 'subject' => 'subject sched_membership_end_2week',
254 );
255 $this->fixtures['sched_on_membership_end_date'] = array(
256 'name' => 'sched_on_membership_end_date',
257 'title' => 'sched_on_membership_end_date',
258 'body_html' => '<p>Your membership expired today</p>',
259 'body_text' => 'Your membership expired today',
260 'is_active' => 1,
261 'mapping_id' => 4,
262 'record_activity' => 1,
263 'start_action_condition' => 'after',
264 'start_action_date' => 'membership_end_date',
265 'start_action_offset' => '0',
266 'start_action_unit' => 'hour',
267 'subject' => 'subject send reminder on membership_end_date',
268 );
269 $this->fixtures['sched_after_1day_membership_end_date'] = array(
270 'name' => 'sched_after_1day_membership_end_date',
271 'title' => 'sched_after_1day_membership_end_date',
272 'body_html' => '<p>Your membership expired yesterday</p>',
273 'body_text' => 'Your membership expired yesterday',
274 'is_active' => 1,
275 'mapping_id' => 4,
276 'record_activity' => 1,
277 'start_action_condition' => 'after',
278 'start_action_date' => 'membership_end_date',
279 'start_action_offset' => '1',
280 'start_action_unit' => 'day',
281 'subject' => 'subject send reminder on membership_end_date',
282 );
283
284 $this->fixtures['sched_membership_end_2month'] = array(
285 'name' => 'sched_membership_end_2month',
286 'title' => 'sched_membership_end_2month',
287 'absolute_date' => '',
288 'body_html' => '<p>body sched_membership_end_2month</p>',
289 'body_text' => 'body sched_membership_end_2month',
290 'end_action' => '',
291 'end_date' => '',
292 'end_frequency_interval' => '',
293 'end_frequency_unit' => '',
294 'entity_status' => '',
295 'entity_value' => '',
296 'group_id' => '',
297 'is_active' => 1,
298 'is_repeat' => '0',
299 'mapping_id' => 4,
300 'msg_template_id' => '',
301 'recipient' => '',
302 'recipient_listing' => '',
303 'recipient_manual' => '',
304 'record_activity' => 1,
305 'repetition_frequency_interval' => '',
306 'repetition_frequency_unit' => '',
307 'start_action_condition' => 'after',
308 'start_action_date' => 'membership_end_date',
309 'start_action_offset' => '2',
310 'start_action_unit' => 'month',
311 'subject' => 'subject sched_membership_end_2month',
312 );
313
314 $this->fixtures['sched_contact_bday_yesterday'] = array(
315 'name' => 'sched_contact_bday_yesterday',
316 'title' => 'sched_contact_bday_yesterday',
317 'absolute_date' => '',
318 'body_html' => '<p>you look like you were born yesterday!</p>',
319 'body_text' => 'you look like you were born yesterday!',
320 'end_action' => '',
321 'end_date' => '',
322 'end_frequency_interval' => '',
323 'end_frequency_unit' => '',
324 'entity_status' => 1,
325 'entity_value' => 'birth_date',
326 'group_id' => '',
327 'is_active' => 1,
328 'is_repeat' => '0',
329 'mapping_id' => 6,
330 'msg_template_id' => '',
331 'recipient' => '',
332 'recipient_listing' => '',
333 'recipient_manual' => '',
334 'record_activity' => 1,
335 'repetition_frequency_interval' => '',
336 'repetition_frequency_unit' => '',
337 'start_action_condition' => 'after',
338 'start_action_date' => 'date_field',
339 'start_action_offset' => '1',
340 'start_action_unit' => 'day',
341 'subject' => 'subject sched_contact_bday_yesterday',
342 );
343
344 $this->fixtures['sched_contact_bday_anniv'] = array(
345 'name' => 'sched_contact_bday_anniv',
346 'title' => 'sched_contact_bday_anniv',
347 'absolute_date' => '',
348 'body_html' => '<p>happy birthday!</p>',
349 'body_text' => 'happy birthday!',
350 'end_action' => '',
351 'end_date' => '',
352 'end_frequency_interval' => '',
353 'end_frequency_unit' => '',
354 'entity_status' => 2,
355 'entity_value' => 'birth_date',
356 'group_id' => '',
357 'is_active' => 1,
358 'is_repeat' => '0',
359 'mapping_id' => 6,
360 'msg_template_id' => '',
361 'recipient' => '',
362 'recipient_listing' => '',
363 'recipient_manual' => '',
364 'record_activity' => 1,
365 'repetition_frequency_interval' => '',
366 'repetition_frequency_unit' => '',
367 'start_action_condition' => 'before',
368 'start_action_date' => 'date_field',
369 'start_action_offset' => '1',
370 'start_action_unit' => 'day',
371 'subject' => 'subject sched_contact_bday_anniv',
372 );
373
374 $this->fixtures['sched_contact_grad_tomorrow'] = array(
375 'name' => 'sched_contact_grad_tomorrow',
376 'title' => 'sched_contact_grad_tomorrow',
377 'absolute_date' => '',
378 'body_html' => '<p>congratulations on your graduation!</p>',
379 'body_text' => 'congratulations on your graduation!',
380 'end_action' => '',
381 'end_date' => '',
382 'end_frequency_interval' => '',
383 'end_frequency_unit' => '',
384 'entity_status' => 1,
385 'group_id' => '',
386 'is_active' => 1,
387 'is_repeat' => '0',
388 'mapping_id' => 6,
389 'msg_template_id' => '',
390 'recipient' => '',
391 'recipient_listing' => '',
392 'recipient_manual' => '',
393 'record_activity' => 1,
394 'repetition_frequency_interval' => '',
395 'repetition_frequency_unit' => '',
396 'start_action_condition' => 'before',
397 'start_action_date' => 'date_field',
398 'start_action_offset' => '1',
399 'start_action_unit' => 'day',
400 'subject' => 'subject sched_contact_grad_tomorrow',
401 );
402
403 $this->fixtures['sched_contact_grad_anniv'] = array(
404 'name' => 'sched_contact_grad_anniv',
405 'title' => 'sched_contact_grad_anniv',
406 'absolute_date' => '',
407 'body_html' => '<p>dear alum, please send us money.</p>',
408 'body_text' => 'dear alum, please send us money.',
409 'end_action' => '',
410 'end_date' => '',
411 'end_frequency_interval' => '',
412 'end_frequency_unit' => '',
413 'entity_status' => 2,
414 'group_id' => '',
415 'is_active' => 1,
416 'is_repeat' => '0',
417 'mapping_id' => 6,
418 'msg_template_id' => '',
419 'recipient' => '',
420 'recipient_listing' => '',
421 'recipient_manual' => '',
422 'record_activity' => 1,
423 'repetition_frequency_interval' => '',
424 'repetition_frequency_unit' => '',
425 'start_action_condition' => 'after',
426 'start_action_date' => 'date_field',
427 'start_action_offset' => '1',
428 'start_action_unit' => 'week',
429 'subject' => 'subject sched_contact_grad_anniv',
430 );
431
432 $this->fixtures['sched_contact_created_yesterday'] = array(
433 'name' => 'sched_contact_created_yesterday',
434 'title' => 'sched_contact_created_yesterday',
435 'absolute_date' => '',
436 'body_html' => '<p>Your contact was created yesterday</p>',
437 'body_text' => 'Your contact was created yesterday!',
438 'end_action' => '',
439 'end_date' => '',
440 'end_frequency_interval' => '',
441 'end_frequency_unit' => '',
442 'entity_status' => 1,
443 'entity_value' => 'created_date',
444 'group_id' => '',
445 'is_active' => 1,
446 'is_repeat' => '0',
447 'mapping_id' => 6,
448 'msg_template_id' => '',
449 'recipient' => '',
450 'recipient_listing' => '',
451 'recipient_manual' => '',
452 'record_activity' => 1,
453 'repetition_frequency_interval' => '',
454 'repetition_frequency_unit' => '',
455 'start_action_condition' => 'after',
456 'start_action_date' => 'date_field',
457 'start_action_offset' => '1',
458 'start_action_unit' => 'day',
459 'subject' => 'subject sched_contact_created_yesterday',
460 );
461
462 $this->fixtures['sched_contact_mod_anniv'] = array(
463 'name' => 'sched_contact_mod_anniv',
464 'title' => 'sched_contact_mod_anniv',
465 'absolute_date' => '',
466 'body_html' => '<p>You last updated your data last year</p>',
467 'body_text' => 'Go update your stuff!',
468 'end_action' => '',
469 'end_date' => '',
470 'end_frequency_interval' => '',
471 'end_frequency_unit' => '',
472 'entity_status' => 2,
473 'entity_value' => 'modified_date',
474 'group_id' => '',
475 'is_active' => 1,
476 'is_repeat' => '0',
477 'mapping_id' => 6,
478 'msg_template_id' => '',
479 'recipient' => '',
480 'recipient_listing' => '',
481 'recipient_manual' => '',
482 'record_activity' => 1,
483 'repetition_frequency_interval' => '',
484 'repetition_frequency_unit' => '',
485 'start_action_condition' => 'before',
486 'start_action_date' => 'date_field',
487 'start_action_offset' => '1',
488 'start_action_unit' => 'day',
489 'subject' => 'subject sched_contact_mod_anniv',
490 );
491
492 $this->fixtures['sched_eventtype_start_1week_before'] = array(
493 'name' => 'sched_eventtype_start_1week_before',
494 'title' => 'sched_eventtype_start_1week_before',
495 'absolute_date' => '',
496 'body_html' => '<p>body sched_eventtype_start_1week_before ({event.title})</p>',
497 'body_text' => 'body sched_eventtype_start_1week_before ({event.title})',
498 'end_action' => '',
499 'end_date' => '',
500 'end_frequency_interval' => '',
501 'end_frequency_unit' => '',
502 'entity_status' => '', // participant status id
503 'entity_value' => '', // event type id
504 'group_id' => '',
505 'is_active' => 1,
506 'is_repeat' => '0',
507 'mapping_id' => 2, // event type
508 'msg_template_id' => '',
509 'recipient' => '',
510 'recipient_listing' => '',
511 'recipient_manual' => '',
512 'record_activity' => 1,
513 'repetition_frequency_interval' => '',
514 'repetition_frequency_unit' => '',
515 'start_action_condition' => 'before',
516 'start_action_date' => 'event_start_date',
517 'start_action_offset' => '1',
518 'start_action_unit' => 'week',
519 'subject' => 'subject sched_eventtype_start_1week_before ({event.title})',
520 );
521 $this->fixtures['sched_eventtype_end_2month_repeat_twice_2_weeks'] = array(
522 'name' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
523 'title' => 'sched_eventtype_end_2month_repeat_twice_2_weeks',
524 'absolute_date' => '',
525 'body_html' => '<p>body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}</p>',
526 'body_text' => 'body sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
527 'end_action' => 'after',
528 'end_date' => 'event_end_date',
529 'end_frequency_interval' => '3',
530 'end_frequency_unit' => 'month',
531 'entity_status' => '', // participant status id
532 'entity_value' => '', // event type id
533 'group_id' => '',
534 'is_active' => 1,
535 'is_repeat' => '1',
536 'mapping_id' => 2, // event type
537 'msg_template_id' => '',
538 'recipient' => '',
539 'recipient_listing' => '',
540 'recipient_manual' => '',
541 'record_activity' => 1,
542 'repetition_frequency_interval' => '2',
543 'repetition_frequency_unit' => 'week',
544 'start_action_condition' => 'after',
545 'start_action_date' => 'event_end_date',
546 'start_action_offset' => '2',
547 'start_action_unit' => 'month',
548 'subject' => 'subject sched_eventtype_end_2month_repeat_twice_2_weeks {event.title}',
549 );
550
551 $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'] = array(
552 'name' => 'sched_membership_end_2month',
553 'title' => 'sched_membership_end_2month',
554 'absolute_date' => '',
555 'body_html' => '<p>body sched_membership_end_2month</p>',
556 'body_text' => 'body sched_membership_end_2month',
557 'end_action' => '',
558 'end_date' => 'membership_end_date',
559 'end_frequency_interval' => '4',
560 'end_frequency_unit' => 'month',
561 'entity_status' => '',
562 'entity_value' => '',
563 'group_id' => '',
564 'is_active' => 1,
565 'is_repeat' => '1',
566 'mapping_id' => 4,
567 'msg_template_id' => '',
568 'recipient' => '',
569 'recipient_listing' => '',
570 'recipient_manual' => '',
571 'record_activity' => 1,
572 'repetition_frequency_interval' => '4',
573 'repetition_frequency_unit' => 'week',
574 'start_action_condition' => 'after',
575 'start_action_date' => 'membership_end_date',
576 'start_action_offset' => '2',
577 'start_action_unit' => 'month',
578 'subject' => 'subject sched_membership_end_2month',
579 );
580 $this->fixtures['sched_membership_end_limit_to_none'] = array(
581 'name' => 'limit to none',
582 'title' => 'limit to none',
583 'absolute_date' => '',
584 'body_html' => '<p>body sched_membership_end_2month</p>',
585 'body_text' => 'body sched_membership_end_2month',
586 'end_action' => '',
587 'end_date' => '',
588 'end_frequency_interval' => '4',
589 'end_frequency_unit' => 'month',
590 'entity_status' => '',
591 'entity_value' => '',
592 'limit_to' => 0,
593 'group_id' => '',
594 'is_active' => 1,
595 'is_repeat' => '1',
596 'mapping_id' => 4,
597 'msg_template_id' => '',
598 'recipient' => '',
599 'recipient_listing' => '',
600 'recipient_manual' => '',
601 'record_activity' => 1,
602 'repetition_frequency_interval' => '4',
603 'repetition_frequency_unit' => 'week',
604 'start_action_condition' => 'after',
605 'start_action_date' => 'membership_end_date',
606 'start_action_offset' => '2',
607 'start_action_unit' => 'month',
608 'subject' => 'limit to none',
609 );
610 $this->fixtures['sched_on_membership_end_date_repeat_interval'] = array(
611 'name' => 'sched_on_membership_end_date',
612 'title' => 'sched_on_membership_end_date',
613 'body_html' => '<p>Your membership expired 1 unit ago</p>',
614 'body_text' => 'Your membership expired 1 unit ago',
615 'end_frequency_interval' => 10,
616 'end_frequency_unit' => 'year',
617 'is_active' => 1,
618 'is_repeat' => TRUE,
619 'mapping_id' => 4,
620 'record_activity' => 1,
621 'start_action_condition' => 'after',
622 'start_action_date' => 'membership_end_date',
623 'start_action_offset' => '0',
624 'start_action_unit' => 'hour',
625 'subject' => 'subject send reminder every unit after membership_end_date',
626 );
627
628 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array(
629 'title' => ts('Test Contact Custom group'),
630 'name' => 'test_contact_cg',
631 'extends' => 'Contact',
632 'domain_id' => CRM_Core_Config::domainID(),
633 'is_active' => 1,
634 'collapse_adv_display' => 0,
635 'collapse_display' => 0,
636 ));
637 $customField = $this->callAPISuccess('CustomField', 'create', array(
638 'label' => 'Test Text',
639 'data_type' => 'String',
640 'html_type' => 'Text',
641 'custom_group_id' => $customGroup['id'],
642 ));
643 $this->fixtures['contact_custom_token'] = array(
644 'id' => $customField['id'],
645 'token' => sprintf('{contact.custom_%s}', $customField['id']),
646 'name' => sprintf('custom_%s', $customField['id']),
647 'value' => 'text ' . substr(sha1(rand()), 0, 7),
648 );
649
650 $this->_setUp();
651 }
652
653 /**
654 * Tears down the fixture, for example, closes a network connection.
655 *
656 * This method is called after a test is executed.
657 */
658 public function tearDown() {
659 parent::tearDown();
660 $this->mut->clearMessages();
661 $this->mut->stop();
662 unset($this->mut);
663 $this->quickCleanup(array(
664 'civicrm_action_schedule',
665 'civicrm_action_log',
666 'civicrm_membership',
667 'civicrm_participant',
668 'civicrm_event',
669 'civicrm_email',
670 ));
671 $this->callAPISuccess('CustomField', 'delete', array('id' => $this->fixtures['contact_custom_token']['id']));
672 $this->callAPISuccess('CustomGroup', 'delete', array(
673 'id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', 'test_contact_cg', 'id', 'name'),
674 ));
675 $this->_tearDown();
676 }
677
678 public function mailerExamples() {
679 $cases = array();
680
681 // Some tokens - short as subject has 128char limit in DB.
682 $someTokensTmpl = implode(';;', array(
683 '{contact.display_name}', // basic contact token
684 '{contact.gender}', // funny legacy contact token
685 '{contact.gender_id}', // funny legacy contact token
686 '{domain.name}', // domain token
687 '{activity.activity_type}', // action-scheduler token
688 ));
689 // Further tokens can be tested in the body text/html.
690 $manyTokensTmpl = implode(';;', array(
691 $someTokensTmpl,
692 '{contact.email_greeting}',
693 $this->fixture['contact_custom_token']['token'],
694 ));
695 // Note: The behavior of domain-tokens on a scheduled reminder is undefined. All we
696 // can really do is check that it has something.
697 $someTokensExpected = 'Churmondleia Ōtākou;;Female;;Female;;[a-zA-Z0-9 ]+;;Phone Call';
698 $manyTokensExpected = sprintf('%s;;Dear Churmondleia;;%s', $someTokensExpected, $this->fixture['contact_custom_token']['value']);
699
700 // In this example, we use a lot of tokens cutting across multiple components.
701 $cases[0] = array(
702 // Schedule definition.
703 array(
704 'subject' => "subj $someTokensTmpl",
705 'body_html' => "html $manyTokensTmpl",
706 'body_text' => "text $manyTokensTmpl",
707 ),
708 // Assertions (regex).
709 array(
710 'from_name' => "/^FIXME\$/",
711 'from_email' => "/^info@EXAMPLE.ORG\$/",
712 'subject' => "/^subj $someTokensExpected\$/",
713 'body_html' => "/^html $manyTokensExpected\$/",
714 'body_text' => "/^text $manyTokensExpected\$/",
715 ),
716 );
717
718 // In this example, we customize the from address.
719 $cases[1] = array(
720 // Schedule definition.
721 array(
722 'from_name' => 'Bob',
723 'from_email' => 'bob@example.org',
724 ),
725 // Assertions (regex).
726 array(
727 'from_name' => "/^Bob\$/",
728 'from_email' => "/^bob@example.org\$/",
729 ),
730 );
731
732 // In this example, we autoconvert HTML to text
733 $cases[2] = array(
734 // Schedule definition.
735 array(
736 'body_html' => '<p>Hello &amp; stuff.</p>',
737 'body_text' => '',
738 ),
739 // Assertions (regex).
740 array(
741 'body_html' => '/^' . preg_quote('<p>Hello &amp; stuff.</p>', '/') . '/',
742 'body_text' => '/^' . preg_quote('Hello & stuff.', '/') . '/',
743 ),
744 );
745
746 // In this example, we autoconvert HTML to text
747 $cases[3] = array(
748 // Schedule definition.
749 array(
750 'body_html' => '',
751 'body_text' => 'Hello world',
752 ),
753 // Assertions (regex).
754 array(
755 'body_html' => '/^--UNDEFINED--$/',
756 'body_text' => '/^Hello world$/',
757 ),
758 );
759
760 return $cases;
761 }
762
763 /**
764 * This generates a single mailing through the scheduled-reminder
765 * system (using an activity-reminder as a baseline) and
766 * checks that the resulting message satisfies various
767 * regular expressions.
768 *
769 * @param array $schedule
770 * Values to set/override in the schedule.
771 * Ex: array('subject' => 'Hello, {contact.first_name}!').
772 * @param array $patterns
773 * A list of regexes to compare with the actual email.
774 * Ex: array('subject' => '/^Hello, Alice!/').
775 * Keys: subject, body_text, body_html, from_name, from_email.
776 * @dataProvider mailerExamples
777 */
778 public function testMailer($schedule, $patterns) {
779 $actionSchedule = array_merge($this->fixtures['sched_activity_1day'], $schedule);
780 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
781 $this->assertTrue(is_numeric($actionScheduleDao->id));
782
783 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
784 $this->assertTrue(is_numeric($activity->id));
785 $contact = $this->callAPISuccess('contact', 'create', array_merge(
786 $this->fixtures['contact'],
787 array(
788 $this->fixtures['contact_custom_token']['name'] => $this->fixtures['contact_custom_token']['value'],
789 )
790 ));
791 $activity->save();
792
793 $source['contact_id'] = $contact['id'];
794 $source['activity_id'] = $activity->id;
795 $source['record_type_id'] = 2;
796 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
797 $activityContact->save();
798
799 CRM_Utils_Time::setTime('2012-06-14 15:00:00');
800 $this->callAPISuccess('job', 'send_reminder', array());
801 $this->mut->assertRecipients(array(array('test-member@example.com')));
802 foreach ($this->mut->getAllMessages('ezc') as $message) {
803 /** @var ezcMail $message */
804
805 $messageArray = array();
806 $messageArray['subject'] = $message->subject;
807 $messageArray['from_name'] = $message->from->name;
808 $messageArray['from_email'] = $message->from->email;
809 $messageArray['body_text'] = '--UNDEFINED--';
810 $messageArray['body_html'] = '--UNDEFINED--';
811
812 foreach ($message->fetchParts() as $part) {
813 /** @var ezcMailText ezcMailText */
814 if ($part instanceof ezcMailText && $part->subType == 'html') {
815 $messageArray['body_html'] = $part->text;
816 }
817 if ($part instanceof ezcMailText && $part->subType == 'plain') {
818 $messageArray['body_text'] = $part->text;
819 }
820 }
821
822 foreach ($patterns as $field => $pattern) {
823 $this->assertRegExp($pattern, $messageArray[$field],
824 "Check that '$field'' matches regex. " . print_r(array('expected' => $patterns, 'actual' => $messageArray), 1));
825 }
826 }
827 $this->mut->clearMessages();
828 }
829
830 public function testActivityDateTimeMatchNonRepeatableSchedule() {
831 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day']);
832 $this->assertTrue(is_numeric($actionScheduleDao->id));
833
834 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
835 $this->assertTrue(is_numeric($activity->id));
836 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
837 $activity->subject = "Test subject for Phonecall";
838 $activity->save();
839
840 $source['contact_id'] = $contact['id'];
841 $source['activity_id'] = $activity->id;
842 $source['record_type_id'] = 2;
843 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
844 $activityContact->save();
845
846 $this->assertCronRuns(array(
847 array(
848 // Before the 24-hour mark, no email
849 'time' => '2012-06-14 04:00:00',
850 'recipients' => array(),
851 'subjects' => array(),
852 ),
853 array(
854 // After the 24-hour mark, an email
855 'time' => '2012-06-14 15:00:00',
856 'recipients' => array(array('test-member@example.com')),
857 'subjects' => array('1-Day (non-repeating) (about Phone Call)'),
858 ),
859 array(
860 // Run cron again; message already sent
861 'time' => '',
862 'recipients' => array(),
863 ),
864 ));
865 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
866 $activityDAO = new CRM_Activity_DAO_Activity();
867 $activityDAO->source_record_id = $activity->id;
868 $activityDAO->activity_type_id = array_search('Reminder Sent', $activityTypes);
869 $activityDAO->find();
870 while ($activityDAO->fetch()) {
871 $this->assertContains($activity->subject, $activityDAO->details);
872 }
873 }
874
875 public function testActivityDateTimeMatchRepeatableSchedule() {
876 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day_r']);
877 $this->assertTrue(is_numeric($actionScheduleDao->id));
878
879 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
880 $this->assertTrue(is_numeric($activity->id));
881 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
882 $activity->save();
883
884 $source['contact_id'] = $contact['id'];
885 $source['activity_id'] = $activity->id;
886 $source['record_type_id'] = 2;
887 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
888 $activityContact->save();
889
890 $this->assertCronRuns(array(
891 array(
892 // Before the 24-hour mark, no email
893 'time' => '012-06-14 04:00:00',
894 'recipients' => array(),
895 'subjects' => array(),
896 ),
897 array(
898 // After the 24-hour mark, an email
899 'time' => '2012-06-14 15:00:00',
900 'recipients' => array(array('test-member@example.com')),
901 'subjects' => array('1-Day (repeating) (about Phone Call)'),
902 ),
903 array(
904 // Run cron 4 hours later; first message already sent
905 'time' => '2012-06-14 20:00:00',
906 'recipients' => array(),
907 'subjects' => array(),
908 ),
909 array(
910 // Run cron 6 hours later; send second message.
911 'time' => '2012-06-14 21:00:01',
912 'recipients' => array(array('test-member@example.com')),
913 'subjects' => array('1-Day (repeating) (about Phone Call)'),
914 ),
915 ));
916 }
917
918 public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate() {
919 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day_r_on_abs_date']);
920 $this->assertTrue(is_numeric($actionScheduleDao->id));
921
922 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
923 $this->assertTrue(is_numeric($activity->id));
924 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
925 $activity->save();
926
927 $source['contact_id'] = $contact['id'];
928 $source['activity_id'] = $activity->id;
929 $source['record_type_id'] = 2;
930 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
931 $activityContact->save();
932
933 $this->assertCronRuns(array(
934 array(
935 // Before the 24-hour mark, no email
936 'time' => '2012-06-13 04:00:00',
937 'recipients' => array(),
938 'subjects' => array(),
939 ),
940 array(
941 // On absolute date set on 2012-06-14
942 'time' => '2012-06-14 00:00:00',
943 'recipients' => array(array('test-member@example.com')),
944 'subjects' => array('1-Day (repeating) (about Phone Call)'),
945 ),
946 array(
947 // Run cron 4 hours later; first message already sent
948 'time' => '2012-06-14 04:00:00',
949 'recipients' => array(),
950 'subjects' => array(),
951 ),
952 array(
953 // Run cron 6 hours later; send second message.
954 'time' => '2012-06-14 06:00:01',
955 'recipients' => array(array('test-member@example.com')),
956 'subjects' => array('1-Day (repeating) (about Phone Call)'),
957 ),
958 ));
959 }
960
961 /**
962 * For contacts/activities which don't match the schedule filter,
963 * an email should *not* be sent.
964 */
965 // TODO // function testActivityDateTime_NonMatch() { }
966
967 /**
968 * For contacts/members which match schedule based on join date,
969 * an email should be sent.
970 */
971 public function testMembershipJoinDateMatch() {
972 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1)));
973 $this->assertTrue(is_numeric($membership->id));
974 $result = $this->callAPISuccess('Email', 'create', array(
975 'contact_id' => $membership->contact_id,
976 'email' => 'test-member@example.com',
977 'location_type_id' => 1,
978 ));
979 $this->assertAPISuccess($result);
980
981 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
982 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
983 $actionSchedule['entity_value'] = $membership->membership_type_id;
984 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
985 $this->assertTrue(is_numeric($actionScheduleDao->id));
986
987 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
988 $this->assertCronRuns(array(
989 array(
990 // Before the 2-week mark, no email.
991 'time' => '2012-03-28 01:00:00',
992 'recipients' => array(),
993 'subjects' => array(),
994 ),
995 array(
996 // After the 2-week mark, send an email.
997 'time' => '2012-03-29 01:00:00',
998 'recipients' => array(array('test-member@example.com')),
999 'subjects' => array('subject sched_membership_join_2week (joined March 15th, 2012)'),
1000 ),
1001 ));
1002 }
1003
1004
1005 /**
1006 * CRM-21675: Support parent and smart group in 'Limit to' field
1007 */
1008 public function testScheduleReminderWithParentGroup() {
1009 // Contact A with birth-date at '07-07-2005' and gender - Male, later got added in smart group
1010 $contactID1 = $this->individualCreate(array('birth_date' => '20050707', 'gender_id' => 1, 'email' => 'abc@test.com'));
1011 // Contact B with birth-date at '07-07-2005', later got added in regular group
1012 $contactID2 = $this->individualCreate(array('birth_date' => '20050707', 'email' => 'def@test.com'), 1);
1013 // Contact C with birth-date at '07-07-2005', but not included in any group
1014 $contactID3 = $this->individualCreate(array('birth_date' => '20050707', 'email' => 'ghi@test.com'), 2);
1015
1016 // create regular group and add Contact B to it
1017 $groupID = $this->groupCreate();
1018 $this->callAPISuccess('GroupContact', 'Create', array(
1019 'group_id' => $groupID,
1020 'contact_id' => $contactID2,
1021 ));
1022
1023 // create smart group which will contain all Male contacts
1024 $smartGroupParams = array('formValues' => array('gender_id' => 1));
1025 $smartGroupID = $this->smartGroupCreate(
1026 $smartGroupParams,
1027 array(
1028 'name' => 'new_smart_group',
1029 'title' => 'New Smart Group',
1030 'parents' => array($groupID => 1),
1031 )
1032 );
1033
1034 $actionScheduleParams = array(
1035 'name' => 'sched_contact_bday_yesterday',
1036 'title' => 'sched_contact_bday_yesterday',
1037 'absolute_date' => '',
1038 'body_html' => '<p>you look like you were born yesterday!</p>',
1039 'body_text' => 'you look like you were born yesterday!',
1040 'end_action' => '',
1041 'end_date' => '',
1042 'end_frequency_interval' => '',
1043 'end_frequency_unit' => '',
1044 'entity_status' => 1,
1045 'entity_value' => 'birth_date',
1046 'limit_to' => 1,
1047 'group_id' => $groupID,
1048 'is_active' => 1,
1049 'is_repeat' => '0',
1050 'mapping_id' => 6,
1051 'msg_template_id' => '',
1052 'recipient' => '2',
1053 'recipient_listing' => '',
1054 'recipient_manual' => '',
1055 'record_activity' => 1,
1056 'repetition_frequency_interval' => '',
1057 'repetition_frequency_unit' => '',
1058 'start_action_condition' => 'after',
1059 'start_action_date' => 'date_field',
1060 'start_action_offset' => '1',
1061 'start_action_unit' => 'day',
1062 'subject' => 'subject sched_contact_bday_yesterday',
1063 );
1064
1065 // Create schedule reminder where parent group ($groupID) is selectd to limit recipients,
1066 // which contain a individual contact - $contactID2 and is parent to smart group.
1067 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionScheduleParams);
1068 $this->assertTrue(is_numeric($actionScheduleDao->id));
1069 $this->assertCronRuns(array(
1070 array(
1071 // On the birthday, no email.
1072 'time' => '2005-07-07 01:00:00',
1073 'recipients' => array(),
1074 ),
1075 array(
1076 // The next day, send an email.
1077 'time' => '2005-07-08 20:00:00',
1078 'recipients' => array(
1079 array(
1080 'def@test.com',
1081 ),
1082 array(
1083 'abc@test.com',
1084 ),
1085 ),
1086 ),
1087 ));
1088 $this->groupDelete($smartGroupID);
1089 $this->groupDelete($groupID);
1090 }
1091
1092 /**
1093 * Test end date email sent.
1094 *
1095 * For contacts/members which match schedule based on join date,
1096 * an email should be sent.
1097 */
1098 public function testMembershipJoinDateNonMatch() {
1099 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures['rolling_membership']);
1100 $this->assertTrue(is_numeric($membership->id));
1101 $result = $this->callAPISuccess('Email', 'create', array(
1102 'contact_id' => $membership->contact_id,
1103 'location_type_id' => 1,
1104 'email' => 'test-member@example.com',
1105 ));
1106
1107 // Add an alternative membership type, and only send messages for that type
1108 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
1109 $this->assertTrue(is_numeric($extraMembershipType->id));
1110 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_membership_join_2week']);
1111 $this->assertTrue(is_numeric($actionScheduleDao->id));
1112 $actionScheduleDao->entity_value = $extraMembershipType->id;
1113 $actionScheduleDao->save();
1114
1115 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1116 $this->assertCronRuns(array(
1117 array(
1118 // After the 2-week mark, don't send email because we have different membership type.
1119 'time' => '2012-03-29 01:00:00',
1120 'recipients' => array(),
1121 ),
1122 ));
1123 }
1124
1125 /**
1126 * Test that the first and SECOND notifications are sent out.
1127 */
1128 public function testMembershipEndDateRepeat() {
1129 // creates membership with end_date = 20120615
1130 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1131 $result = $this->callAPISuccess('Email', 'create', array(
1132 'contact_id' => $membership->contact_id,
1133 'email' => 'test-member@example.com',
1134 ));
1135 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1136
1137 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
1138 $actionSchedule['entity_value'] = $membership->membership_type_id;
1139 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1140
1141 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1142 $this->assertCronRuns(array(
1143 array(
1144 // After the 2-week mark, send an email.
1145 'time' => '2012-08-15 01:00:00',
1146 'recipients' => array(array('test-member@example.com')),
1147 ),
1148 array(
1149 // After the 2-week mark, send an email.
1150 'time' => '2012-09-12 01:00:00',
1151 'recipients' => array(array('test-member@example.com')),
1152 ),
1153 ));
1154 }
1155
1156 /**
1157 * Test behaviour when date changes.
1158 *
1159 * Test that the first notification is sent but the second is NOT sent if the end date changes in
1160 * between
1161 * see CRM-15376
1162 */
1163 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
1164 // creates membership with end_date = 20120615
1165 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1166 $this->callAPISuccess('Email', 'create', array(
1167 'contact_id' => $membership->contact_id,
1168 'email' => 'test-member@example.com',
1169 ));
1170 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1171
1172 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
1173 $actionSchedule['entity_value'] = $membership->membership_type_id;
1174 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1175 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1176 $this->assertCronRuns(array(
1177 array(
1178 // After the 2-week mark, send an email.
1179 'time' => '2012-08-15 01:00:00',
1180 'recipients' => array(array('test-member@example.com')),
1181 ),
1182 ));
1183
1184 // Extend membership - reminder should NOT go out.
1185 $this->callAPISuccess('membership', 'create', array('id' => $membership->id, 'end_date' => '2014-01-01'));
1186 $this->assertCronRuns(array(
1187 array(
1188 // After the 2-week mark, send an email.
1189 'time' => '2012-09-12 01:00:00',
1190 'recipients' => array(),
1191 ),
1192 ));
1193 }
1194
1195 /**
1196 * Test membership end date email sends.
1197 *
1198 * For contacts/members which match schedule based on end date,
1199 * an email should be sent.
1200 */
1201 public function testMembershipEndDateMatch() {
1202 // creates membership with end_date = 20120615
1203 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1204 $this->assertTrue(is_numeric($membership->id));
1205 $this->callAPISuccess('Email', 'create', array(
1206 'contact_id' => $membership->contact_id,
1207 'email' => 'test-member@example.com',
1208 ));
1209 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1210
1211 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
1212 $actionSchedule['entity_value'] = $membership->membership_type_id;
1213 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1214 $this->assertTrue(is_numeric($actionScheduleDao->id));
1215
1216 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1217 $this->assertCronRuns(array(
1218 array(
1219 // Before the 2-week mark, no email.
1220 'time' => '2012-05-31 01:00:00',
1221 // 'time' => '2012-06-01 01:00:00',
1222 // FIXME: Is this the right boundary?
1223 'recipients' => array(),
1224 ),
1225 array(
1226 // After the 2-week mark, send an email.
1227 'time' => '2012-06-01 01:00:00',
1228 'recipients' => array(array('test-member@example.com')),
1229 ),
1230 ));
1231
1232 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
1233 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
1234 $membership->end_date = '2012-09-15';
1235 $membership->save();
1236
1237 //change the email id of chosen membership contact to assert
1238 //recipient of not the previously sent mail but the new one
1239 $result = $this->callAPISuccess('Email', 'create', array(
1240 'is_primary' => 1,
1241 'contact_id' => $membership->contact_id,
1242 'email' => 'member2@example.com',
1243 ));
1244 $this->assertAPISuccess($result);
1245
1246 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
1247 $this->assertCronRuns(array(
1248 array(
1249 // Before the 2-week mark, no email
1250 'time' => '2012-08-31 01:00:00',
1251 'recipients' => array(),
1252 ),
1253 //array( // After the 2-week mark, send an email
1254 //'time' => '2012-09-01 01:00:00',
1255 //'recipients' => array(array('member2@example.com')),
1256 //),
1257 ));
1258 }
1259
1260
1261 /**
1262 * Test membership end date email.
1263 *
1264 * For contacts/members which match schedule based on end date,
1265 * an email should be sent.
1266 */
1267 public function testMembershipEndDateNoMatch() {
1268 // creates membership with end_date = 20120615
1269 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 3)));
1270 $this->assertTrue(is_numeric($membership->id));
1271 $result = $this->callAPISuccess('Email', 'create', array(
1272 'contact_id' => $membership->contact_id,
1273 'email' => 'test-member@example.com',
1274 ));
1275 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1276
1277 $actionSchedule = $this->fixtures['sched_membership_end_2month'];
1278 $actionSchedule['entity_value'] = $membership->membership_type_id;
1279 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1280 $this->assertTrue(is_numeric($actionScheduleDao->id));
1281
1282 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1283 $this->assertCronRuns(array(
1284 array(
1285 // Before the 2-week mark, no email.
1286 'time' => '2012-05-31 01:00:00',
1287 // 'time' => '2012-06-01 01:00:00',
1288 // FIXME: Is this the right boundary?
1289 'recipients' => array(),
1290 ),
1291 array(
1292 // After the 2-week mark, send an email.
1293 'time' => '2013-05-01 01:00:00',
1294 'recipients' => array(),
1295 ),
1296 ));
1297 }
1298
1299 public function testContactBirthDateNoAnniv() {
1300 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1301 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1302 $actionSchedule = $this->fixtures['sched_contact_bday_yesterday'];
1303 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1304 $this->assertTrue(is_numeric($actionScheduleDao->id));
1305 $this->assertCronRuns(array(
1306 array(
1307 // On the birthday, no email.
1308 'time' => '2005-07-07 01:00:00',
1309 'recipients' => array(),
1310 ),
1311 array(
1312 // The next day, send an email.
1313 'time' => '2005-07-08 20:00:00',
1314 'recipients' => array(array('test-bday@example.com')),
1315 ),
1316 ));
1317 }
1318
1319 public function testContactBirthDateAnniversary() {
1320 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1321 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1322 $actionSchedule = $this->fixtures['sched_contact_bday_anniv'];
1323 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1324 $this->assertTrue(is_numeric($actionScheduleDao->id));
1325 $this->assertCronRuns(array(
1326 array(
1327 // On some random day, no email.
1328 'time' => '2014-03-07 01:00:00',
1329 'recipients' => array(),
1330 ),
1331 array(
1332 // On the eve of their 9th birthday, send an email.
1333 'time' => '2014-07-06 20:00:00',
1334 'recipients' => array(array('test-bday@example.com')),
1335 ),
1336 ));
1337 }
1338
1339 public function testContactCustomDateNoAnniv() {
1340 $group = array(
1341 'title' => 'Test_Group',
1342 'name' => 'test_group',
1343 'extends' => array('Individual'),
1344 'style' => 'Inline',
1345 'is_multiple' => FALSE,
1346 'is_active' => 1,
1347 );
1348 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1349 $field = array(
1350 'label' => 'Graduation',
1351 'data_type' => 'Date',
1352 'html_type' => 'Select Date',
1353 'custom_group_id' => $createGroup['id'],
1354 );
1355 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1356 $contactParams = $this->fixtures['contact'];
1357 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1358 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1359 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1360 $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow'];
1361 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1362 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1363 $this->assertTrue(is_numeric($actionScheduleDao->id));
1364 $this->assertCronRuns(array(
1365 array(
1366 // On some random day, no email.
1367 'time' => '2014-03-07 01:00:00',
1368 'recipients' => array(),
1369 ),
1370 array(
1371 // On the eve of their graduation, send an email.
1372 'time' => '2013-12-15 20:00:00',
1373 'recipients' => array(array('test-member@example.com')),
1374 ),
1375 ));
1376 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1377 }
1378
1379 public function testContactCreatedNoAnniv() {
1380 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1381 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1382 $actionSchedule = $this->fixtures['sched_contact_created_yesterday'];
1383 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1384 $this->assertTrue(is_numeric($actionScheduleDao->id));
1385 $this->assertCronRuns(array(
1386 array(
1387 // On the date created, no email.
1388 'time' => $contact['values'][$contact['id']]['created_date'],
1389 'recipients' => array(),
1390 ),
1391 array(
1392 // The next day, send an email.
1393 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['created_date'] . ' +1 day')),
1394 'recipients' => array(array('test-bday@example.com')),
1395 ),
1396 ));
1397 }
1398
1399 public function testContactModifiedAnniversary() {
1400 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
1401 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1402 $modifiedDate = $this->callAPISuccess('Contact', 'getvalue', array('id' => $contact['id'], 'return' => 'modified_date'));
1403 $actionSchedule = $this->fixtures['sched_contact_mod_anniv'];
1404 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1405 $this->assertTrue(is_numeric($actionScheduleDao->id));
1406 $this->assertCronRuns(array(
1407 array(
1408 // On some random day, no email.
1409 'time' => date('Y-m-d H:i:s', strtotime($contact['values'][$contact['id']]['modified_date'] . ' -60 days')),
1410 'recipients' => array(),
1411 ),
1412 array(
1413 // On the eve of 3 years after they were modified, send an email.
1414 'time' => date('Y-m-d H:i:s', strtotime($modifiedDate . ' +3 years -1 day')),
1415 'recipients' => array(array('test-bday@example.com')),
1416 ),
1417 ));
1418 }
1419
1420 /**
1421 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
1422 */
1423 public function testMembershipLimitToNone() {
1424 // creates membership with end_date = 20120615
1425 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1426
1427 $this->assertTrue(is_numeric($membership->id));
1428 $result = $this->callAPISuccess('Email', 'create', array(
1429 'contact_id' => $membership->contact_id,
1430 'email' => 'member@example.com',
1431 ));
1432 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1433 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
1434
1435 $this->assertAPISuccess($result);
1436
1437 $actionSchedule = $this->fixtures['sched_membership_end_limit_to_none'];
1438 $actionSchedule['entity_value'] = $membership->membership_type_id;
1439 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1440 $this->assertTrue(is_numeric($actionScheduleDao->id));
1441
1442 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1443 $this->assertCronRuns(array(
1444 array(
1445 // Before the 2-week mark, no email.
1446 'time' => '2012-05-31 01:00:00',
1447 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
1448 'recipients' => array(),
1449 ),
1450 ));
1451 }
1452
1453 public function testMembership_referenceDate() {
1454 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1455
1456 $this->assertTrue(is_numeric($membership->id));
1457 $result = $this->callAPISuccess('Email', 'create', array(
1458 'contact_id' => $membership->contact_id,
1459 'email' => 'member@example.com',
1460 ));
1461
1462 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1463 $this->assertAPISuccess($result);
1464
1465 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
1466 $actionSchedule['entity_value'] = $membership->membership_type_id;
1467 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1468 $this->assertTrue(is_numeric($actionScheduleDao->id));
1469
1470 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
1471 $this->assertCronRuns(array(
1472 array(
1473 // After the 2-week mark, send an email
1474 'time' => '2012-03-29 01:00:00',
1475 'recipients' => array(array('member@example.com')),
1476 ),
1477 array(
1478 // After the 2-week 1day mark, don't send an email
1479 'time' => '2012-03-30 01:00:00',
1480 'recipients' => array(),
1481 ),
1482 ));
1483
1484 //check if reference date is set to membership's join date
1485 //as per the action_start_date chosen for current schedule reminder
1486 $this->assertEquals('2012-03-15',
1487 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id')
1488 );
1489
1490 //change current membership join date that may signifies as membership renewal activity
1491 $membership->join_date = '2012-03-29';
1492 $membership->save();
1493
1494 $this->assertCronRuns(array(
1495 array(
1496 // After the 13 days of the changed join date 2012-03-29, don't send an email
1497 'time' => '2012-04-11 01:00:00',
1498 'recipients' => array(),
1499 ),
1500 array(
1501 // After the 2-week of the changed join date 2012-03-29, send an email
1502 'time' => '2012-04-12 01:00:00',
1503 'recipients' => array(array('member@example.com')),
1504 ),
1505 ));
1506 $this->assertCronRuns(array(
1507 array(
1508 // It should not re-send on the same day
1509 'time' => '2012-04-12 01:00:00',
1510 'recipients' => array(),
1511 ),
1512 ));
1513 }
1514
1515 public function testMembershipOnMultipleReminder() {
1516 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
1517
1518 $this->assertTrue(is_numeric($membership->id));
1519 $result = $this->callAPISuccess('Email', 'create', array(
1520 'contact_id' => $membership->contact_id,
1521 'email' => 'member@example.com',
1522 ));
1523 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
1524 $this->assertAPISuccess($result);
1525
1526 $actionScheduleBefore = $this->fixtures['sched_membership_end_2week']; // Send email 2 weeks before end_date
1527 $actionScheduleOn = $this->fixtures['sched_on_membership_end_date']; // Send email on end_date/expiry date
1528 $actionScheduleAfter = $this->fixtures['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
1529 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id;
1530 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
1531 $$value = CRM_Core_BAO_ActionSchedule::add($$value);
1532 $this->assertTrue(is_numeric($$value->id));
1533 }
1534
1535 $this->assertCronRuns(
1536 array(
1537 array(
1538 // 1day 2weeks before membership end date(MED), don't send mail
1539 'time' => '2012-05-31 01:00:00',
1540 'recipients' => array(),
1541 ),
1542 array(
1543 // 2 weeks before MED, send an email
1544 'time' => '2012-06-01 01:00:00',
1545 'recipients' => array(array('member@example.com')),
1546 ),
1547 array(
1548 // 1day before MED, don't send mail
1549 'time' => '2012-06-14 01:00:00',
1550 'recipients' => array(),
1551 ),
1552 array(
1553 // On MED, send an email
1554 'time' => '2012-06-15 00:00:00',
1555 'recipients' => array(array('member@example.com')),
1556 ),
1557 array(
1558 // After 1day of MED, send an email
1559 'time' => '2012-06-16 01:00:00',
1560 'recipients' => array(array('member@example.com')),
1561 ),
1562 array(
1563 // After 1day 1min of MED, don't send an email
1564 'time' => '2012-06-17 00:01:00',
1565 'recipients' => array(),
1566 ),
1567 )
1568 );
1569
1570 // Assert the timestamp as of when the emails of respective three reminders as configured
1571 // 2 weeks before, on and 1 day after MED, are sent
1572 $this->assertApproxEquals(
1573 strtotime('2012-06-01 01:00:00'),
1574 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id, 'action_date_time', 'action_schedule_id', TRUE)),
1575 3 // Variation in test execution time.
1576 );
1577 $this->assertApproxEquals(
1578 strtotime('2012-06-15 00:00:00'),
1579 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id, 'action_date_time', 'action_schedule_id', TRUE)),
1580 3 // Variation in test execution time.
1581 );
1582 $this->assertApproxEquals(
1583 strtotime('2012-06-16 01:00:00'),
1584 strtotime(CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id, 'action_date_time', 'action_schedule_id', TRUE)),
1585 3 // Variation in test execution time.
1586 );
1587
1588 //extend MED to 2 weeks after the current MED (that may signifies as membership renewal activity)
1589 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1590 $membership->end_date = '2012-06-20';
1591 $membership->save();
1592
1593 $result = $this->callAPISuccess('Contact', 'get', array('id' => $membership->contact_id));
1594 $this->assertCronRuns(
1595 array(
1596 array(
1597 // 1day 2weeks before membership end date(MED), don't send mail
1598 'time' => '2012-06-05 01:00:00',
1599 'recipients' => array(),
1600 ),
1601 array(
1602 // 2 weeks before MED, send an email
1603 'time' => '2012-06-06 01:00:00',
1604 'recipients' => array(array('member@example.com')),
1605 ),
1606 array(
1607 // 1day before MED, don't send mail
1608 'time' => '2012-06-19 01:00:00',
1609 'recipients' => array(),
1610 ),
1611 array(
1612 // On MED, send an email
1613 'time' => '2012-06-20 00:00:00',
1614 'recipients' => array(array('member@example.com')),
1615 ),
1616 array(
1617 // After 1day of MED, send an email
1618 'time' => '2012-06-21 01:00:00',
1619 'recipients' => array(array('member@example.com')),
1620 ),
1621 array(
1622 // After 1day 1min of MED, don't send an email
1623 'time' => '2012-07-21 00:01:00',
1624 'recipients' => array(),
1625 ),
1626 ));
1627 }
1628
1629 public function testContactCustomDate_Anniv() {
1630 $group = array(
1631 'title' => 'Test_Group now',
1632 'name' => 'test_group_now',
1633 'extends' => array('Individual'),
1634 'style' => 'Inline',
1635 'is_multiple' => FALSE,
1636 'is_active' => 1,
1637 );
1638 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
1639 $field = array(
1640 'label' => 'Graduation',
1641 'data_type' => 'Date',
1642 'html_type' => 'Select Date',
1643 'custom_group_id' => $createGroup['id'],
1644 );
1645 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1646
1647 $contactParams = $this->fixtures['contact'];
1648 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
1649 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
1650 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1651 $actionSchedule = $this->fixtures['sched_contact_grad_anniv'];
1652 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1653 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1654 $this->assertTrue(is_numeric($actionScheduleDao->id));
1655 $this->assertCronRuns(array(
1656 array(
1657 // On some random day, no email.
1658 'time' => '2014-03-07 01:00:00',
1659 'recipients' => array(),
1660 ),
1661 array(
1662 // A week after their 5th anniversary of graduation, send an email.
1663 'time' => '2018-12-23 20:00:00',
1664 'recipients' => array(array('test-member@example.com')),
1665 ),
1666 ));
1667 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
1668 }
1669
1670 public function testEventTypeStartDate() {
1671 // Create event+participant with start_date = 20120315, end_date = 20120615.
1672 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], array('status_id' => 2)));
1673 $this->callAPISuccess('Email', 'create', array(
1674 'contact_id' => $participant->contact_id,
1675 'email' => 'test-event@example.com',
1676 ));
1677 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $participant->contact_id)));
1678
1679 $actionSchedule = $this->fixtures['sched_eventtype_start_1week_before'];
1680 $actionSchedule['entity_value'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'event_type_id');
1681 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1682
1683 //echo "CREATED\n"; ob_flush(); sleep(20);
1684
1685 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
1686 $this->assertCronRuns(array(
1687 array(
1688 // 2 weeks before
1689 'time' => '2012-03-02 01:00:00',
1690 'recipients' => array(),
1691 ),
1692 array(
1693 // 1 week before
1694 'time' => '2012-03-08 01:00:00',
1695 'recipients' => array(array('test-event@example.com')),
1696 ),
1697 array(
1698 // And then nothing else
1699 'time' => '2012-03-16 01:00:00',
1700 'recipients' => array(),
1701 ),
1702 ));
1703 }
1704
1705 public function testEventTypeEndDateRepeat() {
1706 // Create event+participant with start_date = 20120315, end_date = 20120615.
1707 $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], array('status_id' => 2)));
1708 $this->callAPISuccess('Email', 'create', array(
1709 'contact_id' => $participant->contact_id,
1710 'email' => 'test-event@example.com',
1711 ));
1712 $c = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $participant->contact_id)));
1713
1714 $actionSchedule = $this->fixtures['sched_eventtype_end_2month_repeat_twice_2_weeks'];
1715 $actionSchedule['entity_value'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $participant->event_id, 'event_type_id');
1716 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
1717
1718 $this->assertCronRuns(array(
1719 array(
1720 // Almost 2 months.
1721 'time' => '2012-08-13 01:00:00',
1722 'recipients' => array(),
1723 ),
1724 array(
1725 // After the 2-month mark, send an email.
1726 'time' => '2012-08-16 01:00:00',
1727 'recipients' => array(array('test-event@example.com')),
1728 ),
1729 array(
1730 // After 2 months and 1 week, don't repeat yet.
1731 'time' => '2012-08-23 02:00:00',
1732 'recipients' => array(),
1733 ),
1734 array(
1735 // After 2 months and 2 weeks
1736 'time' => '2012-08-30 02:00:00',
1737 'recipients' => array(array('test-event@example.com')),
1738 ),
1739 array(
1740 // After 2 months and 4 week
1741 'time' => '2012-09-13 02:00:00',
1742 'recipients' => array(array('test-event@example.com')),
1743 ),
1744 array(
1745 // After 2 months and 6 weeks
1746 'time' => '2012-09-27 01:00:00',
1747 'recipients' => array(),
1748 ),
1749 ));
1750 }
1751
1752 // TODO // function testMembershipEndDate_NonMatch() { }
1753 // TODO // function testEventTypeStartDate_Match() { }
1754 // TODO // function testEventTypeEndDate_Match() { }
1755 // TODO // function testEventNameStartDate_Match() { }
1756 // TODO // function testEventNameEndDate_Match() { }
1757
1758 /**
1759 * Run a series of cron jobs and make an assertion about email deliveries.
1760 *
1761 * @param array $cronRuns
1762 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1763 * - time: string, e.g. '2012-06-15 21:00:01'
1764 * - recipients: array(array(string)), list of email addresses which should receive messages
1765 */
1766 public function assertCronRuns($cronRuns) {
1767 foreach ($cronRuns as $cronRun) {
1768 CRM_Utils_Time::setTime($cronRun['time']);
1769 $this->callAPISuccess('job', 'send_reminder', array());
1770 $this->mut->assertRecipients($cronRun['recipients']);
1771 if (array_key_exists('subjects', $cronRun)) {
1772 $this->mut->assertSubjects($cronRun['subjects']);
1773 }
1774 $this->mut->clearMessages();
1775 }
1776 }
1777
1778 /**
1779 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1780 */
1781 private $_testObjects;
1782
1783 /**
1784 * Sets up the fixture, for example, opens a network connection.
1785 *
1786 * This method is called before a test is executed.
1787 */
1788 protected function _setUp() {
1789 $this->_testObjects = array();
1790 }
1791
1792 /**
1793 * Tears down the fixture, for example, closes a network connection.
1794 *
1795 * This method is called after a test is executed.
1796 */
1797 protected function _tearDown() {
1798 parent::tearDown();
1799 $this->deleteTestObjects();
1800 }
1801
1802 /**
1803 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
1804 * created entities and provides for brainless cleanup.
1805 *
1806 * @see CRM_Core_DAO::createTestObject
1807 *
1808 * @param $daoName
1809 * @param array $params
1810 * @param int $numObjects
1811 * @param bool $createOnly
1812 *
1813 * @return array|NULL|object
1814 */
1815 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
1816 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
1817 if (is_array($objects)) {
1818 $this->registerTestObjects($objects);
1819 }
1820 else {
1821 $this->registerTestObjects(array($objects));
1822 }
1823 return $objects;
1824 }
1825
1826 /**
1827 * @param array $objects
1828 * DAO or BAO objects.
1829 */
1830 public function registerTestObjects($objects) {
1831 //if (is_object($objects)) {
1832 // $objects = array($objects);
1833 //}
1834 foreach ($objects as $object) {
1835 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1836 $this->_testObjects[$daoName][] = $object->id;
1837 }
1838 }
1839
1840 public function deleteTestObjects() {
1841 // Note: You might argue that the FK relations between test
1842 // objects could make this problematic; however, it should
1843 // behave intuitively as long as we mentally split our
1844 // test-objects between the "manual/primary records"
1845 // and the "automatic/secondary records"
1846 foreach ($this->_testObjects as $daoName => $daoIds) {
1847 foreach ($daoIds as $daoId) {
1848 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
1849 }
1850 }
1851 $this->_testObjects = array();
1852 }
1853
1854 /**
1855 * Test that the various repetition units work correctly.
1856 * CRM-17028
1857 */
1858 public function testRepetitionFrequencyUnit() {
1859 $membershipTypeParams = array(
1860 'duration_interval' => '1',
1861 'duration_unit' => 'year',
1862 'is_active' => 1,
1863 'period_type' => 'rolling',
1864 );
1865 $membershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', $membershipTypeParams);
1866 $interval_units = array('hour', 'day', 'week', 'month', 'year');
1867 foreach ($interval_units as $interval_unit) {
1868 $membershipEndDate = DateTime::createFromFormat('Y-m-d H:i:s', "2013-03-15 00:00:00");
1869 $contactParams = array(
1870 'contact_type' => 'Individual',
1871 'first_name' => 'Test',
1872 'last_name' => "Interval $interval_unit",
1873 'is_deceased' => 0,
1874 );
1875 $contact = $this->createTestObject('CRM_Contact_DAO_Contact', $contactParams);
1876 $this->assertTrue(is_numeric($contact->id));
1877 $emailParams = array(
1878 'contact_id' => $contact->id,
1879 'email' => "test-member-{$interval_unit}@example.com",
1880 'location_type_id' => 1,
1881 );
1882 $email = $this->createTestObject('CRM_Core_DAO_Email', $emailParams);
1883 $this->assertTrue(is_numeric($email->id));
1884 $membershipParams = array(
1885 'membership_type_id' => $membershipType->id,
1886 'contact_id' => $contact->id,
1887 'join_date' => '20120315',
1888 'start_date' => '20120315',
1889 'end_date' => '20130315',
1890 'is_override' => 0,
1891 'status_id' => 2,
1892 );
1893 $membershipParams['status-id'] = 1;
1894 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $membershipParams);
1895 $actionScheduleParams = $this->fixtures['sched_on_membership_end_date_repeat_interval'];
1896 $actionScheduleParams['entity_value'] = $membershipType->id;
1897 $actionScheduleParams['repetition_frequency_unit'] = $interval_unit;
1898 $actionScheduleParams['repetition_frequency_interval'] = 2;
1899 $actionSchedule = CRM_Core_BAO_ActionSchedule::add($actionScheduleParams);
1900 $this->assertTrue(is_numeric($actionSchedule->id));
1901 $beforeEndDate = $this->createModifiedDateTime($membershipEndDate, '-1 day');
1902 $beforeFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+1 $interval_unit");
1903 $afterFirstUnit = $this->createModifiedDateTime($membershipEndDate, "+2 $interval_unit");
1904 $cronRuns = array(
1905 array(
1906 'time' => $beforeEndDate->format('Y-m-d H:i:s'),
1907 'recipients' => array(),
1908 ),
1909 array(
1910 'time' => $membershipEndDate->format('Y-m-d H:i:s'),
1911 'recipients' => array(array("test-member-{$interval_unit}@example.com")),
1912 ),
1913 array(
1914 'time' => $beforeFirstUnit->format('Y-m-d H:i:s'),
1915 'recipients' => array(),
1916 ),
1917 array(
1918 'time' => $afterFirstUnit->format('Y-m-d H:i:s'),
1919 'recipients' => array(array("test-member-{$interval_unit}@example.com")),
1920 ),
1921 );
1922 $this->assertCronRuns($cronRuns);
1923 $actionSchedule->delete();
1924 $membership->delete();
1925 }
1926 }
1927
1928 public function createModifiedDateTime($origDateTime, $modifyRule) {
1929 $newDateTime = clone($origDateTime);
1930 $newDateTime->modify($modifyRule);
1931 return $newDateTime;
1932 }
1933
1934 }