Added unit-tests and other fixes
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / ActionScheduleTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 );
d2868251 209 $this->fixtures['sched_on_membership_end_date'] = array( // create()
210 'name' => 'sched_on_membership_end_date',
211 'title' => 'sched_on_membership_end_date',
212 'body_html' => '<p>Your membership expired today</p>',
213 'body_text' => 'Your membership expired today',
214 'is_active' => 1,
215 'mapping_id' => 4,
216 'record_activity' => 1,
217 'start_action_condition' => 'after',
218 'start_action_date' => 'membership_end_date',
219 'start_action_offset' => '0',
220 'start_action_unit' => 'hour',
221 'subject' => 'subject send reminder on membership_end_date',
222 );
223 $this->fixtures['sched_after_1day_membership_end_date'] = array( // create()
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 );
567b2076 385 $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'] = array(
861d11c4
DG
386 'name' => 'sched_membership_end_2month',
387 'title' => 'sched_membership_end_2month',
388 'absolute_date' => '',
389 'body_html' => '<p>body sched_membership_end_2month</p>',
390 'body_text' => 'body sched_membership_end_2month',
391 'end_action' => '',
5c4d6559 392 'end_date' => 'membership_end_date',
861d11c4
DG
393 'end_frequency_interval' => '4',
394 'end_frequency_unit' => 'month',
395 'entity_status' => '',
396 'entity_value' => '',
397 'group_id' => '',
398 'is_active' => 1,
399 'is_repeat' => '1',
400 'mapping_id' => 4,
401 'msg_template_id' => '',
402 'recipient' => '',
403 'recipient_listing' => '',
404 'recipient_manual' => '',
405 'record_activity' => 1,
406 'repetition_frequency_interval' => '4',
407 'repetition_frequency_unit' => 'week',
408 'start_action_condition' => 'after',
409 'start_action_date' => 'membership_end_date',
410 'start_action_offset' => '2',
411 'start_action_unit' => 'month',
412 'subject' => 'subject sched_membership_end_2month',
413 );
567b2076 414 $this->fixtures['sched_membership_end_limit_to_none'] = array(
baa85770
EM
415 'name' => 'limit to none',
416 'title' => 'limit to none',
417 'absolute_date' => '',
418 'body_html' => '<p>body sched_membership_end_2month</p>',
419 'body_text' => 'body sched_membership_end_2month',
420 'end_action' => '',
421 'end_date' => '',
422 'end_frequency_interval' => '4',
423 'end_frequency_unit' => 'month',
424 'entity_status' => '',
425 'entity_value' => '',
426 'limit_to' => 0,
427 'group_id' => '',
428 'is_active' => 1,
429 'is_repeat' => '1',
430 'mapping_id' => 4,
431 'msg_template_id' => '',
432 'recipient' => '',
433 'recipient_listing' => '',
434 'recipient_manual' => '',
435 'record_activity' => 1,
436 'repetition_frequency_interval' => '4',
437 'repetition_frequency_unit' => 'week',
438 'start_action_condition' => 'after',
439 'start_action_date' => 'membership_end_date',
440 'start_action_offset' => '2',
441 'start_action_unit' => 'month',
442 'subject' => 'limit to none',
443 );
6a488035 444 $this->_setUp();
6a488035
TO
445 }
446
447 /**
448 * Tears down the fixture, for example, closes a network connection.
92c99a4a 449 *
6a488035 450 * This method is called after a test is executed.
6a488035 451 */
00be9182 452 public function tearDown() {
6a488035
TO
453 parent::tearDown();
454
455 $this->mut->clearMessages();
456 $this->mut->stop();
457 unset($this->mut);
92c99a4a
EM
458 $this->quickCleanup(array(
459 'civicrm_action_schedule',
460 'civicrm_action_log',
461 'civicrm_membership',
462 'civicrm_email',
463 ));
6a488035
TO
464 $this->_tearDown();
465 }
466
92c99a4a 467 public function testActivityDateTimeMatchNonRepeatableSchedule() {
cee19268 468 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day']);
6a488035
TO
469 $this->assertTrue(is_numeric($actionScheduleDao->id));
470
471 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
6a488035 472 $this->assertTrue(is_numeric($activity->id));
6c6e6187 473 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
6a488035
TO
474 $activity->save();
475
4f20f356 476 $source['contact_id'] = $contact['id'];
477 $source['activity_id'] = $activity->id;
478 $source['record_type_id'] = 2;
479 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
480 $activityContact->save();
481
6a488035 482 $this->assertCronRuns(array(
567b2076
EM
483 array(
484 // Before the 24-hour mark, no email
6a488035
TO
485 'time' => '2012-06-14 04:00:00',
486 'recipients' => array(),
487 ),
567b2076
EM
488 array(
489 // After the 24-hour mark, an email
6a488035
TO
490 'time' => '2012-06-14 15:00:00',
491 'recipients' => array(array('test-member@example.com')),
492 ),
567b2076
EM
493 array(
494 // Run cron again; message already sent
6a488035
TO
495 'time' => '',
496 'recipients' => array(),
497 ),
498 ));
499 }
500
92c99a4a 501 public function testActivityDateTimeMatchRepeatableSchedule() {
cee19268 502 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_activity_1day_r']);
6a488035
TO
503 $this->assertTrue(is_numeric($actionScheduleDao->id));
504
505 $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']);
506 $this->assertTrue(is_numeric($activity->id));
6c6e6187 507 $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
6a488035
TO
508 $activity->save();
509
4f20f356 510 $source['contact_id'] = $contact['id'];
511 $source['activity_id'] = $activity->id;
6c6e6187 512 $source['record_type_id'] = 2;
4f20f356 513 $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
514 $activityContact->save();
515
6a488035 516 $this->assertCronRuns(array(
567b2076
EM
517 array(
518 // Before the 24-hour mark, no email
6a488035
TO
519 'time' => '012-06-14 04:00:00',
520 'recipients' => array(),
521 ),
567b2076
EM
522 array(
523 // After the 24-hour mark, an email
6a488035
TO
524 'time' => '2012-06-14 15:00:00',
525 'recipients' => array(array('test-member@example.com')),
526 ),
567b2076
EM
527 array(
528 // Run cron 4 hours later; first message already sent
6a488035
TO
529 'time' => '2012-06-14 20:00:00',
530 'recipients' => array(),
531 ),
567b2076
EM
532 array(
533 // Run cron 6 hours later; send second message.
6a488035
TO
534 'time' => '2012-06-14 21:00:01',
535 'recipients' => array(array('test-member@example.com')),
536 ),
537 ));
538 }
539
540 /**
541 * For contacts/activities which don't match the schedule filter,
542 * an email should *not* be sent.
543 */
544 // TODO // function testActivityDateTime_NonMatch() { }
545
546 /**
547 * For contacts/members which match schedule based on join date,
548 * an email should be sent.
549 */
92c99a4a 550 public function testMembershipJoinDateMatch() {
b29be8c2 551 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1)));
6a488035 552 $this->assertTrue(is_numeric($membership->id));
bf308d29 553 $result = $this->callAPISuccess('Email', 'create', array(
6a488035
TO
554 'contact_id' => $membership->contact_id,
555 'email' => 'test-member@example.com',
556 'location_type_id' => 1,
92915c55 557 ));
6a488035
TO
558 $this->assertAPISuccess($result);
559
bf308d29 560 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
6a488035
TO
561 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
562 $actionSchedule['entity_value'] = $membership->membership_type_id;
cee19268 563 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
6a488035
TO
564 $this->assertTrue(is_numeric($actionScheduleDao->id));
565
566 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
567 $this->assertCronRuns(array(
567b2076
EM
568 array(
569 // Before the 2-week mark, no email.
6a488035
TO
570 'time' => '2012-03-28 01:00:00',
571 'recipients' => array(),
572 ),
567b2076
EM
573 array(
574 // After the 2-week mark, send an email.
6a488035
TO
575 'time' => '2012-03-29 01:00:00',
576 'recipients' => array(array('test-member@example.com')),
577 ),
578 ));
579 }
580
581 /**
442cf836
EM
582 * Test end date email sent.
583 *
6a488035
TO
584 * For contacts/members which match schedule based on join date,
585 * an email should be sent.
586 */
92c99a4a 587 public function testMembershipJoinDateNonMatch() {
6a488035
TO
588 $membership = $this->createTestObject('CRM_Member_DAO_Membership', $this->fixtures['rolling_membership']);
589 $this->assertTrue(is_numeric($membership->id));
bf308d29 590 $result = $this->callAPISuccess('Email', 'create', array(
6a488035
TO
591 'contact_id' => $membership->contact_id,
592 'location_type_id' => 1,
593 'email' => 'test-member@example.com',
6a488035 594 ));
fe265b4e 595
6a488035
TO
596 // Add an alternative membership type, and only send messages for that type
597 $extraMembershipType = $this->createTestObject('CRM_Member_DAO_MembershipType', array());
598 $this->assertTrue(is_numeric($extraMembershipType->id));
cee19268 599 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($this->fixtures['sched_membership_join_2week']);
6a488035
TO
600 $this->assertTrue(is_numeric($actionScheduleDao->id));
601 $actionScheduleDao->entity_value = $extraMembershipType->id;
602 $actionScheduleDao->save();
603
604 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
605 $this->assertCronRuns(array(
567b2076
EM
606 array(
607 // After the 2-week mark, don't send email because we have different membership type.
6a488035
TO
608 'time' => '2012-03-29 01:00:00',
609 'recipients' => array(),
610 ),
611 ));
612 }
613
861d11c4 614 /**
442cf836 615 * Test that the first and SECOND notifications are sent out.
861d11c4 616 */
92c99a4a 617 public function testMembershipEndDateRepeat() {
861d11c4
DG
618 // creates membership with end_date = 20120615
619 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
620 $result = $this->callAPISuccess('Email', 'create', array(
621 'contact_id' => $membership->contact_id,
622 'email' => 'test-member@example.com',
623 ));
624 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
625
626 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
627 $actionSchedule['entity_value'] = $membership->membership_type_id;
628 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
629
630 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
631 $this->assertCronRuns(array(
567b2076
EM
632 array(
633 // After the 2-week mark, send an email.
861d11c4
DG
634 'time' => '2012-08-15 01:00:00',
635 'recipients' => array(array('test-member@example.com')),
636 ),
567b2076
EM
637 array(
638 // After the 2-week mark, send an email.
861d11c4
DG
639 'time' => '2012-09-12 01:00:00',
640 'recipients' => array(array('test-member@example.com')),
641 ),
642 ));
643 }
644
645 /**
442cf836
EM
646 * Test behaviour when date changes.
647 *
861d11c4
DG
648 * Test that the first notification is sent but the second is NOT sent if the end date changes in
649 * between
650 * see CRM-15376
651 */
92c99a4a 652 public function testMembershipEndDateRepeatChangedEndDate_CRM_15376() {
861d11c4
DG
653 // creates membership with end_date = 20120615
654 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
655 $this->callAPISuccess('Email', 'create', array(
656 'contact_id' => $membership->contact_id,
657 'email' => 'test-member@example.com',
658 ));
659 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
660
661 $actionSchedule = $this->fixtures['sched_membership_end_2month_repeat_twice_4_weeks'];
662 $actionSchedule['entity_value'] = $membership->membership_type_id;
663 $this->callAPISuccess('action_schedule', 'create', $actionSchedule);
664 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
665 $this->assertCronRuns(array(
567b2076
EM
666 array(
667 // After the 2-week mark, send an email.
861d11c4
DG
668 'time' => '2012-08-15 01:00:00',
669 'recipients' => array(array('test-member@example.com')),
670 ),
671 ));
672
442cf836 673 // Extend membership - reminder should NOT go out.
861d11c4
DG
674 $this->callAPISuccess('membership', 'create', array('id' => $membership->id, 'end_date' => '2014-01-01'));
675 $this->assertCronRuns(array(
442cf836
EM
676 array(
677 // After the 2-week mark, send an email.
861d11c4
DG
678 'time' => '2012-09-12 01:00:00',
679 'recipients' => array(),
680 ),
681 ));
682 }
683
6a488035 684 /**
442cf836
EM
685 * Test membership end date email sends.
686 *
6a488035
TO
687 * For contacts/members which match schedule based on end date,
688 * an email should be sent.
689 */
92c99a4a 690 public function testMembershipEndDateMatch() {
6a488035 691 // creates membership with end_date = 20120615
b29be8c2 692 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
6a488035 693 $this->assertTrue(is_numeric($membership->id));
bf308d29 694 $this->callAPISuccess('Email', 'create', array(
6a488035
TO
695 'contact_id' => $membership->contact_id,
696 'email' => 'test-member@example.com',
92915c55 697 ));
bf308d29 698 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
6a488035
TO
699
700 $actionSchedule = $this->fixtures['sched_membership_end_2week'];
701 $actionSchedule['entity_value'] = $membership->membership_type_id;
cee19268 702 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
6a488035
TO
703 $this->assertTrue(is_numeric($actionScheduleDao->id));
704
705 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
706 $this->assertCronRuns(array(
442cf836
EM
707 array(
708 // Before the 2-week mark, no email.
6a488035 709 'time' => '2012-05-31 01:00:00',
442cf836
EM
710 // 'time' => '2012-06-01 01:00:00',
711 // FIXME: Is this the right boundary?
6a488035
TO
712 'recipients' => array(),
713 ),
442cf836
EM
714 array(
715 // After the 2-week mark, send an email.
6a488035
TO
716 'time' => '2012-06-01 01:00:00',
717 'recipients' => array(array('test-member@example.com')),
718 ),
719 ));
d2868251 720
721 // Now suppose user has renewed for rolling membership after 3 months, so upcoming assertion is written
722 // to ensure that new reminder is sent 2 week before the new end_date i.e. '2012-09-15'
723 $membership->end_date = '2012-09-15';
724 $membership->save();
725
726 //change the email id of chosen membership contact to assert
727 //recipient of not the previously sent mail but the new one
728 $result = $this->callAPISuccess('Email', 'create', array(
729 'is_primary' => 1,
730 'contact_id' => $membership->contact_id,
731 'email' => 'member2@example.com'
732 ));
733 $this->assertAPISuccess($result);
734
735 // end_date=2012-09-15 ; schedule is 2 weeks before end_date
736 $this->assertCronRuns(array(
737 array( // Before the 2-week mark, no email
738 'time' => '2012-08-31 01:00:00',
739 'recipients' => array(),
740 ),
741 /* TODO
742 array( // After the 2-week mark, send an email
743 'time' => '2012-09-01 01:00:00',
744 'recipients' => array(array('member2@example.com')),
745 ),
746 */
747 ));
6a488035
TO
748 }
749
4b1efa1d 750
751 /**
442cf836
EM
752 * Test membership end date email.
753 *
6c6e6187
TO
754 * For contacts/members which match schedule based on end date,
755 * an email should be sent.
756 */
92c99a4a 757 public function testMembershipEndDateNoMatch() {
4b1efa1d 758 // creates membership with end_date = 20120615
5c4d6559 759 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 3)));
4b1efa1d 760 $this->assertTrue(is_numeric($membership->id));
6c6e6187 761 $result = $this->callAPISuccess('Email', 'create', array(
4b1efa1d 762 'contact_id' => $membership->contact_id,
763 'email' => 'test-member@example.com',
4b1efa1d 764 ));
bf308d29 765 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
4b1efa1d 766
767 $actionSchedule = $this->fixtures['sched_membership_end_2month'];
768 $actionSchedule['entity_value'] = $membership->membership_type_id;
cee19268 769 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
4b1efa1d 770 $this->assertTrue(is_numeric($actionScheduleDao->id));
771
772 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
773 $this->assertCronRuns(array(
442cf836
EM
774 array(
775 // Before the 2-week mark, no email.
4b1efa1d 776 'time' => '2012-05-31 01:00:00',
442cf836
EM
777 // 'time' => '2012-06-01 01:00:00',
778 // FIXME: Is this the right boundary?
4b1efa1d 779 'recipients' => array(),
780 ),
442cf836
EM
781 array(
782 // After the 2-week mark, send an email.
4b1efa1d 783 'time' => '2013-05-01 01:00:00',
784 'recipients' => array(),
785 ),
786 ));
787 }
788
92c99a4a 789 public function testContactBirthDateNoAnniv() {
bf308d29 790 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
43030baf
AH
791 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
792 $actionSchedule = $this->fixtures['sched_contact_bday_yesterday'];
793 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
794 $this->assertTrue(is_numeric($actionScheduleDao->id));
795 $this->assertCronRuns(array(
442cf836
EM
796 array(
797 // On the birthday, no email.
43030baf
AH
798 'time' => '2005-07-07 01:00:00',
799 'recipients' => array(),
800 ),
442cf836
EM
801 array(
802 // The next day, send an email.
43030baf
AH
803 'time' => '2005-07-08 20:00:00',
804 'recipients' => array(array('test-bday@example.com')),
805 ),
806 ));
807 }
808
92c99a4a 809 public function testContactBirthDateAnniversary() {
6c6e6187 810 $contact = $this->callAPISuccess('Contact', 'create', $this->fixtures['contact_birthdate']);
43030baf
AH
811 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
812 $actionSchedule = $this->fixtures['sched_contact_bday_anniv'];
813 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
814 $this->assertTrue(is_numeric($actionScheduleDao->id));
815 $this->assertCronRuns(array(
92c99a4a
EM
816 array(
817 // On some random day, no email.
43030baf
AH
818 'time' => '2014-03-07 01:00:00',
819 'recipients' => array(),
820 ),
92c99a4a
EM
821 array(
822 // On the eve of their 9th birthday, send an email.
43030baf
AH
823 'time' => '2014-07-06 20:00:00',
824 'recipients' => array(array('test-bday@example.com')),
825 ),
826 ));
827 }
4b1efa1d 828
92c99a4a 829 public function testContactCustomDateNoAnniv() {
43030baf
AH
830 $group = array(
831 'title' => 'Test_Group',
832 'name' => 'test_group',
833 'extends' => array('Individual'),
834 'style' => 'Inline',
6c6e6187 835 'is_multiple' => FALSE,
43030baf 836 'is_active' => 1,
43030baf 837 );
bf308d29 838 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
43030baf 839 $field = array(
43030baf
AH
840 'label' => 'Graduation',
841 'data_type' => 'Date',
842 'html_type' => 'Select Date',
843 'custom_group_id' => $createGroup['id'],
844 );
bf308d29 845 $createField = $this->callAPISuccess('custom_field', 'create', $field);
43030baf
AH
846 $contactParams = $this->fixtures['contact'];
847 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
bf308d29 848 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
43030baf
AH
849 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
850 $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow'];
851 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
852 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
853 $this->assertTrue(is_numeric($actionScheduleDao->id));
854 $this->assertCronRuns(array(
92c99a4a
EM
855 array(
856 // On some random day, no email.
43030baf
AH
857 'time' => '2014-03-07 01:00:00',
858 'recipients' => array(),
859 ),
92c99a4a
EM
860 array(
861 // On the eve of their graduation, send an email.
43030baf
AH
862 'time' => '2013-12-15 20:00:00',
863 'recipients' => array(array('test-member@example.com')),
864 ),
865 ));
6c6e6187 866 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
43030baf 867 }
92915c55 868
baa85770 869 /**
92c99a4a 870 * Check that limit_to + an empty recipients doesn't sent to multiple contacts.
baa85770 871 */
00be9182 872 public function testMembershipLimitToNone() {
baa85770
EM
873 // creates membership with end_date = 20120615
874 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
875
876 $this->assertTrue(is_numeric($membership->id));
877 $result = $this->callAPISuccess('Email', 'create', array(
878 'contact_id' => $membership->contact_id,
879 'email' => 'member@example.com',
880 ));
881 $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
882 $this->callAPISuccess('contact', 'create', array('email' => 'b@c.com', 'contact_type' => 'Individual'));
883
884 $this->assertAPISuccess($result);
885
886 $actionSchedule = $this->fixtures['sched_membership_end_limit_to_none'];
887 $actionSchedule['entity_value'] = $membership->membership_type_id;
888 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
889 $this->assertTrue(is_numeric($actionScheduleDao->id));
890
891 // end_date=2012-06-15 ; schedule is 2 weeks before end_date
892 $this->assertCronRuns(array(
92c99a4a
EM
893 array(
894 // Before the 2-week mark, no email.
baa85770
EM
895 'time' => '2012-05-31 01:00:00',
896 // 'time' => '2012-06-01 01:00:00', // FIXME: Is this the right boundary?
897 'recipients' => array(),
898 ),
899 ));
900 }
901
5c4d6559 902 function testMembership_referenceDate() {
903 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
904
905 $this->assertTrue(is_numeric($membership->id));
906 $result = $this->callAPISuccess('Email', 'create', array(
907 'contact_id' => $membership->contact_id,
908 'email' => 'member@example.com',
909 ));
910
911 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
912 $this->assertAPISuccess($result);
913
914 $actionSchedule = $this->fixtures['sched_membership_join_2week'];
915 $actionSchedule['entity_value'] = $membership->membership_type_id;
916 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
917 $this->assertTrue(is_numeric($actionScheduleDao->id));
918
919 // start_date=2012-03-15 ; schedule is 2 weeks after start_date
920 $this->assertCronRuns(array(
921 array( // After the 2-week mark, send an email
922 'time' => '2012-03-29 01:00:00',
923 'recipients' => array(array('member@example.com')),
924 )
925 ));
926
927 //check if reference date is set to membership's join date
928 //as per the action_start_date chosen for current schedule reminder
929 $this->assertEquals('2012-03-15',
930 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id')
931 );
932
933 //change current membership join date that may signifies as memberhip renewal activity
934 $membership->join_date = '2012-03-29';
935 $membership->save();
936
937 //change the email id of chosen membership contact to assert
938 //recipient of not the previously sent mail but the new one
939 $result = $this->callAPISuccess('Email', 'create', array(
940 'is_primary' => 1,
941 'contact_id' => $membership->contact_id,
942 'email' => 'member2@example.com'
943 ));
944 $this->assertAPISuccess($result);
945
946 $this->assertCronRuns(array(
947 array( // After the 2-week of the changed join date 2012-03-29, send an email
948 'time' => '2012-04-12 01:00:00',
949 'recipients' => array(array('member2@example.com')
950),
951 ),
952 ));
953
954 //To check whether the reference date is being changed to new join_date
955 $this->assertEquals('2012-03-29',
956 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $membership->contact_id, 'reference_date', 'contact_id', TRUE)
957 );
958 }
baa85770 959
d2868251 960 public function testMembershipOnMultipleReminder() {
961 $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 2)));
962 print_r($membership );
963
964 $this->assertTrue(is_numeric($membership->id));
965 $result = $this->callAPISuccess('Email', 'create', array(
966 'contact_id' => $membership->contact_id,
967 'email' => 'member@example.com',
968 ));
969
970 $result = $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id)));
971 $this->assertAPISuccess($result);
972
973 $actionScheduleBefore = $this->fixtures['sched_membership_end_2week']; // Send email 2 weeks before end_date
974 $actionScheduleOn = $this->fixtures['sched_on_membership_end_date']; // Send email on end_date/expiry date
975 $actionScheduleAfter = $this->fixtures['sched_after_1day_membership_end_date']; // Send email 1 day after end_date/grace period
976 $actionScheduleBefore['entity_value'] = $actionScheduleOn['entity_value'] = $actionScheduleAfter['entity_value'] = $membership->membership_type_id;
977 foreach (array('actionScheduleBefore', 'actionScheduleOn', 'actionScheduleAfter') as $value) {
978 $$value = CRM_Core_BAO_ActionSchedule::add($$value);
979 $this->assertTrue(is_numeric($$value->id));
980 }
981
982 $this->assertCronRuns(
983 array(
984 array( // 1day 2weeks before membership end date(MED), don't send mail
985 'time' => '2012-05-31 01:00:00',
986 'recipients' => array(),
987 ),
988 array( // 2 weeks before MED, send an email
989 'time' => '2012-06-01 01:00:00',
990 'recipients' => array(array('member@example.com')),
991 ),
992 array( // 1day before MED, don't send mail
993 'time' => '2012-06-14 01:00:00',
994 'recipients' => array(),
995 ),
996 array( // On MED, send an email
997 'time' => '2012-06-15 00:00:00',
998 'recipients' => array(array('member@example.com')),
999 ),
1000 array( // After 1day of MED, send an email
1001 'time' => '2012-06-16 01:00:00',
1002 'recipients' => array(array('member@example.com')),
1003 ),
1004 array( // After 1day 1min of MED, don't send an email
1005 'time' => '2012-06-17 00:01:00',
1006 'recipients' => array(),
1007 ),
1008 ));
1009
1010 // Assert the timestamp as of when the emails of respective three reminders as configured
1011 // 2 weeks before, on and 1 day after MED, are sent
1012 $this->assertEquals('2012-06-01 01:00:00',
1013 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleBefore->id, 'action_date_time', 'action_schedule_id', TRUE));
1014 $this->assertEquals('2012-06-15 00:00:00',
1015 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleOn->id, 'action_date_time', 'action_schedule_id', TRUE));
1016 $this->assertEquals('2012-06-16 01:00:00',
1017 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionLog', $actionScheduleAfter->id, 'action_date_time', 'action_schedule_id', TRUE));
1018
1019 //extend MED to 2 weeks after the current MED (that may signifies as memberhip renewal activity)
1020 // and lets assert as of when the new set of reminders will be sent against their respective Schedule Reminders(SR)
1021 $membership->end_date = '2012-06-29';
1022 $membership->save();
1023 print_r($membership );
1024
1025 //change the email id of chosen membership contact to assert
1026 //recipient of not the previously sent mail but the new one
1027 $result = $this->callAPISuccess('Email', 'create', array(
1028 'is_primary' => 1,
1029 'contact_id' => $membership->contact_id,
1030 'email' => 'member2@example.com'
1031 ));
1032 $this->assertAPISuccess($result);
1033
1034 $this->assertCronRuns(
1035 array(
1036 array( // 1day 2weeks before membership end date(MED), don't send mail
1037 'time' => '2012-06-14 01:00:00',
1038 'recipients' => array(),
1039 ),
1040 //TODO : Add asssertion for before, on and after SR impact on new MED
1041 /*
1042 array( // 2 weeks before MED, send an email
1043 'time' => '2012-06-15 01:00:00',
1044 'recipients' => array(array('member2@example.com')),
1045 ),
1046 array( // 1day before MED, don't send mail
1047 'time' => '2012-06-28 01:00:00',
1048 'recipients' => array(),
1049 ),
1050 array( // On MED, send an email
1051 'time' => '2012-06-29 00:00:00',
1052 'recipients' => array(array('member@example.com')),
1053 ),
1054 array( // After 1day of MED, send an email
1055 'time' => '2012-06-30 01:00:00',
1056 'recipients' => array(array('member@example.com')),
1057 ),
1058 array( // After 1day 1min of MED, don't send an email
1059 'time' => '2012-07-01 00:01:00',
1060 'recipients' => array(),
1061 ),
1062 */
1063 ));
1064 }
1065
1066 public function testContactCustomDate_Anniv() {
43030baf
AH
1067 $group = array(
1068 'title' => 'Test_Group now',
1069 'name' => 'test_group_now',
1070 'extends' => array('Individual'),
1071 'style' => 'Inline',
6c6e6187 1072 'is_multiple' => FALSE,
43030baf 1073 'is_active' => 1,
43030baf 1074 );
bf308d29 1075 $createGroup = $this->callAPISuccess('custom_group', 'create', $group);
43030baf 1076 $field = array(
43030baf
AH
1077 'label' => 'Graduation',
1078 'data_type' => 'Date',
1079 'html_type' => 'Select Date',
1080 'custom_group_id' => $createGroup['id'],
1081 );
bf308d29
EM
1082 $createField = $this->callAPISuccess('custom_field', 'create', $field);
1083
43030baf
AH
1084 $contactParams = $this->fixtures['contact'];
1085 $contactParams["custom_{$createField['id']}"] = '2013-12-16';
bf308d29 1086 $contact = $this->callAPISuccess('Contact', 'create', $contactParams);
43030baf
AH
1087 $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id'];
1088 $actionSchedule = $this->fixtures['sched_contact_grad_anniv'];
1089 $actionSchedule['entity_value'] = "custom_{$createField['id']}";
1090 $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule);
1091 $this->assertTrue(is_numeric($actionScheduleDao->id));
1092 $this->assertCronRuns(array(
92c99a4a
EM
1093 array(
1094 // On some random day, no email.
43030baf
AH
1095 'time' => '2014-03-07 01:00:00',
1096 'recipients' => array(),
1097 ),
92c99a4a
EM
1098 array(
1099 // A week after their 5th anniversary of graduation, send an email.
43030baf
AH
1100 'time' => '2018-12-23 20:00:00',
1101 'recipients' => array(array('test-member@example.com')),
1102 ),
1103 ));
6c6e6187 1104 $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id']));
43030baf 1105 }
4b1efa1d 1106
6a488035
TO
1107 // TODO // function testMembershipEndDate_NonMatch() { }
1108 // TODO // function testEventTypeStartDate_Match() { }
1109 // TODO // function testEventTypeEndDate_Match() { }
1110 // TODO // function testEventNameStartDate_Match() { }
1111 // TODO // function testEventNameEndDate_Match() { }
1112
1113 /**
92c99a4a 1114 * Run a series of cron jobs and make an assertion about email deliveries.
6a488035 1115 *
16b10e64
CW
1116 * @param array $cronRuns
1117 * array specifying when to run cron and what messages to expect; each item is an array with keys:
1118 * - time: string, e.g. '2012-06-15 21:00:01'
1119 * - recipients: array(array(string)), list of email addresses which should receive messages
6a488035 1120 */
00be9182 1121 public function assertCronRuns($cronRuns) {
6a488035
TO
1122 foreach ($cronRuns as $cronRun) {
1123 CRM_Utils_Time::setTime($cronRun['time']);
bf308d29 1124 $this->callAPISuccess('job', 'send_reminder', array());
6a488035
TO
1125 $this->mut->assertRecipients($cronRun['recipients']);
1126 $this->mut->clearMessages();
1127 }
1128 }
1129
6a488035
TO
1130 /**
1131 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
1132 */
1133 private $_testObjects;
1134
1135 /**
1136 * Sets up the fixture, for example, opens a network connection.
92c99a4a 1137 *
6a488035 1138 * This method is called before a test is executed.
6a488035
TO
1139 */
1140 protected function _setUp() {
1141 $this->_testObjects = array();
1142 }
1143
1144 /**
1145 * Tears down the fixture, for example, closes a network connection.
92c99a4a 1146 *
6a488035 1147 * This method is called after a test is executed.
6a488035
TO
1148 */
1149 protected function _tearDown() {
1150 parent::tearDown();
1151 $this->deleteTestObjects();
1152 }
1153
1154 /**
1155 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
92c99a4a 1156 * created entities and provides for brainless cleanup.
6a488035
TO
1157 *
1158 * @see CRM_Core_DAO::createTestObject
92c99a4a 1159 *
1e1fdcf6
EM
1160 * @param $daoName
1161 * @param array $params
1162 * @param int $numObjects
1163 * @param bool $createOnly
92c99a4a
EM
1164 *
1165 * @return array|NULL|object
6a488035 1166 */
2da40d21 1167 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
6a488035
TO
1168 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
1169 if (is_array($objects)) {
1170 $this->registerTestObjects($objects);
0db6c3e1
TO
1171 }
1172 else {
6a488035
TO
1173 $this->registerTestObjects(array($objects));
1174 }
1175 return $objects;
1176 }
1177
1178 /**
5a4f6742
CW
1179 * @param array $objects
1180 * DAO or BAO objects.
6a488035 1181 */
00be9182 1182 public function registerTestObjects($objects) {
6a488035
TO
1183 //if (is_object($objects)) {
1184 // $objects = array($objects);
1185 //}
1186 foreach ($objects as $object) {
1187 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
1188 $this->_testObjects[$daoName][] = $object->id;
1189 }
1190 }
1191
00be9182 1192 public function deleteTestObjects() {
6a488035
TO
1193 // Note: You might argue that the FK relations between test
1194 // objects could make this problematic; however, it should
1195 // behave intuitively as long as we mentally split our
1196 // test-objects between the "manual/primary records"
1197 // and the "automatic/secondary records"
1198 foreach ($this->_testObjects as $daoName => $daoIds) {
1199 foreach ($daoIds as $daoId) {
1200 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
1201 }
1202 }
1203 $this->_testObjects = array();
1204 }
1205
1206}