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