Cleanup following smart group conversions and fix the old name of the relationship...
[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 converting relationship fields
161 */
162 public function testSmartGroupRelationshipDateConversions() {
163 $this->callAPISuccess('SavedSearch', 'create', [
164 'form_values' => [
165 ['relationship_start_date_low', '=', '20191001000000'],
166 ['relationship_start_date_high', '=', '20191031235959'],
167 ['relationship_end_date_low', '=', '20191001000000'],
168 ['relationship_end_date_high', '=', '20191031235959'],
169 'relative_dates' => [
170 'relation_start' => 'this.month',
171 'relation_end' => 'this.month',
172 ],
173 ],
174 ]);
175 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
176 $smartGroupConversionObject->updateGroups([
177 'datepickerConversion' => [
178 'relationship_start_date',
179 'relationship_end_date',
180 ],
181 ]);
182 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
183 $this->assertEquals([], $savedSearch['form_values']['relative_dates']);
184 $this->assertEquals(['relationship_start_date_relative', '=', 'this.month'], $savedSearch['form_values'][4]);
185 $this->assertEquals(['relationship_end_date_relative', '=', 'this.month'], $savedSearch['form_values'][5]);
186 }
187
188 /**
189 * Test conversion of on hold group.
190 */
191 public function testOnHoldConversion() {
192 $this->callAPISuccess('SavedSearch', 'create', [
193 'form_values' => [
194 ['on_hold', '=', '1'],
195 ],
196 ]);
197 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups('5.11.alpha1');
198 $smartGroupConversionObject->convertEqualsStringToInArray('on_hold');
199 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
200 $this->assertEquals('IN', $savedSearch['form_values'][0][1]);
201 $this->assertEquals(['1'], $savedSearch['form_values'][0][2]);
202
203 }
204
205 /**
206 * Test renaming a field.
207 */
208 public function testRenameField() {
209 $this->callAPISuccess('SavedSearch', 'create', [
210 'form_values' => [
211 ['activity_date_low', '=', '01/22/2019'],
212 ],
213 ]);
214 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
215 $smartGroupConversionObject->renameField('activity_date_low', 'activity_date_time_low');
216 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
217 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
218 }
219
220 /**
221 * Test renaming multiple fields.
222 *
223 * @throws Exception
224 */
225 public function testRenameFields() {
226 $this->callAPISuccess('SavedSearch', 'create', [
227 'form_values' => [
228 ['activity_date_low', '=', '01/22/2019'],
229 ['activity_date_relative', '=', 0],
230 ],
231 ]);
232 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
233 $smartGroupConversionObject->renameFields([
234 ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'],
235 ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'],
236 ]);
237 $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
238 $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]);
239 $this->assertEquals('activity_date_time_relative', $savedSearch['form_values'][1][0]);
240 }
241
242 /**
243 * Test that a mis-saved variable in 'contribute settings' can be converted to a
244 * 'proper' setting.
245 */
246 public function testConvertUpgradeContributeSettings() {
247 Civi::settings()->set('contribution_invoice_settings', ['foo' => 'bar', 'deferred_revenue_enabled' => 1]);
248 $this->assertEquals(0, Civi::settings()->get('deferred_revenue_enabled'));
249 CRM_Upgrade_Incremental_Base::updateContributeSettings(NULL, 5.1);
250 $this->assertEquals(1, Civi::settings()->get('deferred_revenue_enabled'));
251 }
252
253 }