Merge pull request #16469 from civicrm/5.22
[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 public function testPermissionChecks() {
79 $entity = $this->createTestEntity();
80 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates'];
81 // Ensure that it cannot create a system message or update a system message tempalte given current permissions.
82 $this->callAPIFailure('MessageTemplate', 'create', [
83 'id' => $entity['id'],
84 'msg_subject' => 'test msg permission subject',
85 'check_permissions' => TRUE,
86 ]);
87 $testUserEntity = $entity['values'][$entity['id']];
88 unset($testUserEntity['id']);
89 $testUserEntity['msg_subject'] = 'Test user message template';
90 unset($testUserEntity['workflow_id']);
91 $testuserEntity['check_permissions'] = TRUE;
92 // ensure that it can create user templates;
93 $userEntity = $this->callAPISuccess('MessageTemplate', 'create', $testUserEntity);
94 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates'];
95 // Now check that when its swapped around permissions that the correct reponses are detected.
96 $this->callAPIFailure('MessageTemplate', 'create', [
97 'id' => $userEntity['id'],
98 'msg_subject' => 'User template updated by system message permission',
99 'check_permissions' => TRUE,
100 ]);
101 $this->callAPISuccess('MessageTemplate', 'create', [
102 'id' => $entity['id'],
103 'msg_subject' => 'test msg permission subject',
104 'check_permissions' => TRUE,
105 ]);
106 // verify with all 3 permissions someone can do everything.
107 CRM_Core_Config::singleton()->userPermissionClass->permissions = [
108 'edit system workflow message templates',
109 'edit user-driven message templates',
110 ];
111 $this->callAPISuccess('MessageTemplate', 'create', [
112 'id' => $userEntity['id'],
113 'msg_subject' => 'User template updated by system message permission',
114 'check_permissions' => TRUE,
115 ]);
116 $this->callAPISuccess('MessageTemplate', 'create', [
117 'id' => $entity['id'],
118 'msg_subject' => 'test msg permission subject',
119 'check_permissions' => TRUE,
120 ]);
121 // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates
122 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates'];
123 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]);
124 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject backwards compatabilty', 'check_permissions' => TRUE]);
125 }
126
127 }