Merge pull request #15666 from revati90/shared_address
[civicrm-core.git] / tests / phpunit / api / v3 / MessageTemplateTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for Template API - civicrm_msg_template*
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_MessageTemplateTest extends CiviUnitTestCase {
35
36 protected $entity = 'MessageTemplate';
37 protected $params;
38
39 public function setUp() {
40 $this->_apiversion = 3;
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 $template = CRM_Core_DAO::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray();
44 $this->params = [
45 'msg_title' => $template['msg_title'],
46 'msg_subject' => $template['msg_subject'],
47 'msg_text' => $template['msg_text'],
48 'msg_html' => $template['msg_html'],
49 'workflow_id' => $template['workflow_id'],
50 'is_default' => $template['is_default'],
51 'is_reserved' => $template['is_reserved'],
52 ];
53 }
54
55 public function tearDown() {
56 parent::tearDown();
57 unset(CRM_Core_Config::singleton()->userPermissionClass->permissions);
58 }
59
60 /**
61 * Test create function succeeds.
62 */
63 public function testCreate() {
64 $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params, __FUNCTION__, __FILE__);
65 $this->getAndCheck($this->params, $result['id'], $this->entity);
66 }
67
68 /**
69 * Test get function succeeds.
70 *
71 * This is actually largely tested in the get action on create.
72 *
73 * Add extra checks for any 'special' return values or
74 * behaviours
75 */
76 public function testGet() {
77 $result = $this->callAPIAndDocument('MessageTemplate', 'get', $this->params, __FUNCTION__, __FILE__);
78 $this->assertEquals(1, $result['count']);
79 $this->assertNotNull($result['values'][$result['id']]['id']);
80 }
81
82 /**
83 * Check the delete function succeeds.
84 */
85 public function testDelete() {
86 $entity = $this->createTestEntity();
87 $result = $this->callAPIAndDocument('MessageTemplate', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__);
88 $checkDeleted = $this->callAPISuccess($this->entity, 'get', [
89 'id' => $entity['id'],
90 ]);
91 $this->assertEquals(0, $checkDeleted['count']);
92 }
93
94 public function testPermissionChecks() {
95 $entity = $this->createTestEntity();
96 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit user-driven message templates'];
97 // Ensure that it cannot create a system message or update a system message tempalte given current permissions.
98 $this->callAPIFailure('MessageTemplate', 'create', [
99 'id' => $entity['id'],
100 'msg_subject' => 'test msg permission subject',
101 'check_permissions' => TRUE,
102 ]);
103 $testUserEntity = $entity['values'][$entity['id']];
104 unset($testUserEntity['id']);
105 $testUserEntity['msg_subject'] = 'Test user message template';
106 unset($testUserEntity['workflow_id']);
107 $testuserEntity['check_permissions'] = TRUE;
108 // ensure that it can create user templates;
109 $userEntity = $this->callAPISuccess('MessageTemplate', 'create', $testUserEntity);
110 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit system workflow message templates'];
111 // Now check that when its swapped around permissions that the correct reponses are detected.
112 $this->callAPIFailure('MessageTemplate', 'create', [
113 'id' => $userEntity['id'],
114 'msg_subject' => 'User template updated by system message permission',
115 'check_permissions' => TRUE,
116 ]);
117 $this->callAPISuccess('MessageTemplate', 'create', [
118 'id' => $entity['id'],
119 'msg_subject' => 'test msg permission subject',
120 'check_permissions' => TRUE,
121 ]);
122 // verify with all 3 permissions someone can do everything.
123 CRM_Core_Config::singleton()->userPermissionClass->permissions = [
124 'edit system workflow message templates',
125 'edit user-driven message templates',
126 ];
127 $this->callAPISuccess('MessageTemplate', 'create', [
128 'id' => $userEntity['id'],
129 'msg_subject' => 'User template updated by system message permission',
130 'check_permissions' => TRUE,
131 ]);
132 $this->callAPISuccess('MessageTemplate', 'create', [
133 'id' => $entity['id'],
134 'msg_subject' => 'test msg permission subject',
135 'check_permissions' => TRUE,
136 ]);
137 // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates
138 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['edit message templates'];
139 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]);
140 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject backwards compatabilty', 'check_permissions' => TRUE]);
141 }
142
143 }