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