Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / MessageTemplateTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for Template API - civicrm_msg_template*
14 *
15 * @package CiviCRM_APIv3
16 * @group headless
17 */
18 class api_v3_MessageTemplateTest extends CiviUnitTestCase {
19
20 protected $entity = 'MessageTemplate';
21 protected $params;
22
23 public function setUp() {
24 $this->_apiversion = 3;
25 parent::setUp();
26 $this->useTransaction(TRUE);
27 $template = CRM_Core_DAO::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray();
28 $this->params = [
29 'msg_title' => $template['msg_title'],
30 'msg_subject' => $template['msg_subject'],
31 'msg_text' => $template['msg_text'],
32 'msg_html' => $template['msg_html'],
33 'workflow_id' => $template['workflow_id'],
34 'is_default' => $template['is_default'],
35 'is_reserved' => $template['is_reserved'],
36 ];
37 }
38
39 public function tearDown() {
40 parent::tearDown();
41 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
42 }
43
44 /**
45 * Test create function succeeds.
46 */
47 public function testCreate() {
48 $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params, __FUNCTION__, __FILE__);
49 $this->getAndCheck($this->params, $result['id'], $this->entity);
50 }
51
52 /**
53 * Test get function succeeds.
54 *
55 * This is actually largely tested in the get action on create.
56 *
57 * Add extra checks for any 'special' return values or
58 * behaviours
59 */
60 public function testGet() {
61 $result = $this->callAPIAndDocument('MessageTemplate', 'get', $this->params, __FUNCTION__, __FILE__);
62 $this->assertEquals(1, $result['count']);
63 $this->assertNotNull($result['values'][$result['id']]['id']);
64 }
65
66 /**
67 * Check the delete function succeeds.
68 */
69 public function testDelete() {
70 $entity = $this->createTestEntity();
71 $result = $this->callAPIAndDocument('MessageTemplate', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__);
72 $checkDeleted = $this->callAPISuccess($this->entity, 'get', [
73 'id' => $entity['id'],
74 ]);
75 $this->assertEquals(0, $checkDeleted['count']);
76 }
77
78 /**
79 * If you give workflow_id, then workflow_name should also be set.
80 */
81 public function testWorkflowIdToName() {
82 $wfName = 'uf_notify';
83 $wfId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [
84 1 => [$wfName, 'String'],
85 ]);
86
87 $created = $this->callAPISuccess('MessageTemplate', 'create', [
88 'msg_title' => __FUNCTION__,
89 'msg_subject' => __FUNCTION__,
90 'msg_text' => __FUNCTION__,
91 'msg_html' => __FUNCTION__,
92 'workflow_id' => $wfId,
93 ]);
94 $this->assertEquals($wfName, $created['values'][$created['id']]['workflow_name']);
95 $this->assertEquals($wfId, $created['values'][$created['id']]['workflow_id']);
96 $get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]);
97 $this->assertEquals($wfName, $get['workflow_name']);
98 $this->assertEquals($wfId, $get['workflow_id']);
99 }
100
101 /**
102 * If you give workflow_name, then workflow_id should also be set.
103 */
104 public function testWorkflowNameToId() {
105 $wfName = 'petition_sign';
106 $wfId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_option_value WHERE name = %1', [
107 1 => [$wfName, 'String'],
108 ]);
109
110 $created = $this->callAPISuccess('MessageTemplate', 'create', [
111 'msg_title' => __FUNCTION__,
112 'msg_subject' => __FUNCTION__,
113 'msg_text' => __FUNCTION__,
114 'msg_html' => __FUNCTION__,
115 'workflow_name' => $wfName,
116 ]);
117 $this->assertEquals($wfName, $created['values'][$created['id']]['workflow_name']);
118 $this->assertEquals($wfId, $created['values'][$created['id']]['workflow_id']);
119 $get = $this->callAPISuccess('MessageTemplate', 'getsingle', ['id' => $created['id']]);
120 $this->assertEquals($wfName, $get['workflow_name']);
121 $this->assertEquals($wfId, $get['workflow_id']);
122 }
123
124 public function testPermissionChecks() {
125 $entity = $this->createTestEntity();
126 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates'];
127 // Ensure that it cannot create a system message or update a system message tempalte given current permissions.
128 $this->callAPIFailure('MessageTemplate', 'create', [
129 'id' => $entity['id'],
130 'msg_subject' => 'test msg permission subject',
131 'check_permissions' => TRUE,
132 ]);
133 $testUserEntity = $entity['values'][$entity['id']];
134 unset($testUserEntity['id']);
135 $testUserEntity['msg_subject'] = 'Test user message template';
136 unset($testUserEntity['workflow_id']);
137 unset($testUserEntity['workflow_name']);
138 $testuserEntity['check_permissions'] = TRUE;
139 // ensure that it can create user templates;
140 $userEntity = $this->callAPISuccess('MessageTemplate', 'create', $testUserEntity);
141 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates'];
142 // Now check that when its swapped around permissions that the correct reponses are detected.
143 $this->callAPIFailure('MessageTemplate', 'create', [
144 'id' => $userEntity['id'],
145 'msg_subject' => 'User template updated by system message permission',
146 'check_permissions' => TRUE,
147 ]);
148 $this->callAPISuccess('MessageTemplate', 'create', [
149 'id' => $entity['id'],
150 'msg_subject' => 'test msg permission subject',
151 'check_permissions' => TRUE,
152 ]);
153 $newEntityParams = $entity['values'][$entity['id']];
154 unset($newEntityParams['id']);
155 $newEntityParams['check_permissions'] = TRUE;
156 $this->callAPISuccess('MessageTemplate', 'create', $newEntityParams);
157 // verify with all 3 permissions someone can do everything.
158 CRM_Core_Config::singleton()->userPermissionClass->permissions = [
159 'edit system workflow message templates',
160 'edit user-driven message templates',
161 ];
162 $this->callAPISuccess('MessageTemplate', 'create', [
163 'id' => $userEntity['id'],
164 'msg_subject' => 'User template updated by system message permission',
165 'check_permissions' => TRUE,
166 ]);
167 $this->callAPISuccess('MessageTemplate', 'create', [
168 'id' => $entity['id'],
169 'msg_subject' => 'test msg permission subject',
170 'check_permissions' => TRUE,
171 ]);
172 // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates
173 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates'];
174 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]);
175 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject backwards compatabilty', 'check_permissions' => TRUE]);
176 }
177
178 }