3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for Template API - civicrm_msg_template*
31 * @package CiviCRM_APIv3
34 class api_v3_MessageTemplateTest
extends CiviUnitTestCase
{
36 * Assume empty database with just civicrm_data.
38 protected $entity = 'MessageTemplate';
42 public function setUp() {
43 $this->_apiversion
= 3;
45 $this->useTransaction(TRUE);
46 $template = CRM_Core_DAO
::createTestObject('CRM_Core_DAO_MessageTemplate')->toArray();
47 $this->params
= array(
48 'msg_title' => $template['msg_title'],
49 'msg_subject' => $template['msg_subject'],
50 'msg_text' => $template['msg_text'],
51 'msg_html' => $template['msg_html'],
52 'workflow_id' => $template['workflow_id'],
53 'is_default' => $template['is_default'],
54 'is_reserved' => $template['is_reserved'],
58 public function tearDown() {
60 unset(CRM_Core_Config
::singleton()->userPermissionClass
->permissions
);
64 * Test create function succeeds.
66 public function testCreate() {
67 $result = $this->callAPIAndDocument('MessageTemplate', 'create', $this->params
, __FUNCTION__
, __FILE__
);
68 $this->getAndCheck($this->params
, $result['id'], $this->entity
);
72 * Test get function succeeds.
74 * This is actually largely tested in the get action on create.
76 * Add extra checks for any 'special' return values or
79 public function testGet() {
80 $result = $this->callAPIAndDocument('MessageTemplate', 'get', $this->params
, __FUNCTION__
, __FILE__
);
81 $this->assertEquals(1, $result['count']);
82 $this->assertNotNull($result['values'][$result['id']]['id']);
86 * Check the delete function succeeds.
88 public function testDelete() {
89 $entity = $this->createTestEntity();
90 $result = $this->callAPIAndDocument('MessageTemplate', 'delete', array('id' => $entity['id']), __FUNCTION__
, __FILE__
);
91 $checkDeleted = $this->callAPISuccess($this->entity
, 'get', array(
92 'id' => $entity['id'],
94 $this->assertEquals(0, $checkDeleted['count']);
97 public function testPermissionChecks() {
98 $entity = $this->createTestEntity();
99 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('edit user-driven message templates');
100 // Ensure that it cannot create a system message or update a system message tempalte given current permissions.
101 $this->callAPIFailure('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject', 'check_permissions' => TRUE]);
102 $testUserEntity = $entity['values'][$entity['id']];
103 unset($testUserEntity['id']);
104 $testUserEntity['msg_subject'] = 'Test user message template';
105 unset($testUserEntity['workflow_id']);
106 $testuserEntity['check_permissions'] = TRUE;
107 // ensure that it can create user templates;
108 $userEntity = $this->callAPISuccess('MessageTemplate', 'create', $testUserEntity);
109 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('edit system workflow message templates');
110 // Now check that when its swapped around permissions that the correct reponses are detected.
111 $this->callAPIFailure('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by system message permission', 'check_permissions' => TRUE]);
112 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject', 'check_permissions' => TRUE]);
113 // verify with all 3 permissions someone can do everything.
114 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('edit system workflow message templates', 'edit user-driven message templates');
115 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by system message permission', 'check_permissions' => TRUE]);
116 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject', 'check_permissions' => TRUE]);
117 // Verify that the backwards compatabiltiy still works i.e. having edit message templates allows for editing of both kinds of message templates
118 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('edit message templates');
119 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $userEntity['id'], 'msg_subject' => 'User template updated by edit message permission', 'check_permissions' => TRUE]);
120 $this->callAPISuccess('MessageTemplate', 'create', ['id' => $entity['id'], 'msg_subject' => 'test msg permission subject backwards compatabilty', 'check_permissions' => TRUE]);