/**
  * Testing Event Generation through Entity Recursion.
  */
 public function testEventGeneration()
 {
     //Event set initial params
     $daoEvent = new CRM_Event_DAO_Event();
     $daoEvent->title = 'Test event for Recurring Entity';
     $daoEvent->event_type_id = 3;
     $daoEvent->is_public = 1;
     $daoEvent->start_date = date('YmdHis', strtotime('2014-10-26 10:30:00'));
     $daoEvent->end_date = date('YmdHis', strtotime('2014-10-28 10:30:00'));
     $daoEvent->created_date = date('YmdHis');
     $daoEvent->is_active = 1;
     $daoEvent->save();
     $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id, 'id', 'id', 'Check DB if event was created');
     //Create tell a friend for event
     $daoTellAFriend = new CRM_Friend_DAO_Friend();
     $daoTellAFriend->entity_table = 'civicrm_event';
     $daoTellAFriend->entity_id = $daoEvent->id;
     // join with event
     $daoTellAFriend->title = 'Testing tell a friend';
     $daoTellAFriend->is_active = 1;
     $daoTellAFriend->save();
     $this->assertDBNotNull('CRM_Friend_DAO_Friend', $daoTellAFriend->id, 'id', 'id', 'Check DB if tell a friend was created');
     // time to use recursion
     $recursion = new CRM_Core_BAO_RecurringEntity();
     $recursion->entity_id = $daoEvent->id;
     $recursion->entity_table = 'civicrm_event';
     $recursion->dateColumns = array('start_date');
     $recursion->schedule = array('entity_value' => $daoEvent->id, 'start_action_date' => $daoEvent->start_date, 'start_action_condition' => 'monday', 'repetition_frequency_unit' => 'week', 'repetition_frequency_interval' => 1, 'start_action_offset' => 4, 'used_for' => 'event');
     $recursion->linkedEntities = array(array('table' => 'civicrm_tell_friend', 'findCriteria' => array('entity_id' => $recursion->entity_id, 'entity_table' => 'civicrm_event'), 'linkedColumns' => array('entity_id'), 'isRecurringEntityRecord' => TRUE));
     $interval = $recursion->getInterval($daoEvent->start_date, $daoEvent->end_date);
     $recursion->intervalDateColumns = array('end_date' => $interval);
     $generatedEntities = $recursion->generate();
     $this->assertArrayHasKey('civicrm_event', $generatedEntities, 'Check if generatedEntities has civicrm_event as required key');
     $expectedDates = array('20141027103000' => '20141029103000', '20141103103000' => '20141105103000', '20141110103000' => '20141112103000', '20141117103000' => '20141119103000');
     $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_event'], 'Check if the number of events created are right');
     $actualDates = array();
     foreach ($generatedEntities['civicrm_event'] as $key => $val) {
         $this->assertDBNotNull('CRM_Event_DAO_Event', $val, 'id', 'id', 'Check if repeating events were created.');
         $startDate = date('YmdHis', strtotime(CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $val, 'start_date', 'id')));
         $endDate = date('YmdHis', strtotime(CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $val, 'end_date', 'id')));
         $actualDates[$startDate] = $endDate;
     }
     $resultDates = array_diff($actualDates, $expectedDates);
     $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
     foreach ($generatedEntities['civicrm_tell_friend'] as $key => $val) {
         $this->assertDBNotNull('CRM_Friend_DAO_Friend', $val, 'id', 'id', 'Check if friends were created in loop');
         $this->assertDBCompareValue('CRM_Friend_DAO_Friend', $val, 'entity_id', 'id', $generatedEntities['civicrm_event'][$key], 'Check DB if correct FK was maintained with event for Friend');
     }
     $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_tell_friend'], 'Check if the number of tell a friend records are right');
     // set mode to ALL, i.e any change to changing event affects all related recurring activities
     $recursion->mode(3);
     $daoEvent->find(TRUE);
     $daoEvent->title = 'Event Changed';
     $daoEvent->save();
     // check if other events were affected
     foreach ($generatedEntities['civicrm_event'] as $entityID) {
         $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated');
     }
     end($generatedEntities['civicrm_event']);
     $key = key($generatedEntities['civicrm_event']);
     end($generatedEntities['civicrm_tell_friend']);
     $actKey = key($generatedEntities['civicrm_tell_friend']);
     //Check if both(event/tell a friend) keys are same
     $this->assertEquals($key, $actKey, "Check if both the keys are same");
     //Cross check event exists before we test deletion
     $searchParamsEventBeforeDelete = array('entity_id' => $generatedEntities['civicrm_event'][$key], 'entity_table' => 'civicrm_event');
     $expectedValuesEventBeforeDelete = array('entity_id' => $generatedEntities['civicrm_event'][$key], 'entity_table' => 'civicrm_event');
     $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsEventBeforeDelete, $expectedValuesEventBeforeDelete);
     //Cross check event exists before we test deletion
     $searchParamsTellAFriendBeforeDelete = array('entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], 'entity_table' => 'civicrm_tell_friend');
     $expectedValuesTellAFriendBeforeDelete = array('entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], 'entity_table' => 'civicrm_tell_friend');
     $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsTellAFriendBeforeDelete, $expectedValuesTellAFriendBeforeDelete);
     //Delete an event from recurring set and respective linked entity should be deleted from civicrm_recurring_entity_table
     $daoRecurEvent = new CRM_Event_DAO_Event();
     $daoRecurEvent->id = $generatedEntities['civicrm_event'][$key];
     if ($daoRecurEvent->find(TRUE)) {
         $daoRecurEvent->delete();
         $daoRecurEvent->free();
     }
     //Check if this event_id was deleted
     $this->assertDBNull('CRM_Event_DAO_Event', $generatedEntities['civicrm_event'][$key], 'id', 'id', 'Check if event was deleted');
     $searchParams = array('entity_id' => $generatedEntities['civicrm_event'][$key], 'entity_table' => 'civicrm_event');
     $compareParams = array();
     $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParams, $compareParams);
     //Find tell_a_friend id if that was deleted from civicrm
     $searchActParams = array('entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey], 'entity_table' => 'civicrm_tell_friend');
     $compareActParams = array();
     $this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams);
 }