Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / MessageTemplateTest.php
CommitLineData
397e0e51
EC
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
397e0e51 5 | |
7d61e75f
TO
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 |
397e0e51 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
397e0e51 11
397e0e51
EC
12/**
13 * Test class for Template API - civicrm_msg_template*
14 *
6c6e6187 15 * @package CiviCRM_APIv3
acb109b7 16 * @group headless
397e0e51 17 */
63c9befe 18class api_v3_MessageTemplateTest extends CiviUnitTestCase {
39b959db 19
63c9befe 20 protected $entity = 'MessageTemplate';
397e0e51
EC
21 protected $params;
22
00be9182 23 public function setUp() {
397e0e51
EC
24 $this->_apiversion = 3;
25 parent::setUp();
ba399c15 26 $this->useTransaction(TRUE);
c6327d7d 27 $template = CRM_Core_DAO::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray();
9099cab3 28 $this->params = [
397e0e51
EC
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'],
9099cab3 36 ];
397e0e51 37 }
397e0e51 38
781ed314
SL
39 public function tearDown() {
40 parent::tearDown();
41 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
42 }
43
6c6e6187 44 /**
eceb18cc 45 * Test create function succeeds.
397e0e51
EC
46 */
47 public function testCreate() {
63c9befe 48 $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params, __FUNCTION__, __FILE__);
397e0e51
EC
49 $this->getAndCheck($this->params, $result['id'], $this->entity);
50 }
51
52 /**
fe482240
EM
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
397e0e51 58 * behaviours
397e0e51
EC
59 */
60 public function testGet() {
63c9befe 61 $result = $this->callAPIAndDocument('MessageTemplate', 'get', $this->params, __FUNCTION__, __FILE__);
ba4a1892
TM
62 $this->assertEquals(1, $result['count']);
63 $this->assertNotNull($result['values'][$result['id']]['id']);
397e0e51
EC
64 }
65
6c6e6187 66 /**
eceb18cc 67 * Check the delete function succeeds.
6c6e6187 68 */
397e0e51
EC
69 public function testDelete() {
70 $entity = $this->createTestEntity();
9099cab3
CW
71 $result = $this->callAPIAndDocument('MessageTemplate', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__);
72 $checkDeleted = $this->callAPISuccess($this->entity, 'get', [
21dfd5f5 73 'id' => $entity['id'],
9099cab3 74 ]);
ba4a1892 75 $this->assertEquals(0, $checkDeleted['count']);
397e0e51
EC
76 }
77
781ed314
SL
78 public function testPermissionChecks() {
79 $entity = $this->createTestEntity();
9099cab3 80 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates'];
781ed314 81 // Ensure that it cannot create a system message or update a system message tempalte given current permissions.
9099cab3
CW
82 $this->callAPIFailure('MessageTemplate', 'create', [
83 'id' => $entity['id'],
84 'msg_subject' => 'test msg permission subject',
85 'check_permissions' => TRUE,
86 ]);
781ed314
SL
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);
9099cab3 94 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates'];
781ed314 95 // Now check that when its swapped around permissions that the correct reponses are detected.
9099cab3
CW
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 ]);
781ed314 106 // verify with all 3 permissions someone can do everything.
9099cab3
CW
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 ]);
781ed314 121 // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates
9099cab3 122 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates'];
781ed314
SL
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
397e0e51 127}