From 9f1cadcef6cbd1078b680a02957634b94329ea7b Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Fri, 11 May 2018 16:28:30 +0530 Subject: [PATCH 1/3] membership/issues/2 - Add membership start date and registration start/end date to schedule reminder --- CRM/Event/ActionMapping.php | 21 +++++++++++++++------ CRM/Member/ActionMapping.php | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CRM/Event/ActionMapping.php b/CRM/Event/ActionMapping.php index c58591407bc8..6f50a0b44493 100644 --- a/CRM/Event/ActionMapping.php +++ b/CRM/Event/ActionMapping.php @@ -61,8 +61,6 @@ public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\Mappi 'entity_value_label' => ts('Event Type'), 'entity_status' => 'civicrm_participant_status_type', 'entity_status_label' => ts('Participant Status'), - 'entity_date_start' => 'event_start_date', - 'entity_date_end' => 'event_end_date', ))); $registrations->register(CRM_Event_ActionMapping::create(array( 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, @@ -72,8 +70,6 @@ public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\Mappi 'entity_value_label' => ts('Event Name'), 'entity_status' => 'civicrm_participant_status_type', 'entity_status_label' => ts('Participant Status'), - 'entity_date_start' => 'event_start_date', - 'entity_date_end' => 'event_end_date', ))); $registrations->register(CRM_Event_ActionMapping::create(array( 'id' => CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID, @@ -83,11 +79,24 @@ public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\Mappi 'entity_value_label' => ts('Event Template'), 'entity_status' => 'civicrm_participant_status_type', 'entity_status_label' => ts('Participant Status'), - 'entity_date_start' => 'event_start_date', - 'entity_date_end' => 'event_end_date', ))); } + /** + * Get a list of available date fields. + * + * @return array + * Array(string $fieldName => string $fieldLabel). + */ + public function getDateFields() { + return array( + 'start_date' => ts('Event Start Date'), + 'end_date' => ts('Event End Date'), + 'registration_start_date' => ts('Registration Start Date'), + 'registration_end_date' => ts('Registration End Date'), + ); + } + /** * Get a list of recipient types. * diff --git a/CRM/Member/ActionMapping.php b/CRM/Member/ActionMapping.php index 59429f80b914..069577e9dab9 100644 --- a/CRM/Member/ActionMapping.php +++ b/CRM/Member/ActionMapping.php @@ -58,11 +58,23 @@ public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\Mappi 'entity_value_label' => ts('Membership Type'), 'entity_status' => 'auto_renew_options', 'entity_status_label' => ts('Auto Renew Options'), - 'entity_date_start' => 'membership_join_date', - 'entity_date_end' => 'membership_end_date', ))); } + /** + * Get a list of available date fields. + * + * @return array + * Array(string $fieldName => string $fieldLabel). + */ + public function getDateFields() { + return array( + 'join_date' => ts('Membership Join Date'), + 'start_date' => ts('Membership Start Date'), + 'end_date' => ts('Membership End Date'), + ); + } + /** * Generate a query to locate recipients who match the given * schedule. From 76ea51c7b145fbefd7ecea629befcac77ecfcbe8 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Tue, 22 May 2018 11:18:07 +0530 Subject: [PATCH 2/3] Add unit test --- .../CRM/Core/BAO/ActionScheduleTest.php | 105 +++++++++++++----- 1 file changed, 75 insertions(+), 30 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index 7e7fb8d6f6a4..f169fa0b8407 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -965,40 +965,43 @@ public function testActivityDateTimeMatchRepeatableScheduleOnAbsDate() { // TODO // function testActivityDateTime_NonMatch() { } /** - * For contacts/members which match schedule based on join date, + * For contacts/members which match schedule based on join/start date, * an email should be sent. */ - public function testMembershipJoinDateMatch() { - $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1))); - $this->assertTrue(is_numeric($membership->id)); - $result = $this->callAPISuccess('Email', 'create', array( - 'contact_id' => $membership->contact_id, - 'email' => 'test-member@example.com', - 'location_type_id' => 1, - )); - $this->assertAPISuccess($result); + public function testMembershipDateMatch() { + foreach (['membership_join_date', 'membership_start_date'] as $date) { + $membership = $this->createTestObject('CRM_Member_DAO_Membership', array_merge($this->fixtures['rolling_membership'], array('status_id' => 1))); + $this->assertTrue(is_numeric($membership->id)); + $result = $this->callAPISuccess('Email', 'create', array( + 'contact_id' => $membership->contact_id, + 'email' => 'test-member@example.com', + 'location_type_id' => 1, + )); + $this->assertAPISuccess($result); - $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id))); - $actionSchedule = $this->fixtures['sched_membership_join_2week']; - $actionSchedule['entity_value'] = $membership->membership_type_id; - $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); - $this->assertTrue(is_numeric($actionScheduleDao->id)); + $this->callAPISuccess('contact', 'create', array_merge($this->fixtures['contact'], array('contact_id' => $membership->contact_id))); + $actionSchedule = $this->fixtures['sched_membership_join_2week']; + $actionSchedule['start_action_date'] = $date; + $actionSchedule['entity_value'] = $membership->membership_type_id; + $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); + $this->assertTrue(is_numeric($actionScheduleDao->id)); - // start_date=2012-03-15 ; schedule is 2 weeks after start_date - $this->assertCronRuns(array( - array( - // Before the 2-week mark, no email. - 'time' => '2012-03-28 01:00:00', - 'recipients' => array(), - 'subjects' => array(), - ), - array( - // After the 2-week mark, send an email. - 'time' => '2012-03-29 01:00:00', - 'recipients' => array(array('test-member@example.com')), - 'subjects' => array('subject sched_membership_join_2week (joined March 15th, 2012)'), - ), - )); + // start_date=2012-03-15 ; schedule is 2 weeks after start_date + $this->assertCronRuns(array( + array( + // Before the 2-week mark, no email. + 'time' => '2012-03-28 01:00:00', + 'recipients' => array(), + 'subjects' => array(), + ), + array( + // After the 2-week mark, send an email. + 'time' => '2012-03-29 01:00:00', + 'recipients' => array(array('test-member@example.com')), + 'subjects' => array('subject sched_membership_join_2week (joined March 15th, 2012)'), + ), + )); + } } @@ -1667,6 +1670,48 @@ public function testContactCustomDate_Anniv() { $this->callAPISuccess('custom_group', 'delete', array('id' => $createGroup['id'])); } + /** + * Test sched reminder set via registration date. + */ + public function testEventTypeRegistrationDate() { + //Create contact + $contactParams = array( + 'email' => 'test-event@example.com', + ); + $contact = $this->individualCreate($contactParams); + //Add it as a participant to an event ending registration - 7 days from now. + $params = array( + 'start_date' => date('Ymd', strtotime('-5 day')), + 'end_date' => date('Ymd', strtotime('+7 day')), + 'registration_start_date' => date('Ymd', strtotime('-5 day')), + 'registration_end_date' => date('Ymd', strtotime('+7 day')), + ); + $event = $this->eventCreate($params); + $this->participantCreate(array('contact_id' => $contact, 'event_id' => $event['id'])); + + //Create a scheduled reminder to send email 7 days before registration date. + $actionSchedule = $this->fixtures['sched_eventtype_start_1week_before']; + $actionSchedule['start_action_offset'] = 7; + $actionSchedule['start_action_unit'] = 'day'; + $actionSchedule['start_action_date'] = 'registration_end_date'; + $actionSchedule['entity_value'] = $event['values'][$event['id']]['event_type_id']; + $actionSchedule['entity_status'] = $this->callAPISuccessGetValue('ParticipantStatusType', array( + 'return' => "id", + 'name' => "Attended", + )); + $this->callAPISuccess('action_schedule', 'create', $actionSchedule); + //Run the cron and verify if an email was sent. + $this->assertCronRuns(array( + array( + 'time' => date('Y-m-d'), + 'recipients' => array(array('test-event@example.com')), + ), + )); + } + + /** + * Test sched reminder set via start date. + */ public function testEventTypeStartDate() { // Create event+participant with start_date = 20120315, end_date = 20120615. $participant = $this->createTestObject('CRM_Event_DAO_Participant', array_merge($this->fixtures['participant'], array('status_id' => 2))); From 899bd155df6a41902e5bbd11c4fe0e603f37be02 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Mon, 4 Jun 2018 10:46:20 +0530 Subject: [PATCH 3/3] Additional unit test --- .../CRM/Core/BAO/ActionScheduleTest.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index f169fa0b8407..fa0f43d80c41 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -1699,7 +1699,7 @@ public function testEventTypeRegistrationDate() { 'return' => "id", 'name' => "Attended", )); - $this->callAPISuccess('action_schedule', 'create', $actionSchedule); + $actionSched = $this->callAPISuccess('action_schedule', 'create', $actionSchedule); //Run the cron and verify if an email was sent. $this->assertCronRuns(array( array( @@ -1707,6 +1707,40 @@ public function testEventTypeRegistrationDate() { 'recipients' => array(array('test-event@example.com')), ), )); + + //Create contact 2 + $contactParams = array( + 'email' => 'test-event2@example.com', + ); + $contact2 = $this->individualCreate($contactParams); + //Create an event with registration end date = 2 week from now. + $params['end_date'] = date('Ymd', strtotime('+2 week')); + $params['registration_end_date'] = date('Ymd', strtotime('+2 week')); + $event2 = $this->eventCreate($params); + $this->participantCreate(array('contact_id' => $contact2, 'event_id' => $event2['id'])); + + //Assert there is no reminder sent to the contact. + $this->assertCronRuns(array( + array( + 'time' => date('Y-m-d'), + 'recipients' => array(), + ), + )); + + //Modify the sched reminder to be sent 2 week from registration end date. + $this->callAPISuccess('action_schedule', 'create', array( + 'id' => $actionSched['id'], + 'start_action_offset' => 2, + 'start_action_unit' => 'week', + )); + + //Contact should receive the reminder now. + $this->assertCronRuns(array( + array( + 'time' => date('Y-m-d'), + 'recipients' => array(array('test-event2@example.com')), + ), + )); } /**