Merge pull request #15948 from eileenmcnaughton/export_test
[civicrm-core.git] / tests / phpunit / CRM / Upgrade / Incremental / BaseTest.php
1 <?php
2
3 /**
4 * Class CRM_UF_Page_ProfileEditorTest
5 * @group headless
6 */
7 class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
8 use CRMTraits_Custom_CustomDataTrait;
9
10 public function tearDown() {
11 $this->quickCleanup(['civicrm_saved_search']);
12 }
13
14 /**
15 * Test message upgrade process.
16 */
17 public function testMessageTemplateUpgrade() {
18 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
19
20 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
21 foreach ($templates as $template) {
22 $originalText = $template['msg_text'];
23 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
24 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
25 $this->assertEquals('great what a cool member you are', $msg_text);
26 }
27 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
28 $messageTemplateObject->updateTemplates();
29
30 foreach ($templates as $template) {
31 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
32 $this->assertContains('{assign var="greeting" value="{contact.email_greeting}"}{if $greeting}{$greeting},{/if}', $msg_text);
33 if ($msg_text !== $originalText) {
34 // Reset value for future tests.
35 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
36 }
37 }
38 }
39
40 /**
41 * Test message upgrade process only edits the default if the template is customised.
42 */
43 public function testMessageTemplateUpgradeAlreadyCustomised() {
44 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
45
46 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
47 foreach ($templates as $template) {
48 if ($template['is_reserved']) {
49 $originalText = $template['msg_text'];
50 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
51 }
52 else {
53 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
54 }
55 }
56 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
57 $messageTemplateObject->updateTemplates();
58
59 foreach ($templates as $template) {
60 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
61 if ($template['is_reserved']) {
62 $this->assertContains('{assign var="greeting" value="{contact.email_greeting}"}{if $greeting}{$greeting},{/if}', $msg_text);
63 }
64 else {
65 $this->assertEquals('great what a silly sausage you are', $msg_text);
66 }
67
68 if ($msg_text !== $originalText) {
69 // Reset value for future tests.
70 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
71 }
72 }
73 }
74
75 /**
76 * Test function for messages on upgrade.
77 */
78 public function testMessageTemplateGetUpgradeMessages() {
79 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
80 $messages = $messageTemplateObject->getUpgradeMessages();
81 $this->assertEquals([
82 'Memberships - Receipt (on-line)' => 'Use email greeting at top where available',
83 'Contributions - Receipt (on-line)' => 'Use email greeting at top where available',
84 'Events - Registration Confirmation and Receipt (on-line)' => 'Use email greeting at top where available',
85 ], $messages);
86 }
87
88 /**
89 * Test converting a datepicker field.
90 */
91 public function testSmartGroupDatePickerConversion() {
92 $this->callAPISuccess('SavedSearch', 'create', [
93 'form_values' => [
94 ['grant_application_received_date_high', '=', '01/20/2019'],
95 ['grant_due_date_low', '=', '01/22/2019'],
96 ],
97 ]);
98 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
99 $smartGroupConversionObject->updateGroups([
100 'datepickerConversion' => [
101 'grant_application_received_date',
102 'grant_decision_date',
103 'grant_money_transfer_date',
104 'grant_due_date',
105 ],
106 ]);
107 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
108 $this->assertEquals('grant_application_received_date_high', $savedSearch['form_values'][0][0]);
109 $this->assertEquals('2019-01-20 23:59:59', $savedSearch['form_values'][0][2]);
110 $this->assertEquals('grant_due_date_low', $savedSearch['form_values'][1][0]);
111 $this->assertEquals('2019-01-22 00:00:00', $savedSearch['form_values'][1][2]);
112 $hasRelative = FALSE;
113 foreach ($savedSearch['form_values'] as $form_value) {
114 if ($form_value[0] === 'grant_due_date_relative') {
115 $hasRelative = TRUE;
116 }
117 }
118 $this->assertEquals(TRUE, $hasRelative);
119 }
120
121 /**
122 * Test Multiple Relative Date conversions
123 */
124 public function testSmartGroupMultipleRelatvieDateConversions() {
125 $this->callAPISuccess('SavedSearch', 'create', [
126 'form_values' => [
127 ['membership_join_date_low', '=', '20190903000000'],
128 ['membership_join_date_high', '=', '20190903235959'],
129 ['membership_start_date_low', '=' , '20190901000000'],
130 ['membership_start_date_high', '=', '20190907235959'],
131 ['membership_end_date_low', '=', '20190901000000'],
132 ['membership_end_date_high', '=', '20190907235959'],
133 'relative_dates' => [
134 'member_join' => 'this.day',
135 'member_start' => 'this.week',
136 'member_end' => 'this.week',
137 ],
138 ],
139 ]);
140 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
141 $smartGroupConversionObject->updateGroups([
142 'datepickerConversion' => [
143 'membership_join_date',
144 'membership_start_date',
145 'membership_end_date',
146 ],
147 ]);
148 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
149 $this->assertContains('6', array_keys($savedSearch['form_values']));
150 $this->assertEquals('membership_join_date_relative', $savedSearch['form_values'][6][0]);
151 $this->assertEquals('this.day', $savedSearch['form_values'][6][2]);
152 $this->assertContains('7', array_keys($savedSearch['form_values']));
153 $this->assertEquals('membership_start_date_relative', $savedSearch['form_values'][7][0]);
154 $this->assertEquals('this.week', $savedSearch['form_values'][7][2]);
155 $this->assertContains('8', array_keys($savedSearch['form_values']));
156 $this->assertEquals('membership_end_date_relative', $savedSearch['form_values'][8][0]);
157 $this->assertEquals('this.week', $savedSearch['form_values'][8][2]);
158 }
159
160 /**
161 * Test upgrading multiple Event smart groups of different formats
162 */
163 public function testMultipleEventSmartGroupDateConversions() {
164 $this->callAPISuccess('SavedSearch', 'create', [
165 'form_values' => [
166 ['event_start_date_low', '=', '20191001000000'],
167 ['event_end_date_high', '=', '20191031235959'],
168 'relative_dates' => [
169 'event' => 'this.month',
170 ],
171 ],
172 ]);
173 $this->callAPISuccess('SavedSearch', 'create', [
174 'form_values' => [
175 ['event_start_date_low', '=', '20191001000000'],
176 ],
177 ]);
178 $this->callAPISuccess('SavedSearch', 'create', [
179 'form_values' => [
180 'event_start_date_low' => '20191001000000',
181 'event_end_date_high' => '20191031235959',
182 'event_relative' => 'this.month',
183 ],
184 ]);
185 $this->callAPISuccess('SavedSearch', 'create', [
186 'form_values' => [
187 'event_start_date_low' => '10/01/2019',
188 'event_end_date_high' => '',
189 'event_relative' => '0',
190 ],
191 ]);
192 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
193 $smartGroupConversionObject->renameFields([
194 ['old' => 'event_start_date_low', 'new' => 'event_low'],
195 ['old' => 'event_end_date_high', 'new' => 'event_high'],
196 ]);
197 $smartGroupConversionObject->updateGroups([
198 'datepickerConversion' => [
199 'event',
200 ],
201 ]);
202 $expectedResults = [
203 1 => [
204 'relative_dates' => [],
205 2 => ['event_relative', '=', 'this.month'],
206 ],
207 2 => [
208 0 => ['event_low', '=', '2019-10-01 00:00:00'],
209 1 => ['event_relative', '=', 0],
210 ],
211 3 => [
212 'event_relative' => 'this.month',
213 ],
214 4 => [
215 'event_relative' => 0,
216 'event_low' => '2019-10-01 00:00:00',
217 ],
218 ];
219 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
220 foreach ($savedSearches['values'] as $id => $savedSearch) {
221 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
222 }
223 }
224
225 /**
226 * Test Log Date conversion
227 */
228 public function testLogDateConversion() {
229 // Create two sets of searches one set for added by and one for modified by
230 // Each set contains a relative search on this.month and a specific date search low
231 $this->callAPISuccess('SavedSearch', 'create', [
232 'form_values' => [
233 ['log_date', '=', 1],
234 ['log_date_low', '=', '20191001000000'],
235 ['log_date_high', '=', '20191031235959'],
236 'relative_dates' => [
237 'log' => 'this.month',
238 ],
239 ],
240 ]);
241 $this->callAPISuccess('SavedSearch', 'create', [
242 'form_values' => [
243 ['log_date', '=', 1],
244 ['log_date_low', '=', '20191001000000'],
245 ],
246 ]);
247 $this->callAPISuccess('SavedSearch', 'create', [
248 'form_values' => [
249 ['log_date', '=', 2],
250 ['log_date_low', '=', '20191001000000'],
251 ['log_date_high', '=', '20191031235959'],
252 'relative_dates' => [
253 'log' => 'this.month',
254 ],
255 ],
256 ]);
257 $this->callAPISuccess('SavedSearch', 'create', [
258 'form_values' => [
259 ['log_date', '=', 2],
260 ['log_date_low', '=', '20191001000000'],
261 ],
262 ]);
263 // On the original search form you didn't need to select the log_date radio
264 // If it wasn't selected it defaulted to created_date filtering.
265 $this->callAPISuccess('SavedSearch', 'create', [
266 'form_values' => [
267 ['log_date_low', '=', '20191001000000'],
268 ['log_date_high', '=', '20191031235959'],
269 'relative_dates' => [
270 'log' => 'this.month',
271 ],
272 ],
273 ]);
274 $this->callAPISuccess('SavedSearch', 'create', [
275 'form_values' => [
276 ['log_date_low', '=', '20191001000000'],
277 ],
278 ]);
279 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
280 $smartGroupConversionObject->renameLogFields();
281 $smartGroupConversionObject->updateGroups([
282 'datepickerConversion' => [
283 'created_date',
284 'modified_date',
285 ],
286 ]);
287 $savedSearhes = $this->callAPISuccess('SavedSearch', 'get', []);
288 $expectedResults = [
289 1 => [
290 0 => ['log_date', '=', 1],
291 'relative_dates' => [],
292 3 => ['created_date_relative', '=', 'this.month'],
293 ],
294 2 => [
295 0 => ['log_date', '=', 1],
296 1 => ['created_date_low', '=', '2019-10-01 00:00:00'],
297 2 => ['created_date_relative', '=', 0],
298 ],
299 3 => [
300 0 => ['log_date', '=', 2],
301 'relative_dates' => [],
302 3 => ['modified_date_relative', '=', 'this.month'],
303 ],
304 4 => [
305 0 => ['log_date', '=', 2],
306 1 => ['modified_date_low', '=', '2019-10-01 00:00:00'],
307 2 => ['modified_date_relative', '=', 0],
308 ],
309 5 => [
310 'relative_dates' => [],
311 2 => ['created_date_relative', '=', 'this.month'],
312 ],
313 6 => [
314 0 => ['created_date_low', '=', '2019-10-01 00:00:00'],
315 1 => ['created_date_relative', '=', 0],
316 ],
317 ];
318 }
319
320 /**
321 * Test converting relationship fields
322 */
323 public function testSmartGroupRelationshipDateConversions() {
324 $this->callAPISuccess('SavedSearch', 'create', [
325 'form_values' => [
326 ['relationship_start_date_low', '=', '20191001000000'],
327 ['relationship_start_date_high', '=', '20191031235959'],
328 ['relationship_end_date_low', '=', '20191001000000'],
329 ['relationship_end_date_high', '=', '20191031235959'],
330 'relative_dates' => [
331 'relation_start' => 'this.month',
332 'relation_end' => 'this.month',
333 ],
334 ],
335 ]);
336 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
337 $smartGroupConversionObject->updateGroups([
338 'datepickerConversion' => [
339 'relationship_start_date',
340 'relationship_end_date',
341 ],
342 ]);
343 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
344 $this->assertEquals([], $savedSearch['form_values']['relative_dates']);
345 $this->assertEquals(['relationship_start_date_relative', '=', 'this.month'], $savedSearch['form_values'][4]);
346 $this->assertEquals(['relationship_end_date_relative', '=', 'this.month'], $savedSearch['form_values'][5]);
347 }
348
349 /**
350 * Test convert custom saved search
351 */
352 public function testSmartGroupCustomDateRangeSearch() {
353 $this->entity = 'Contact';
354 $this->createCustomGroupWithFieldOfType([], 'date');
355 $dateCustomFieldName = $this->getCustomFieldName('date');
356 $this->callAPISuccess('SavedSearch', 'create', [
357 'form_values' => [
358 [$dateCustomFieldName . '_relative', '=', 0],
359 [$dateCustomFieldName, '=', ['BETWEEN' => ['20191001000000', '20191031235959']]],
360 ],
361 ]);
362 $this->callAPISuccess('SavedSearch', 'create', [
363 'form_values' => [
364 [$dateCustomFieldName . '_relative', '=', 0],
365 [$dateCustomFieldName, '=', ['>=' => '20191001000000']],
366 ],
367 ]);
368 $this->callAPISuccess('SavedSearch', 'create', [
369 'form_values' => [
370 [$dateCustomFieldName . '_relative', '=', 0],
371 [$dateCustomFieldName, '=', ['<=' => '20191031235959']],
372 ],
373 ]);
374 $this->callAPISuccess('SavedSearch', 'create', [
375 'form_values' => [
376 [$dateCustomFieldName . '_relative', '=', 'this.month'],
377 ],
378 ]);
379 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
380 $smartGroupConversionObject->convertCustomSmartGroups();
381 $expectedResults = [
382 1 => [
383 0 => [$dateCustomFieldName . '_relative', '=', 0],
384 2 => [$dateCustomFieldName . '_low', '=', '2019-10-01 00:00:00'],
385 3 => [$dateCustomFieldName . '_high', '=', '2019-10-31 23:59:59'],
386 ],
387 2 => [
388 0 => [$dateCustomFieldName . '_relative', '=', 0],
389 2 => [$dateCustomFieldName . '_low', '=', '2019-10-01 00:00:00'],
390 ],
391 3 => [
392 0 => [$dateCustomFieldName . '_relative', '=', 0],
393 2 => [$dateCustomFieldName . '_high', '=', '2019-10-31 23:59:59'],
394 ],
395 4 => [
396 0 => [$dateCustomFieldName . '_relative', '=', 'this.month'],
397 ],
398 ];
399 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
400 foreach ($savedSearches['values'] as $id => $savedSearch) {
401 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
402 }
403 }
404
405 /**
406 * Test conversion of on hold group.
407 */
408 public function testOnHoldConversion() {
409 $this->callAPISuccess('SavedSearch', 'create', [
410 'form_values' => [
411 ['on_hold', '=', '1'],
412 ],
413 ]);
414 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups('5.11.alpha1');
415 $smartGroupConversionObject->convertEqualsStringToInArray('on_hold');
416 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
417 $this->assertEquals('IN', $savedSearch['form_values'][0][1]);
418 $this->assertEquals(['1'], $savedSearch['form_values'][0][2]);
419
420 }
421
422 /**
423 * Test renaming a field.
424 */
425 public function testRenameField() {
426 $this->callAPISuccess('SavedSearch', 'create', [
427 'form_values' => [
428 ['activity_date_low', '=', '01/22/2019'],
429 ],
430 ]);
431 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
432 $smartGroupConversionObject->renameField('activity_date_low', 'activity_date_time_low');
433 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
434 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
435 }
436
437 /**
438 * Test renaming multiple fields.
439 *
440 * @throws Exception
441 */
442 public function testRenameFields() {
443 $this->callAPISuccess('SavedSearch', 'create', [
444 'form_values' => [
445 ['activity_date_low', '=', '01/22/2019'],
446 ['activity_date_relative', '=', 0],
447 ],
448 ]);
449 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
450 $smartGroupConversionObject->renameFields([
451 ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
452 ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
453 ]);
454 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
455 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
456 $this->assertEquals('activity_date_time_relative', $savedSearch['form_values'][1][0]);
457 }
458
459 /**
460 * Test that a mis-saved variable in 'contribute settings' can be converted to a
461 * 'proper' setting.
462 */
463 public function testConvertUpgradeContributeSettings() {
464 Civi::settings()->set('contribution_invoice_settings', ['foo' => 'bar', 'deferred_revenue_enabled' => 1]);
465 $this->assertEquals(0, Civi::settings()->get('deferred_revenue_enabled'));
466 CRM_Upgrade_Incremental_Base::updateContributeSettings(NULL, 5.1);
467 $this->assertEquals(1, Civi::settings()->get('deferred_revenue_enabled'));
468 }
469
470 /**
471 * dev/core#1405 Test fixing option groups with spaces in the name
472 */
473 public function testFixOptionGroupName() {
474 $name = 'This is a test Name';
475 $fixedName = CRM_Utils_String::titleToVar(strtolower($name));
476 $optionGroup = $this->callAPISuccess('OptionGroup', 'create', [
477 'title' => 'Test Option Group',
478 'name' => $name,
479 ]);
480 // API is hardened to strip the spaces to lets re-add in now
481 CRM_Core_DAO::executeQuery("UPDATE civicrm_option_group SET name = %1 WHERE id = %2", [
482 1 => [$name, 'String'],
483 2 => [$optionGroup['id'], 'Positive'],
484 ]);
485 $preUpgrade = $this->callAPISuccess('OptionGroup', 'getsingle', ['id' => $optionGroup['id']]);
486 $this->assertEquals($name, $preUpgrade['name']);
487 CRM_Upgrade_Incremental_php_FiveTwentyOne::fixOptionGroupName();
488 $postUpgrade = $this->callAPISuccess('OptionGroup', 'getsingle', ['id' => $optionGroup['id']]);
489 $this->assertEquals($fixedName, $postUpgrade['name'], 'Ensure that the spaces have been removed from OptionGroup name');
490 $this->assertEquals($postUpgrade['name'], $optionGroup['values'][$optionGroup['id']]['name'], 'Ensure that the fixed name matches what the API would produce');
491 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $optionGroup['id']]);
492 }
493
494 /**
495 * Test that if there is an option group name as the same as the proposed fix name that doesn't cause a hard fail in the upgrade
496 */
497 public function testFixOptionGroupNameWithFixedNameInDatabase() {
498 $name = 'This is a test Name';
499 $fixedName = CRM_Utils_String::titleToVar(strtolower($name));
500 $optionGroup = $this->callAPISuccess('OptionGroup', 'create', [
501 'title' => 'Test Option Group',
502 'name' => $name,
503 ]);
504 // API is hardened to strip the spaces to lets re-add in now
505 CRM_Core_DAO::executeQuery("UPDATE civicrm_option_group SET name = %1 WHERE id = %2", [
506 1 => [$name, 'String'],
507 2 => [$optionGroup['id'], 'Positive'],
508 ]);
509 $optionGroup2 = $this->callAPISuccess('OptionGroup', 'create', [
510 'title' => 'Test Option Group 2',
511 'name' => $name,
512 ]);
513 $preUpgrade = $this->callAPISuccess('OptionGroup', 'getsingle', ['id' => $optionGroup['id']]);
514 $this->assertEquals($name, $preUpgrade['name']);
515 $preUpgrade = $this->callAPISuccess('OptionGroup', 'getsingle', ['id' => $optionGroup2['id']]);
516 $this->assertEquals($fixedName, $preUpgrade['name']);
517 CRM_Upgrade_Incremental_php_FiveTwentyOne::fixOptionGroupName();
518 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $optionGroup['id']]);
519 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $optionGroup2['id']]);
520 }
521
522 }