Add upgrade function for message templates that does not involve copying the whole...
[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_Base_Test extends CiviUnitTestCase {
8
9 /**
10 * Test message upgrade process.
11 */
12 public function testMessageTemplateUpgrade() {
13 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
14
15 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
16 foreach ($templates as $template) {
17 $originalText = $template['msg_text'];
18 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
19 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
20 $this->assertEquals('great what a cool member you are', $msg_text);
21 }
22 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
23 $messageTemplateObject->updateTemplates();
24
25 foreach ($templates as $template) {
26 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
27 $this->assertContains('{ts}Membership Information{/ts}', $msg_text);
28 if ($msg_text !== $originalText) {
29 // Reset value for future tests.
30 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
31 }
32 }
33 }
34
35 /**
36 * Test message upgrade process only edits the default if the template is customised.
37 */
38 public function testMessageTemplateUpgradeAlreadyCustomised() {
39 $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
40
41 $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values'];
42 foreach ($templates as $template) {
43 if ($template['is_reserved']) {
44 $originalText = $template['msg_text'];
45 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]);
46 }
47 else {
48 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]);
49 }
50 }
51 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
52 $messageTemplateObject->updateTemplates();
53
54 foreach ($templates as $template) {
55 $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']);
56 if ($template['is_reserved']) {
57 $this->assertTrue(strstr($msg_text, '{ts}Membership Information{/ts}'));
58 }
59 else {
60 $this->assertEquals('great what a silly sausage you are', $msg_text);
61 }
62
63 if ($msg_text !== $originalText) {
64 // Reset value for future tests.
65 $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]);
66 }
67 }
68 }
69
70 /**
71 * Test function for messages on upgrade.
72 */
73 public function testMessageTemplateGetUpgradeMessages() {
74 $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
75 $messages = $messageTemplateObject->getUpgradeMessages();
76 $this->assertEquals(['Memberships - Receipt (on-line)' => 'Use email greeting at top where available'], $messages);
77 }
78
79 }