From: Andrew Hunt Date: Tue, 8 Jul 2014 15:43:36 +0000 (-0400) Subject: CRM-14879 tests for reminders on contact date fields X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=43030baffefa8e2935185e99a4d337cce44da510;p=civicrm-core.git CRM-14879 tests for reminders on contact date fields ---------------------------------------- * CRM-14879: Scheduled reminders for contact date fields https://issues.civicrm.org/jira/browse/CRM-14879 --- diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 1ac9deb7fc..836ce4c81a 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -1066,7 +1066,7 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL $status_ = explode(',', $status); if (in_array(2, $status_)) { // anniversary mode: - $dateField = 'DATE_ADD(e.' . $dateDBField . ', INTERVAL ROUND(DATEDIFF(NOW(), e.' . $dateDBField . ') / 365) YEAR)'; + $dateField = 'DATE_ADD(e.' . $dateDBField . ', INTERVAL ROUND(DATEDIFF(DATE(' . $now . '), e.' . $dateDBField . ') / 365) YEAR)'; $anniversary = true; } else { diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index f675060e43..7de69679f8 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -93,6 +93,13 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { 'contact_type' => 'Individual', 'email' => 'test-member@example.com', ); + $this->fixtures['contact_birthdate'] = array( // API + 'version' => 3, + 'is_deceased' => 0, + 'contact_type' => 'Individual', + 'email' => 'test-bday@example.com', + 'birth_date' => '20050707', + ); $this->fixtures['sched_activity_1day'] = array( // create() 'name' => 'One_Day_Phone_Call_Notice', 'title' => 'One Day Phone Call Notice', @@ -240,6 +247,124 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { 'subject' => 'subject sched_membership_end_2month', ); + $this->fixtures['sched_contact_bday_yesterday'] = array( // create() + 'name' => 'sched_contact_bday_yesterday', + 'title' => 'sched_contact_bday_yesterday', + 'absolute_date' => '', + 'body_html' => '

you look like you were born yesterday!

', + 'body_text' => 'you look like you were born yesterday!', + 'end_action' => '', + 'end_date' => '', + 'end_frequency_interval' => '', + 'end_frequency_unit' => '', + 'entity_status' => 1, + 'entity_value' => 'birth_date', + 'group_id' => '', + 'is_active' => 1, + 'is_repeat' => '0', + 'mapping_id' => 6, + 'msg_template_id' => '', + 'recipient' => '', + 'recipient_listing' => '', + 'recipient_manual' => '', + 'record_activity' => 1, + 'repetition_frequency_interval' => '', + 'repetition_frequency_unit' => '', + 'start_action_condition' => 'after', + 'start_action_date' => 'date_field', + 'start_action_offset' => '1', + 'start_action_unit' => 'day', + 'subject' => 'subject sched_contact_bday_yesterday', + ); + + $this->fixtures['sched_contact_bday_anniv'] = array( // create() + 'name' => 'sched_contact_bday_anniv', + 'title' => 'sched_contact_bday_anniv', + 'absolute_date' => '', + 'body_html' => '

happy birthday!

', + 'body_text' => 'happy birthday!', + 'end_action' => '', + 'end_date' => '', + 'end_frequency_interval' => '', + 'end_frequency_unit' => '', + 'entity_status' => 2, + 'entity_value' => 'birth_date', + 'group_id' => '', + 'is_active' => 1, + 'is_repeat' => '0', + 'mapping_id' => 6, + 'msg_template_id' => '', + 'recipient' => '', + 'recipient_listing' => '', + 'recipient_manual' => '', + 'record_activity' => 1, + 'repetition_frequency_interval' => '', + 'repetition_frequency_unit' => '', + 'start_action_condition' => 'before', + 'start_action_date' => 'date_field', + 'start_action_offset' => '1', + 'start_action_unit' => 'day', + 'subject' => 'subject sched_contact_bday_anniv', + ); + + $this->fixtures['sched_contact_grad_tomorrow'] = array( // create() + 'name' => 'sched_contact_grad_tomorrow', + 'title' => 'sched_contact_grad_tomorrow', + 'absolute_date' => '', + 'body_html' => '

congratulations on your graduation!

', + 'body_text' => 'congratulations on your graduation!', + 'end_action' => '', + 'end_date' => '', + 'end_frequency_interval' => '', + 'end_frequency_unit' => '', + 'entity_status' => 1, + 'group_id' => '', + 'is_active' => 1, + 'is_repeat' => '0', + 'mapping_id' => 6, + 'msg_template_id' => '', + 'recipient' => '', + 'recipient_listing' => '', + 'recipient_manual' => '', + 'record_activity' => 1, + 'repetition_frequency_interval' => '', + 'repetition_frequency_unit' => '', + 'start_action_condition' => 'before', + 'start_action_date' => 'date_field', + 'start_action_offset' => '1', + 'start_action_unit' => 'day', + 'subject' => 'subject sched_contact_grad_tomorrow', + ); + + $this->fixtures['sched_contact_grad_anniv'] = array( // create() + 'name' => 'sched_contact_grad_anniv', + 'title' => 'sched_contact_grad_anniv', + 'absolute_date' => '', + 'body_html' => '

dear alum, please send us money.

