4 * Class CRM_UF_Page_ProfileEditorTest
7 class CRM_Upgrade_Incremental_BaseTest
extends CiviUnitTestCase
{
9 public function tearDown() {
10 $this->quickCleanup(['civicrm_saved_search']);
14 * Test message upgrade process.
16 public function testMessageTemplateUpgrade() {
17 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
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);
26 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
27 $messageTemplateObject->updateTemplates();
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']]);
40 * Test message upgrade process only edits the default if the template is customised.
42 public function testMessageTemplateUpgradeAlreadyCustomised() {
43 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
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']]);
52 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
55 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
56 $messageTemplateObject->updateTemplates();
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);
64 $this->assertEquals('great what a silly sausage you are', $msg_text);
67 if ($msg_text !== $originalText) {
68 // Reset value for future tests.
69 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
75 * Test function for messages on upgrade.
77 public function testMessageTemplateGetUpgradeMessages() {
78 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
79 $messages = $messageTemplateObject->getUpgradeMessages();
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',
88 * Test converting a datepicker field.
90 public function testSmartGroupDatePickerConversion() {
91 $this->callAPISuccess('SavedSearch', 'create', [
93 ['grant_application_received_date_high', '=', '01/20/2019'],
94 ['grant_due_date_low', '=', '01/22/2019'],
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',
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') {
117 $this->assertEquals(TRUE, $hasRelative);
121 * Test Multiple Relative Date conversions
123 public function testSmartGroupMultipleRelatvieDateConversions() {
124 $this->callAPISuccess('SavedSearch', 'create', [
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',
139 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
140 $smartGroupConversionObject->updateGroups([
141 'datepickerConversion' => [
142 'membership_join_date',
143 'membership_start_date',
144 'membership_end_date',
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]);
160 * Test upgrading multiple Event smart groups of different formats
162 public function testMultipleEventSmartGroupDateConversions() {
163 $this->callAPISuccess('SavedSearch', 'create', [
165 ['event_start_date_low', '=', '20191001000000'],
166 ['event_end_date_high', '=', '20191031235959'],
167 'relative_dates' => [
168 'event' => 'this.month',
172 $this->callAPISuccess('SavedSearch', 'create', [
174 ['event_start_date_low', '=', '20191001000000'],
177 $this->callAPISuccess('SavedSearch', 'create', [
179 'event_start_date_low' => '20191001000000',
180 'event_end_date_high' => '20191031235959',
181 'event_relative' => 'this.month',
184 $this->callAPISuccess('SavedSearch', 'create', [
186 'event_start_date_low' => '10/01/2019',
187 'event_end_date_high' => '',
188 'event_relative' => '0',
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'],
196 $smartGroupConversionObject->updateGroups([
197 'datepickerConversion' => [
203 'relative_dates' => [],
204 2 => ['event_relative', '=', 'this.month'],
207 0 => ['event_low', '=', '2019-10-01 00:00:00'],
208 1 => ['event_relative', '=', 0],
211 'event_relative' => 'this.month',
214 'event_relative' => 0,
215 'event_low' => '2019-10-01 00:00:00',
218 $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []);
219 foreach ($savedSearches['values'] as $id => $savedSearch) {
220 $this->assertEquals($expectedResults[$id], $savedSearch['form_values']);
225 * Test converting relationship fields
227 public function testSmartGroupRelationshipDateConversions() {
228 $this->callAPISuccess('SavedSearch', 'create', [
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',
240 $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
241 $smartGroupConversionObject->updateGroups([
242 'datepickerConversion' => [
243 'relationship_start_date',
244 'relationship_end_date',
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]);
254 * Test conversion of on hold group.
256 public function testOnHoldConversion() {
257 $this->callAPISuccess('SavedSearch', 'create', [
259 ['on_hold', '=', '1'],
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]);
271 * Test renaming a field.
273 public function testRenameField() {
274 $this->callAPISuccess('SavedSearch', 'create', [
276 ['activity_date_low', '=', '01/22/2019'],
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]);
286 * Test renaming multiple fields.
290 public function testRenameFields() {
291 $this->callAPISuccess('SavedSearch', 'create', [
293 ['activity_date_low', '=', '01/22/2019'],
294 ['activity_date_relative', '=', 0],
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'],
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]);
308 * Test that a mis-saved variable in 'contribute settings' can be converted to a
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'));