3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for Template API - civicrm_msg_template*
15 * @package CiviCRM_APIv3
18 class api_v3_MessageTemplateTest
extends CiviUnitTestCase
{
20 protected $entity = 'MessageTemplate';
23 public function setUp() {
24 $this->_apiversion
= 3;
26 $this->useTransaction(TRUE);
27 $template = CRM_Core_DAO
::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray();
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'],
39 public function tearDown() {
41 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
45 * Test create function succeeds.
47 public function testCreate() {
48 $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params
, __FUNCTION__
, __FILE__
);
49 $this->getAndCheck($this->params
, $result['id'], $this->entity
);
53 * Test get function succeeds.
55 * This is actually largely tested in the get action on create.
57 * Add extra checks for any 'special' return values or
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']);
67 * Check the delete function succeeds.
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'],
75 $this->assertEquals(0, $checkDeleted['count']);
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,
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,
101 $this->callAPISuccess('MessageTemplate', 'create', [
102 'id' => $entity['id'],
103 'msg_subject' => 'test msg permission subject',
104 'check_permissions' => TRUE,
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',
111 $this->callAPISuccess('MessageTemplate', 'create', [
112 'id' => $userEntity['id'],
113 'msg_subject' => 'User template updated by system message permission',
114 'check_permissions' => TRUE,
116 $this->callAPISuccess('MessageTemplate', 'create', [
117 'id' => $entity['id'],
118 'msg_subject' => 'test msg permission subject',
119 'check_permissions' => TRUE,
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]);