4 * Class CRM_UF_Page_ProfileEditorTest
7 class CRM_Upgrade_Incremental_BaseTest
extends CiviUnitTestCase
{
8 use CRMTraits_Custom_CustomDataTrait
;
10 public function tearDown() {
11 $this->quickCleanup(['civicrm_saved_search']);
15 * Test message upgrade process.
17 public function testMessageTemplateUpgrade() {
18 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
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);
27 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
28 $messageTemplateObject->updateTemplates();
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']]);
41 * Test message upgrade process only edits the default if the template is customised.
43 public function testMessageTemplateUpgradeAlreadyCustomised() {
44 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
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']]);
53 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
56 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
57 $messageTemplateObject->updateTemplates();
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);
65 $this->assertEquals('great what a silly sausage you are', $msg_text);
68 if ($msg_text !== $originalText) {
69 // Reset value for future tests.
70 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
76 * Test function for messages on upgrade.
78 public function testMessageTemplateGetUpgradeMessages() {
79 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
80 $messages = $messageTemplateObject->getUpgradeMessages();
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',
89 * Test converting a datepicker field.
91 public function testSmartGroupDatePickerConversion() {
92 $this->callAPISuccess('SavedSearch', 'create', [
94 ['grant_application_received_date_high', '=', '01/20/2019'],
95 ['grant_due_date_low', '=', '01/22/2019'],
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',
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') {
118 $this->assertEquals(TRUE, $hasRelative);
122 * Test Multiple Relative Date conversions
124 public function testSmartGroupMultipleRelatvieDateConversions() {
125 $this->callAPISuccess('SavedSearch', 'create', [
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',
140 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
141 $smartGroupConversionObject->updateGroups([
142 'datepickerConversion' => [
143 'membership_join_date',
144 'membership_start_date',
145 'membership_end_date',
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]);
161 * Test upgrading multiple Event smart groups of different formats
163 public function testMultipleEventSmartGroupDateConversions() {
164 $this->callAPISuccess('SavedSearch', 'create', [
166 ['event_start_date_low', '=', '20191001000000'],
167 ['event_end_date_high', '=', '20191031235959'],
168 'relative_dates' => [
169 'event' => 'this.month',
173 $this->callAPISuccess('SavedSearch', 'create', [
175 ['event_start_date_low', '=', '20191001000000'],
178 $this->callAPISuccess('SavedSearch', 'create', [
180 'event_start_date_low' => '20191001000000',
181 'event_end_date_high' => '20191031235959',
182 'event_relative' => 'this.month',
185 $this->callAPISuccess('SavedSearch', 'create', [
187 'event_start_date_low' => '10/01/2019',
188 'event_end_date_high' => '',
189 'event_relative' => '0',
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'],
197 $smartGroupConversionObject->updateGroups([
198 'datepickerConversion' => [
204 'relative_dates' => [],
205 2 => ['event_relative', '=', 'this.month'],
208 0 => ['event_low', '=', '2019-10-01 00:00:00'],
209 1 => ['event_relative', '=', 0],
212 'event_relative' => 'this.month',
215 'event_relative' => 0,
216 'event_low' => '2019-10-01 00:00:00',
219 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
220 foreach ($savedSearches['values'] as $id => $savedSearch) {
221 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
226 * Test Log Date conversion
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', [
233 ['log_date', '=', 1],
234 ['log_date_low', '=', '20191001000000'],
235 ['log_date_high', '=', '20191031235959'],
236 'relative_dates' => [
237 'log' => 'this.month',
241 $this->callAPISuccess('SavedSearch', 'create', [
243 ['log_date', '=', 1],
244 ['log_date_low', '=', '20191001000000'],
247 $this->callAPISuccess('SavedSearch', 'create', [
249 ['log_date', '=', 2],
250 ['log_date_low', '=', '20191001000000'],
251 ['log_date_high', '=', '20191031235959'],
252 'relative_dates' => [
253 'log' => 'this.month',
257 $this->callAPISuccess('SavedSearch', 'create', [
259 ['log_date', '=', 2],
260 ['log_date_low', '=', '20191001000000'],
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', [
267 ['log_date_low', '=', '20191001000000'],
268 ['log_date_high', '=', '20191031235959'],
269 'relative_dates' => [
270 'log' => 'this.month',
274 $this->callAPISuccess('SavedSearch', 'create', [
276 ['log_date_low', '=', '20191001000000'],
279 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
280 $smartGroupConversionObject->renameLogFields();
281 $smartGroupConversionObject->updateGroups([
282 'datepickerConversion' => [
287 $savedSearhes = $this->callAPISuccess('SavedSearch', 'get', []);
290 0 => ['log_date', '=', 1],
291 'relative_dates' => [],
292 3 => ['created_date_relative', '=', 'this.month'],
295 0 => ['log_date', '=', 1],
296 1 => ['created_date_low', '=', '2019-10-01 00:00:00'],
297 2 => ['created_date_relative', '=', 0],
300 0 => ['log_date', '=', 2],
301 'relative_dates' => [],
302 3 => ['modified_date_relative', '=', 'this.month'],
305 0 => ['log_date', '=', 2],
306 1 => ['modified_date_low', '=', '2019-10-01 00:00:00'],
307 2 => ['modified_date_relative', '=', 0],
310 'relative_dates' => [],
311 2 => ['created_date_relative', '=', 'this.month'],
314 0 => ['created_date_low', '=', '2019-10-01 00:00:00'],
315 1 => ['created_date_relative', '=', 0],
321 * Test converting relationship fields
323 public function testSmartGroupRelationshipDateConversions() {
324 $this->callAPISuccess('SavedSearch', 'create', [
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',
336 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
337 $smartGroupConversionObject->updateGroups([
338 'datepickerConversion' => [
339 'relationship_start_date',
340 'relationship_end_date',
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]);
350 * Test convert custom saved search
352 public function testSmartGroupCustomDateRangeSearch() {
353 $this->entity
= 'Contact';
354 $this->createCustomGroupWithFieldOfType([], 'date');
355 $dateCustomFieldName = $this->getCustomFieldName('date');
356 $this->callAPISuccess('SavedSearch', 'create', [
358 [$dateCustomFieldName . '_relative', '=', 0],
359 [$dateCustomFieldName, '=', ['BETWEEN' => ['20191001000000', '20191031235959']]],
362 $this->callAPISuccess('SavedSearch', 'create', [
364 [$dateCustomFieldName . '_relative', '=', 0],
365 [$dateCustomFieldName, '=', ['>=' => '20191001000000']],
368 $this->callAPISuccess('SavedSearch', 'create', [
370 [$dateCustomFieldName . '_relative', '=', 0],
371 [$dateCustomFieldName, '=', ['<=' => '20191031235959']],
374 $this->callAPISuccess('SavedSearch', 'create', [
376 [$dateCustomFieldName . '_relative', '=', 'this.month'],
379 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
380 $smartGroupConversionObject->convertCustomSmartGroups();
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'],
388 0 => [$dateCustomFieldName . '_relative', '=', 0],
389 2 => [$dateCustomFieldName . '_low', '=', '2019-10-01 00:00:00'],
392 0 => [$dateCustomFieldName . '_relative', '=', 0],
393 2 => [$dateCustomFieldName . '_high', '=', '2019-10-31 23:59:59'],
396 0 => [$dateCustomFieldName . '_relative', '=', 'this.month'],
399 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
400 foreach ($savedSearches['values'] as $id => $savedSearch) {
401 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
406 * Test conversion of on hold group.
408 public function testOnHoldConversion() {
409 $this->callAPISuccess('SavedSearch', 'create', [
411 ['on_hold', '=', '1'],
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]);
423 * Test renaming a field.
425 public function testRenameField() {
426 $this->callAPISuccess('SavedSearch', 'create', [
428 ['activity_date_low', '=', '01/22/2019'],
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]);
438 * Test renaming multiple fields.
442 public function testRenameFields() {
443 $this->callAPISuccess('SavedSearch', 'create', [
445 ['activity_date_low', '=', '01/22/2019'],
446 ['activity_date_relative', '=', 0],
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'],
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]);
460 * Test that a mis-saved variable in 'contribute settings' can be converted to a
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'));