-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathMessageTemplateTest.php
194 lines (177 loc) · 7.4 KB
/
MessageTemplateTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Test class for Template API - civicrm_msg_template*
*
* @package CiviCRM_APIv3
* @group headless
* @group msgtpl
*/
class api_v3_MessageTemplateTest extends CiviUnitTestCase {
protected $entity = 'MessageTemplate';
protected $params;
public function setUp(): void {
$this->_apiversion = 3;
parent::setUp();
$this->useTransaction();
$this->params = [
'msg_title' => 'title',
'msg_subject' => 'subject',
'msg_text' => 'text',
'msg_html' => 'html',
'workflow_name' => 'friend',
];
}
/**
* Test create function succeeds.
*/
public function testCreate(): void {
$result = $this->callAPISuccess('MessageTemplate', 'create', $this->params);
$this->getAndCheck($this->params, $result['id'], $this->entity);
}
/**
* Test get function succeeds.
*
* This is actually largely tested in the get action on create.
*
* Add extra checks for any 'special' return values or
* behaviours
*/
public function testGet(): void {
$result = $this->callAPISuccess('MessageTemplate', 'get', [
'workflow_name' => 'contribution_invoice_receipt',
'is_default' => 1,
]);
$this->assertEquals(1, $result['count']);
$this->assertNotNull($result['values'][$result['id']]['id']);
}
/**
* Check the delete function succeeds.
*/
public function testDelete(): void {
$entity = $this->createTestEntity('MessageTemplate', $this->params);
$this->callAPISuccess('MessageTemplate', 'delete', ['id' => $entity['id']]);
$checkDeleted = $this->callAPISuccess($this->entity, 'get', [
'id' => $entity['id'],
]);
$this->assertEquals(0, $checkDeleted['count']);
}
/**
* If you give workflow_id, then workflow_name should also be set.
*
* @throws \CRM_Core_Exception
*/
public function testWorkflowIDToName(): void {
$workflowName = 'uf_notify';
$workflowID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [
1 => [$workflowName, 'String'],
]);
$created = $this->callAPISuccess('MessageTemplate', 'create', [
'msg_title' => __FUNCTION__,
'msg_subject' => __FUNCTION__,
'msg_text' => __FUNCTION__,
'msg_html' => __FUNCTION__,
'workflow_id' => $workflowID,
]);
$this->assertEquals($workflowName, $created['values'][$created['id']]['workflow_name']);
$this->assertEquals($workflowID, $created['values'][$created['id']]['workflow_id']);
$get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]);
$this->assertEquals($workflowName, $get['workflow_name']);
$this->assertEquals($workflowID, $get['workflow_id']);
}
/**
* If you give workflow_name, then workflow_id should also be set.
*
* @throws \CRM_Core_Exception
*/
public function testWorkflowNameToID(): void {
$workflowName = 'petition_sign';
$workflowID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [
1 => ['petition_sign', 'String'],
]);
$created = $this->callAPISuccess('MessageTemplate', 'create', [
'msg_title' => __FUNCTION__,
'msg_subject' => __FUNCTION__,
'msg_text' => __FUNCTION__,
'msg_html' => __FUNCTION__,
'workflow_name' => $workflowName,
]);
$this->assertEquals($workflowName, $created['values'][$created['id']]['workflow_name']);
$this->assertEquals($workflowID, $created['values'][$created['id']]['workflow_id']);
$get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]);
$this->assertEquals($workflowName, $get['workflow_name']);
$this->assertEquals($workflowID, $get['workflow_id']);
}
/**
* Test workflow permissions.
*
* edit message templates allows editing all templates, otherwise:
* - edit user-driven message templates is required when workflow_name is not set.
* - edit system workflow message templates is required when workflow_name is set.
*/
public function testPermissionChecks(): void {
$this->createTestEntity('MessageTemplate', [
'msg_title' => 'title',
'msg_subject' => 'subject',
'msg_html' => 'html',
'workflow_name' => 'friend',
], 'workflow');
$this->createTestEntity('MessageTemplate', [
'msg_title' => 'title',
'msg_subject' => 'subject',
'msg_html' => 'html',
], 'user');
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates'];
// Attempting to update the workflow template should fail with only user permissions.
$this->callAPIFailure('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['workflow'],
'msg_subject' => 'test msg permission subject',
'check_permissions' => TRUE,
]);
// The user message should be possible to update.
$this->callAPISuccess('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['user'],
'msg_subject' => 'Test user message template',
'check_permissions' => TRUE,
]);
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates'];
// Now check that when its swapped around permissions that the correct responses are detected.
$this->callAPIFailure('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['user'],
'msg_subject' => 'User template updated by system message permission',
'check_permissions' => TRUE,
]);
$this->callAPISuccess('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['workflow'],
'msg_subject' => 'test msg permission subject',
'check_permissions' => TRUE,
]);
// With both permissions the user can update both template types.
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'edit system workflow message templates',
'edit user-driven message templates',
];
$this->callAPISuccess('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['workflow'],
'msg_subject' => 'Workflow template updated',
'check_permissions' => TRUE,
]);
$this->callAPISuccess('MessageTemplate', 'create', [
'id' => $this->ids['MessageTemplate']['user'],
'msg_subject' => 'User template updated',
'check_permissions' => TRUE,
]);
// Verify that the backwards compatibility still works i.e. having edit message templates allows for editing of both kinds of message templates
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates'];
$this->callAPISuccess('MessageTemplate', 'create', ['id' => $this->ids['MessageTemplate']['workflow'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]);
$this->callAPISuccess('MessageTemplate', 'create', ['id' => $this->ids['MessageTemplate']['user'], 'msg_subject' => 'test msg permission subject backwards compatibility', 'check_permissions' => TRUE]);
}
}