', + 'body_text' => 'dear alum, please send us money.', + 'end_action' => '', + 'end_date' => '', + 'end_frequency_interval' => '', + 'end_frequency_unit' => '', + 'entity_status' => 2, + 'group_id' => '', + 'is_active' => 1, + 'is_repeat' => '0', + 'mapping_id' => 6, + 'msg_template_id' => '', + 'recipient' => '', + 'recipient_listing' => '', + 'recipient_manual' => '', + 'record_activity' => 1, + 'repetition_frequency_interval' => '', + 'repetition_frequency_unit' => '', + 'start_action_condition' => 'after', + 'start_action_date' => 'date_field', + 'start_action_offset' => '1', + 'start_action_unit' => 'week', + 'subject' => 'subject sched_contact_grad_anniv', + ); + $this->_setUp(); $this->quickCleanup(array('civicrm_action_log', 'civicrm_action_schedule')); } @@ -469,7 +594,137 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { )); } + function testContactBirthDate_noAnniv() { + $contact = civicrm_api('Contact', 'create', $this->fixtures['contact_birthdate']); + $this->assertAPISuccess($contact); + $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id']; + $actionSchedule = $this->fixtures['sched_contact_bday_yesterday']; + $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); + $this->assertTrue(is_numeric($actionScheduleDao->id)); + $this->assertCronRuns(array( + array( // On the birthday, no email + 'time' => '2005-07-07 01:00:00', + 'recipients' => array(), + ), + array( // The next day, send an email + 'time' => '2005-07-08 20:00:00', + 'recipients' => array(array('test-bday@example.com')), + ), + )); + } + + function testContactBirthDate_Anniv() { + $contact = civicrm_api('Contact', 'create', $this->fixtures['contact_birthdate']); + $this->assertAPISuccess($contact); + $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id']; + $actionSchedule = $this->fixtures['sched_contact_bday_anniv']; + $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); + $this->assertTrue(is_numeric($actionScheduleDao->id)); + $this->assertCronRuns(array( + array( // On some random day, no email + 'time' => '2014-03-07 01:00:00', + 'recipients' => array(), + ), + array( // On the eve of their 9th birthday, send an email + 'time' => '2014-07-06 20:00:00', + 'recipients' => array(array('test-bday@example.com')), + ), + )); + } + function testContactCustomDate_noAnniv() { + $group = array( + 'title' => 'Test_Group', + 'name' => 'test_group', + 'extends' => array('Individual'), + 'style' => 'Inline', + 'is_multiple' => false, + 'is_active' => 1, + 'version' => 3, + ); + $createGroup = civicrm_api('custom_group', 'create', $group); + $this->assertAPISuccess($createGroup); + $field = array( + 'version' => 3, + 'label' => 'Graduation', + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'custom_group_id' => $createGroup['id'], + ); + $createField = civicrm_api('custom_field', 'create', $field); + $this->assertAPISuccess($createField); + $contactParams = $this->fixtures['contact']; + $contactParams["custom_{$createField['id']}"] = '2013-12-16'; + $contact = civicrm_api('Contact', 'create', $contactParams); + $this->assertAPISuccess($contact); + $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id']; + $actionSchedule = $this->fixtures['sched_contact_grad_tomorrow']; + $actionSchedule['entity_value'] = "custom_{$createField['id']}"; + $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); + $this->assertTrue(is_numeric($actionScheduleDao->id)); + $this->assertCronRuns(array( + array( // On some random day, no email + 'time' => '2014-03-07 01:00:00', + 'recipients' => array(), + ), + array( // On the eve of their graduation, send an email + 'time' => '2013-12-15 20:00:00', + 'recipients' => array(array('test-member@example.com')), + ), + )); + $deleteParams = array( + 'version' => 3, + 'id' => $createGroup['id'], + ); + $deleteGroup = civicrm_api('custom_group', 'delete', $deleteParams); + } + + function testContactCustomDate_Anniv() { + $group = array( + 'title' => 'Test_Group now', + 'name' => 'test_group_now', + 'extends' => array('Individual'), + 'style' => 'Inline', + 'is_multiple' => false, + 'is_active' => 1, + 'version' => 3, + ); + $createGroup = civicrm_api('custom_group', 'create', $group); + $this->assertAPISuccess($createGroup); + $field = array( + 'version' => 3, + 'label' => 'Graduation', + 'data_type' => 'Date', + 'html_type' => 'Select Date', + 'custom_group_id' => $createGroup['id'], + ); + $createField = civicrm_api('custom_field', 'create', $field); + $this->assertAPISuccess($createField); + $contactParams = $this->fixtures['contact']; + $contactParams["custom_{$createField['id']}"] = '2013-12-16'; + $contact = civicrm_api('Contact', 'create', $contactParams); + $this->assertAPISuccess($contact); + $this->_testObjects['CRM_Contact_DAO_Contact'][] = $contact['id']; + $actionSchedule = $this->fixtures['sched_contact_grad_anniv']; + $actionSchedule['entity_value'] = "custom_{$createField['id']}"; + $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); + $this->assertTrue(is_numeric($actionScheduleDao->id)); + $this->assertCronRuns(array( + array( // On some random day, no email + 'time' => '2014-03-07 01:00:00', + 'recipients' => array(), + ), + array( // A week after their 5th anniversary of graduation, send an email + 'time' => '2018-12-23 20:00:00', + 'recipients' => array(array('test-member@example.com')), + ), + )); + $deleteParams = array( + 'version' => 3, + 'id' => $createGroup['id'], + ); + $deleteGroup = civicrm_api('custom_group', 'delete', $deleteParams); + } // TODO // function testMembershipEndDate_NonMatch() { } // TODO // function testEventTypeStartDate_Match() { }