Merge pull request #15641 from eileenmcnaughton/cont_clean
[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
9 public function tearDown() {
10 $this->quickCleanup(['civicrm_saved_search']);
11 }
12
13 /**
14 * Test message upgrade process.
15 */
16 public function testMessageTemplateUpgrade() {
17 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
18
19 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
20 foreach ($templates as $template) {
21 $originalText = $template['msg_text'];
22 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
23 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
24 $this->assertEquals('great what a cool member you are', $msg_text);
25 }
26 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
27 $messageTemplateObject->updateTemplates();
28
29 foreach ($templates as $template) {
30 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
31 $this->assertContains('{assign var="greeting" value="{contact.email_greeting}"}{if $greeting}{$greeting},{/if}', $msg_text);
32 if ($msg_text !== $originalText) {
33 // Reset value for future tests.
34 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
35 }
36 }
37 }
38
39 /**
40 * Test message upgrade process only edits the default if the template is customised.
41 */
42 public function testMessageTemplateUpgradeAlreadyCustomised() {
43 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
44
45 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
46 foreach ($templates as $template) {
47 if ($template['is_reserved']) {
48 $originalText = $template['msg_text'];
49 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
50 }
51 else {
52 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
53 }
54 }
55 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
56 $messageTemplateObject->updateTemplates();
57
58 foreach ($templates as $template) {
59 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
60 if ($template['is_reserved']) {
61 $this->assertContains('{assign var="greeting" value="{contact.email_greeting}"}{if $greeting}{$greeting},{/if}', $msg_text);
62 }
63 else {
64 $this->assertEquals('great what a silly sausage you are', $msg_text);
65 }
66
67 if ($msg_text !== $originalText) {
68 // Reset value for future tests.
69 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
70 }
71 }
72 }
73
74 /**
75 * Test function for messages on upgrade.
76 */
77 public function testMessageTemplateGetUpgradeMessages() {
78 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
79 $messages = $messageTemplateObject->getUpgradeMessages();
80 $this->assertEquals([
81 'Memberships - Receipt (on-line)' => 'Use email greeting at top where available',
82 'Contributions - Receipt (on-line)' => 'Use email greeting at top where available',
83 'Events - Registration Confirmation and Receipt (on-line)' => 'Use email greeting at top where available',
84 ], $messages);
85 }
86
87 /**
88 * Test converting a datepicker field.
89 */
90 public function testSmartGroupDatePickerConversion() {
91 $this->callAPISuccess('SavedSearch', 'create', [
92 'form_values' => [
93 ['grant_application_received_date_high', '=', '01/20/2019'],
94 ['grant_due_date_low', '=', '01/22/2019'],
95 ],
96 ]);
97 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
98 $smartGroupConversionObject->updateGroups([
99 'datepickerConversion' => [
100 'grant_application_received_date',
101 'grant_decision_date',
102 'grant_money_transfer_date',
103 'grant_due_date',
104 ],
105 ]);
106 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
107 $this->assertEquals('grant_application_received_date_high', $savedSearch['form_values'][0][0]);
108 $this->assertEquals('2019-01-20 23:59:59', $savedSearch['form_values'][0][2]);
109 $this->assertEquals('grant_due_date_low', $savedSearch['form_values'][1][0]);
110 $this->assertEquals('2019-01-22 00:00:00', $savedSearch['form_values'][1][2]);
111 $hasRelative = FALSE;
112 foreach ($savedSearch['form_values'] as $form_value) {
113 if ($form_value[0] === 'grant_due_date_relative') {
114 $hasRelative = TRUE;
115 }
116 }
117 $this->assertEquals(TRUE, $hasRelative);
118 }
119
120 /**
121 * Test Multiple Relative Date conversions
122 */
123 public function testSmartGroupMultipleRelatvieDateConversions() {
124 $this->callAPISuccess('SavedSearch', 'create', [
125 'form_values' => [
126 ['membership_join_date_low', '=', '20190903000000'],
127 ['membership_join_date_high', '=', '20190903235959'],
128 ['membership_start_date_low', '=' , '20190901000000'],
129 ['membership_start_date_high', '=', '20190907235959'],
130 ['membership_end_date_low', '=', '20190901000000'],
131 ['membership_end_date_high', '=', '20190907235959'],
132 'relative_dates' => [
133 'member_join' => 'this.day',
134 'member_start' => 'this.week',
135 'member_end' => 'this.week',
136 ],
137 ],
138 ]);
139 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
140 $smartGroupConversionObject->updateGroups([
141 'datepickerConversion' => [
142 'membership_join_date',
143 'membership_start_date',
144 'membership_end_date',
145 ],
146 ]);
147 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
148 $this->assertContains('6', array_keys($savedSearch['form_values']));
149 $this->assertEquals('membership_join_date_relative', $savedSearch['form_values'][6][0]);
150 $this->assertEquals('this.day', $savedSearch['form_values'][6][2]);
151 $this->assertContains('7', array_keys($savedSearch['form_values']));
152 $this->assertEquals('membership_start_date_relative', $savedSearch['form_values'][7][0]);
153 $this->assertEquals('this.week', $savedSearch['form_values'][7][2]);
154 $this->assertContains('8', array_keys($savedSearch['form_values']));
155 $this->assertEquals('membership_end_date_relative', $savedSearch['form_values'][8][0]);
156 $this->assertEquals('this.week', $savedSearch['form_values'][8][2]);
157 }
158
159 /**
160 * Test upgrading multiple Event smart groups of different formats
161 */
162 public function testMultipleEventSmartGroupDateConversions() {
163 $this->callAPISuccess('SavedSearch', 'create', [
164 'form_values' => [
165 ['event_start_date_low', '=', '20191001000000'],
166 ['event_end_date_high', '=', '20191031235959'],
167 'relative_dates' => [
168 'event' => 'this.month',
169 ],
170 ],
171 ]);
172 $this->callAPISuccess('SavedSearch', 'create', [
173 'form_values' => [
174 ['event_start_date_low', '=', '20191001000000'],
175 ],
176 ]);
177 $this->callAPISuccess('SavedSearch', 'create', [
178 'form_values' => [
179 'event_start_date_low' => '20191001000000',
180 'event_end_date_high' => '20191031235959',
181 'event_relative' => 'this.month',
182 ],
183 ]);
184 $this->callAPISuccess('SavedSearch', 'create', [
185 'form_values' => [
186 'event_start_date_low' => '10/01/2019',
187 'event_end_date_high' => '',
188 'event_relative' => '0',
189 ],
190 ]);
191 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
192 $smartGroupConversionObject->renameFields([
193 ['old' => 'event_start_date_low', 'new' => 'event_low'],
194 ['old' => 'event_end_date_high', 'new' => 'event_high'],
195 ]);
196 $smartGroupConversionObject->updateGroups([
197 'datepickerConversion' => [
198 'event',
199 ],
200 ]);
201 $expectedResults = [
202 1 => [
203 'relative_dates' => [],
204 2 => ['event_relative', '=', 'this.month'],
205 ],
206 2 => [
207 0 => ['event_low', '=', '2019-10-01 00:00:00'],
208 1 => ['event_relative', '=', 0],
209 ],
210 3 => [
211 'event_relative' => 'this.month',
212 ],
213 4 => [
214 'event_relative' => 0,
215 'event_low' => '2019-10-01 00:00:00',
216 ],
217 ];
218 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
219 foreach ($savedSearches['values'] as $id => $savedSearch) {
220 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
221 }
222 }
223
224 /**
225 * Test converting relationship fields
226 */
227 public function testSmartGroupRelationshipDateConversions() {
228 $this->callAPISuccess('SavedSearch', 'create', [
229 'form_values' => [
230 ['relationship_start_date_low', '=', '20191001000000'],
231 ['relationship_start_date_high', '=', '20191031235959'],
232 ['relationship_end_date_low', '=', '20191001000000'],
233 ['relationship_end_date_high', '=', '20191031235959'],
234 'relative_dates' => [
235 'relation_start' => 'this.month',
236 'relation_end' => 'this.month',
237 ],
238 ],
239 ]);
240 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
241 $smartGroupConversionObject->updateGroups([
242 'datepickerConversion' => [
243 'relationship_start_date',
244 'relationship_end_date',
245 ],
246 ]);
247 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
248 $this->assertEquals([], $savedSearch['form_values']['relative_dates']);
249 $this->assertEquals(['relationship_start_date_relative', '=', 'this.month'], $savedSearch['form_values'][4]);
250 $this->assertEquals(['relationship_end_date_relative', '=', 'this.month'], $savedSearch['form_values'][5]);
251 }
252
253 /**
254 * Test conversion of on hold group.
255 */
256 public function testOnHoldConversion() {
257 $this->callAPISuccess('SavedSearch', 'create', [
258 'form_values' => [
259 ['on_hold', '=', '1'],
260 ],
261 ]);
262 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups('5.11.alpha1');
263 $smartGroupConversionObject->convertEqualsStringToInArray('on_hold');
264 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
265 $this->assertEquals('IN', $savedSearch['form_values'][0][1]);
266 $this->assertEquals(['1'], $savedSearch['form_values'][0][2]);
267
268 }
269
270 /**
271 * Test renaming a field.
272 */
273 public function testRenameField() {
274 $this->callAPISuccess('SavedSearch', 'create', [
275 'form_values' => [
276 ['activity_date_low', '=', '01/22/2019'],
277 ],
278 ]);
279 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
280 $smartGroupConversionObject->renameField('activity_date_low', 'activity_date_time_low');
281 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
282 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
283 }
284
285 /**
286 * Test renaming multiple fields.
287 *
288 * @throws Exception
289 */
290 public function testRenameFields() {
291 $this->callAPISuccess('SavedSearch', 'create', [
292 'form_values' => [
293 ['activity_date_low', '=', '01/22/2019'],
294 ['activity_date_relative', '=', 0],
295 ],
296 ]);
297 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
298 $smartGroupConversionObject->renameFields([
299 ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
300 ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
301 ]);
302 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
303 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
304 $this->assertEquals('activity_date_time_relative', $savedSearch['form_values'][1][0]);
305 }
306
307 /**
308 * Test that a mis-saved variable in 'contribute settings' can be converted to a
309 * 'proper' setting.
310 */
311 public function testConvertUpgradeContributeSettings() {
312 Civi::settings()->set('contribution_invoice_settings', ['foo' => 'bar', 'deferred_revenue_enabled' => 1]);
313 $this->assertEquals(0, Civi::settings()->get('deferred_revenue_enabled'));
314 CRM_Upgrade_Incremental_Base::updateContributeSettings(NULL, 5.1);
315 $this->assertEquals(1, Civi::settings()->get('deferred_revenue_enabled'));
316 }
317
318 }