3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_RecurringEntityTest
16 class CRM_Core_BAO_RecurringEntityTest
extends CiviUnitTestCase
{
19 * Sets up the fixture, for example, opens a network connection.
20 * This method is called before a test is executed.
22 protected function setUp() {
27 * Tears down the fixture, for example, closes a network connection.
28 * This method is called after a test is executed.
30 protected function tearDown() {
34 * Testing Activity Generation through Entity Recursion.
36 public function testActivityGeneration() {
37 //Activity set initial params
38 $daoActivity = new CRM_Activity_DAO_Activity();
39 $daoActivity->activity_type_id
= 1;
40 $daoActivity->subject
= "Initial Activity";
41 $daoActivity->activity_date_time
= '20141002103000';
44 $recursion = new CRM_Core_BAO_RecurringEntity();
45 $recursion->entity_id
= $daoActivity->id
;
46 $recursion->entity_table
= 'civicrm_activity';
47 $recursion->dateColumns
= ['activity_date_time'];
48 $recursion->schedule
= [
49 'entity_value' => $daoActivity->id
,
50 'start_action_date' => $daoActivity->activity_date_time
,
51 'entity_status' => 'fourth saturday',
52 'repetition_frequency_unit' => 'month',
53 'repetition_frequency_interval' => 3,
54 'start_action_offset' => 5,
55 'used_for' => 'activity',
58 $generatedEntities = $recursion->generate();
59 $this->assertEquals(5, count($generatedEntities['civicrm_activity']), "Cehck if number of iterations are 5");
67 foreach ($generatedEntities['civicrm_activity'] as $entityID) {
68 $this->assertDBNotNull('CRM_Activity_DAO_Activity', $entityID, 'id',
69 'id', 'Check DB if repeating activities were created'
73 // set mode to ALL, i.e any change to changing activity affects all related recurring activities
76 // lets change subject of initial activity that we created in beginning
77 $daoActivity->find(TRUE);
78 $daoActivity->subject
= 'Changed Activity';
81 // check if other activities were affected
83 foreach ($generatedEntities['civicrm_activity'] as $entityID) {
84 $this->assertDBCompareValue('CRM_Activity_DAO_Activity', $entityID, 'subject', 'id', 'Changed Activity', 'Check if subject was updated');
85 $actualDates[] = date('YmdHis', strtotime(CRM_Core_DAO
::getFieldValue('CRM_Activity_DAO_Activity', $entityID, 'activity_date_time', 'id')));
87 $resultDates = array_diff($actualDates, $expectedDates);
88 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
93 * Creating action schedule
95 private function createActionSchedule($entity_id, $entity_table) {
97 "used_for" => $entity_table,
98 "entity_value" => $entity_id,
99 "start_action_date" => date("YmdHis"),
100 "repetition_frequency_unit" => "week",
101 "repetition_frequency_interval" => "3",
102 "start_action_condition" => "monday,tuesday,wednesday,thursday,friday,saturday",
103 "start_action_offset" => "2",
105 $actionScheduleObj = CRM_Core_BAO_ActionSchedule
::add($params);
106 return $actionScheduleObj;
110 * Creating recurring entities
112 private function createRecurringEntities($actionScheduleObj, $entity_id, $entity_table) {
113 $recursion = new CRM_Core_BAO_RecurringEntity();
114 $recursion->dateColumns
= [
117 $recursion->scheduleId
= $actionScheduleObj->id
;
118 $recursion->entity_id
= $entity_id;
119 $recursion->entity_table
= $entity_table;
120 $recursion->linkedEntities
= [
122 "table" => "civicrm_price_set_entity",
124 "entity_id" => $entity_id,
125 "entity_table" => $entity_table,
130 "isRecurringEntityRecord" => FALSE,
133 return $recursion->generate();
137 * Testing Event Generation through Entity Recursion.
139 public function testRepeatEventCreation() {
140 $event = $this->eventCreate();
141 $entity_table = "civicrm_event";
142 $entity_id = $event["id"];
143 CRM_Price_BAO_PriceSet
::addTo($entity_table, $entity_id, 1);
144 $actionScheduleObj = $this->createActionSchedule($entity_id, $entity_table);
145 $recurringEntities = $this->createRecurringEntities($actionScheduleObj, $entity_id, $entity_table);
146 $finalResult = CRM_Core_BAO_RecurringEntity
::updateModeAndPriceSet($entity_id, $entity_table, CRM_Core_BAO_RecurringEntity
::MODE_ALL_ENTITY_IN_SERIES
, [], 2);
147 $this->assertEquals(2, count($recurringEntities["civicrm_event"]), "Recurring events not created.");
148 $this->assertEquals(2, count($recurringEntities["civicrm_price_set_entity"]), "Recurring price sets not created.");
149 $priceSetOne = CRM_Price_BAO_PriceSet
::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][0]);
150 $priceSetTwo = CRM_Price_BAO_PriceSet
::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][1]);
151 $this->assertEquals(2, $priceSetOne, "Price set id of the recurring event is not updated.");
152 $this->assertEquals(2, $priceSetTwo, "Price set id of the recurring event is not updated.");
156 * Testing Event Generation through Entity Recursion.
158 public function testEventGeneration() {
159 //Event set initial params
160 $daoEvent = new CRM_Event_DAO_Event();
161 $daoEvent->title
= 'Test event for Recurring Entity';
162 $daoEvent->event_type_id
= 3;
163 $daoEvent->is_public
= 1;
164 $daoEvent->start_date
= date('YmdHis', strtotime('2014-10-26 10:30:00'));
165 $daoEvent->end_date
= date('YmdHis', strtotime('2014-10-28 10:30:00'));
166 $daoEvent->created_date
= date('YmdHis');
167 $daoEvent->is_active
= 1;
169 $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id
, 'id', 'id', 'Check DB if event was created');
171 //Create tell a friend for event
172 $daoTellAFriend = new CRM_Friend_DAO_Friend();
173 $daoTellAFriend->entity_table
= 'civicrm_event';
175 $daoTellAFriend->entity_id
= $daoEvent->id
;
176 $daoTellAFriend->title
= 'Testing tell a friend';
177 $daoTellAFriend->is_active
= 1;
178 $daoTellAFriend->save();
179 $this->assertDBNotNull('CRM_Friend_DAO_Friend', $daoTellAFriend->id
, 'id', 'id', 'Check DB if tell a friend was created');
181 // time to use recursion
182 $recursion = new CRM_Core_BAO_RecurringEntity();
183 $recursion->entity_id
= $daoEvent->id
;
184 $recursion->entity_table
= 'civicrm_event';
185 $recursion->dateColumns
= ['start_date'];
186 $recursion->schedule
= [
187 'entity_value' => $daoEvent->id
,
188 'start_action_date' => $daoEvent->start_date
,
189 'start_action_condition' => 'monday',
190 'repetition_frequency_unit' => 'week',
191 'repetition_frequency_interval' => 1,
192 'start_action_offset' => 4,
193 'used_for' => 'event',
196 $recursion->linkedEntities
= [
198 'table' => 'civicrm_tell_friend',
200 'entity_id' => $recursion->entity_id
,
201 'entity_table' => 'civicrm_event',
203 'linkedColumns' => ['entity_id'],
204 'isRecurringEntityRecord' => TRUE,
208 $interval = $recursion->getInterval($daoEvent->start_date
, $daoEvent->end_date
);
209 $recursion->intervalDateColumns
= ['end_date' => $interval];
210 $generatedEntities = $recursion->generate();
211 $this->assertArrayHasKey('civicrm_event', $generatedEntities, 'Check if generatedEntities has civicrm_event as required key');
213 '20141027103000' => '20141029103000',
214 '20141103103000' => '20141105103000',
215 '20141110103000' => '20141112103000',
216 '20141117103000' => '20141119103000',
219 $this->assertCount($recursion->schedule
['start_action_offset'], $generatedEntities['civicrm_event'], 'Check if the number of events created are right');
221 foreach ($generatedEntities['civicrm_event'] as $key => $val) {
222 $this->assertDBNotNull('CRM_Event_DAO_Event', $val, 'id', 'id', 'Check if repeating events were created.');
223 $startDate = date('YmdHis', strtotime(CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $val, 'start_date', 'id')));
224 $endDate = date('YmdHis', strtotime(CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $val, 'end_date', 'id')));
225 $actualDates[$startDate] = $endDate;
228 $resultDates = array_diff($actualDates, $expectedDates);
229 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
231 foreach ($generatedEntities['civicrm_tell_friend'] as $key => $val) {
232 $this->assertDBNotNull('CRM_Friend_DAO_Friend', $val, 'id', 'id', 'Check if friends were created in loop');
233 $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');
235 $this->assertCount($recursion->schedule
['start_action_offset'], $generatedEntities['civicrm_tell_friend'], 'Check if the number of tell a friend records are right');
237 // set mode to ALL, i.e any change to changing event affects all related recurring activities
240 $daoEvent->find(TRUE);
241 $daoEvent->title
= 'Event Changed';
244 // check if other events were affected
245 foreach ($generatedEntities['civicrm_event'] as $entityID) {
246 $this->assertDBCompareValue('CRM_Event_DAO_Event', $entityID, 'title', 'id', 'Event Changed', 'Check if title was updated');
249 end($generatedEntities['civicrm_event']);
250 $key = key($generatedEntities['civicrm_event']);
252 end($generatedEntities['civicrm_tell_friend']);
253 $actKey = key($generatedEntities['civicrm_tell_friend']);
255 //Check if both(event/tell a friend) keys are same
256 $this->assertEquals($key, $actKey, "Check if both the keys are same");
258 //Cross check event exists before we test deletion
259 $searchParamsEventBeforeDelete = [
260 'entity_id' => $generatedEntities['civicrm_event'][$key],
261 'entity_table' => 'civicrm_event',
263 $expectedValuesEventBeforeDelete = [
264 'entity_id' => $generatedEntities['civicrm_event'][$key],
265 'entity_table' => 'civicrm_event',
267 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsEventBeforeDelete, $expectedValuesEventBeforeDelete);
269 //Cross check event exists before we test deletion
270 $searchParamsTellAFriendBeforeDelete = [
271 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
272 'entity_table' => 'civicrm_tell_friend',
274 $expectedValuesTellAFriendBeforeDelete = [
275 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
276 'entity_table' => 'civicrm_tell_friend',
278 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsTellAFriendBeforeDelete, $expectedValuesTellAFriendBeforeDelete);
280 //Delete an event from recurring set and respective linked entity should be deleted from civicrm_recurring_entity_table
281 $daoRecurEvent = new CRM_Event_DAO_Event();
282 $daoRecurEvent->id
= $generatedEntities['civicrm_event'][$key];
283 if ($daoRecurEvent->find(TRUE)) {
284 $daoRecurEvent->delete();
287 //Check if this event_id was deleted
288 $this->assertDBNull('CRM_Event_DAO_Event', $generatedEntities['civicrm_event'][$key], 'id', 'id', 'Check if event was deleted');
290 'entity_id' => $generatedEntities['civicrm_event'][$key],
291 'entity_table' => 'civicrm_event',
294 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParams, $compareParams);
296 //Find tell_a_friend id if that was deleted from civicrm
298 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
299 'entity_table' => 'civicrm_tell_friend',
301 $compareActParams = [];
302 $this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams);
306 * Testing Activity Generation through Entity Recursion with Custom Data and Tags.
308 public function testRecurringEntityGenerationWithCustomDataAndTags() {
310 // Create custom group and field
311 $customGroup = $this->customGroupCreate([
312 'extends' => 'Activity',
314 $customField = $this->customFieldCreate([
315 'custom_group_id' => $customGroup['id'],
316 'default_value' => '',
319 // Create activity Tag
320 $tag = $this->tagCreate([
321 'used_for' => 'Activities',
324 // Create original activity
325 $customFieldValue = 'Custom Value';
326 $activityDateTime = date('YmdHis');
327 $activityId = $this->activityCreate([
328 'activity_date_time' => $activityDateTime,
329 'custom_' . $customField['id'] => $customFieldValue,
332 $activityId = $activityId['id'];
334 // Assign tag to a activity.
335 $this->callAPISuccess('EntityTag', 'create', [
336 'entity_table' => 'civicrm_activity',
337 'entity_id' => $activityId,
338 'tag_id' => $tag['id'],
341 // Create recurring activities.
342 $recursion = new CRM_Core_BAO_RecurringEntity();
343 $recursion->entity_id
= $activityId;
344 $recursion->entity_table
= 'civicrm_activity';
345 $recursion->dateColumns
= ['activity_date_time'];
346 $recursion->schedule
= [
347 'entity_value' => $activityId,
348 'start_action_date' => $activityDateTime,
349 'entity_status' => 'fourth saturday',
350 'repetition_frequency_unit' => 'month',
351 'repetition_frequency_interval' => 3,
352 'start_action_offset' => 3,
353 'used_for' => 'activity',
356 $generatedEntities = $recursion->generate();
357 $generatedActivities = $generatedEntities['civicrm_activity'];
359 $this->assertEquals(3, count($generatedActivities), "Check if number of iterations are 3");
361 foreach ($generatedActivities as $generatedActivityId) {
363 /* Validate tag in recurring activity
364 // @todo - refer https://github.com/civicrm/civicrm-core/pull/13470
365 $this->callAPISuccess('EntityTag', 'getsingle', [
366 'entity_table' => 'civicrm_activity',
367 'entity_id' => $generatedActivityId,
371 // Validate custom data in recurring activity
372 $activity = $this->callAPISuccess('activity', 'getsingle', [
374 'custom_' . $customField['id'],
376 'id' => $generatedActivityId,
379 $this->assertEquals($customFieldValue, $activity['custom_' . $customField['id']], 'Custom field value should be ' . $customFieldValue);