Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / RecurringEntityTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Core_BAO_RecurringEntityTest
14 * @group headless
15 */
16 class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
17
18 /**
19 * Sets up the fixture, for example, opens a network connection.
20 * This method is called before a test is executed.
21 */
22 protected function setUp() {
23 parent::setUp();
24 }
25
26 /**
27 * Tears down the fixture, for example, closes a network connection.
28 * This method is called after a test is executed.
29 */
30 protected function tearDown() {
31 }
32
33 /**
34 * Testing Activity Generation through Entity Recursion.
35 */
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';
42 $daoActivity->save();
43
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',
56 ];
57
58 $generatedEntities = $recursion->generate();
59 $this->assertEquals(5, count($generatedEntities['civicrm_activity']), "Cehck if number of iterations are 5");
60 $expectedDates = [
61 '20141025103000',
62 '20150124103000',
63 '20150425103000',
64 '20150725103000',
65 '20151024103000',
66 ];
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'
70 );
71 }
72
73 // set mode to ALL, i.e any change to changing activity affects all related recurring activities
74 $recursion->mode(3);
75
76 // lets change subject of initial activity that we created in beginning
77 $daoActivity->find(TRUE);
78 $daoActivity->subject = 'Changed Activity';
79 $daoActivity->save();
80
81 // check if other activities were affected
82 $actualDates = [];
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')));
86 }
87 $resultDates = array_diff($actualDates, $expectedDates);
88 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
89
90 }
91
92 /**
93 * Creating action schedule
94 */
95 private function createActionSchedule($entity_id, $entity_table) {
96 $params = [
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",
104 ];
105 $actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($params);
106 return $actionScheduleObj;
107 }
108
109 /**
110 * Creating recurring entities
111 */
112 private function createRecurringEntities($actionScheduleObj, $entity_id, $entity_table) {
113 $recursion = new CRM_Core_BAO_RecurringEntity();
114 $recursion->dateColumns = [
115 "start_date",
116 ];
117 $recursion->scheduleId = $actionScheduleObj->id;
118 $recursion->entity_id = $entity_id;
119 $recursion->entity_table = $entity_table;
120 $recursion->linkedEntities = [
121 [
122 "table" => "civicrm_price_set_entity",
123 "findCriteria" => [
124 "entity_id" => $entity_id,
125 "entity_table" => $entity_table,
126 ],
127 "linkedColumns" => [
128 "entity_id",
129 ],
130 "isRecurringEntityRecord" => FALSE,
131 ],
132 ];
133 return $recursion->generate();
134 }
135
136 /**
137 * Testing Event Generation through Entity Recursion.
138 */
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.");
153 }
154
155 /**
156 * Testing Event Generation through Entity Recursion.
157 */
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;
168 $daoEvent->save();
169 $this->assertDBNotNull('CRM_Event_DAO_Event', $daoEvent->id, 'id', 'id', 'Check DB if event was created');
170
171 //Create tell a friend for event
172 $daoTellAFriend = new CRM_Friend_DAO_Friend();
173 $daoTellAFriend->entity_table = 'civicrm_event';
174 // join with 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');
180
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',
194 ];
195
196 $recursion->linkedEntities = [
197 [
198 'table' => 'civicrm_tell_friend',
199 'findCriteria' => [
200 'entity_id' => $recursion->entity_id,
201 'entity_table' => 'civicrm_event',
202 ],
203 'linkedColumns' => ['entity_id'],
204 'isRecurringEntityRecord' => TRUE,
205 ],
206 ];
207
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');
212 $expectedDates = [
213 '20141027103000' => '20141029103000',
214 '20141103103000' => '20141105103000',
215 '20141110103000' => '20141112103000',
216 '20141117103000' => '20141119103000',
217 ];
218
219 $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_event'], 'Check if the number of events created are right');
220 $actualDates = [];
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;
226 }
227
228 $resultDates = array_diff($actualDates, $expectedDates);
229 $this->assertEquals(0, count($resultDates), "Check if all the value in expected array matches actual array");
230
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');
234 }
235 $this->assertCount($recursion->schedule['start_action_offset'], $generatedEntities['civicrm_tell_friend'], 'Check if the number of tell a friend records are right');
236
237 // set mode to ALL, i.e any change to changing event affects all related recurring activities
238 $recursion->mode(3);
239
240 $daoEvent->find(TRUE);
241 $daoEvent->title = 'Event Changed';
242 $daoEvent->save();
243
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');
247 }
248
249 end($generatedEntities['civicrm_event']);
250 $key = key($generatedEntities['civicrm_event']);
251
252 end($generatedEntities['civicrm_tell_friend']);
253 $actKey = key($generatedEntities['civicrm_tell_friend']);
254
255 //Check if both(event/tell a friend) keys are same
256 $this->assertEquals($key, $actKey, "Check if both the keys are same");
257
258 //Cross check event exists before we test deletion
259 $searchParamsEventBeforeDelete = [
260 'entity_id' => $generatedEntities['civicrm_event'][$key],
261 'entity_table' => 'civicrm_event',
262 ];
263 $expectedValuesEventBeforeDelete = [
264 'entity_id' => $generatedEntities['civicrm_event'][$key],
265 'entity_table' => 'civicrm_event',
266 ];
267 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsEventBeforeDelete, $expectedValuesEventBeforeDelete);
268
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',
273 ];
274 $expectedValuesTellAFriendBeforeDelete = [
275 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
276 'entity_table' => 'civicrm_tell_friend',
277 ];
278 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParamsTellAFriendBeforeDelete, $expectedValuesTellAFriendBeforeDelete);
279
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();
285 }
286
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');
289 $searchParams = [
290 'entity_id' => $generatedEntities['civicrm_event'][$key],
291 'entity_table' => 'civicrm_event',
292 ];
293 $compareParams = [];
294 $this->assertDBCompareValues('CRM_Core_DAO_RecurringEntity', $searchParams, $compareParams);
295
296 //Find tell_a_friend id if that was deleted from civicrm
297 $searchActParams = [
298 'entity_id' => $generatedEntities['civicrm_tell_friend'][$actKey],
299 'entity_table' => 'civicrm_tell_friend',
300 ];
301 $compareActParams = [];
302 $this->assertDBCompareValues('CRM_Friend_DAO_Friend', $searchActParams, $compareActParams);
303 }
304
305 /**
306 * Testing Activity Generation through Entity Recursion with Custom Data and Tags.
307 */
308 public function testRecurringEntityGenerationWithCustomDataAndTags() {
309
310 // Create custom group and field
311 $customGroup = $this->customGroupCreate([
312 'extends' => 'Activity',
313 ]);
314 $customField = $this->customFieldCreate([
315 'custom_group_id' => $customGroup['id'],
316 'default_value' => '',
317 ]);
318
319 // Create activity Tag
320 $tag = $this->tagCreate([
321 'used_for' => 'Activities',
322 ]);
323
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,
330 ]);
331
332 $activityId = $activityId['id'];
333
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'],
339 ]);
340
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',
354 ];
355
356 $generatedEntities = $recursion->generate();
357 $generatedActivities = $generatedEntities['civicrm_activity'];
358
359 $this->assertEquals(3, count($generatedActivities), "Check if number of iterations are 3");
360
361 foreach ($generatedActivities as $generatedActivityId) {
362
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,
368 ]);
369 */
370
371 // Validate custom data in recurring activity
372 $activity = $this->callAPISuccess('activity', 'getsingle', [
373 'return' => [
374 'custom_' . $customField['id'],
375 ],
376 'id' => $generatedActivityId,
377 ]);
378
379 $this->assertEquals($customFieldValue, $activity['custom_' . $customField['id']], 'Custom field value should be ' . $customFieldValue);
380
381 }
382 }
383
384 }