Merge pull request #23309 from civicrm/5.49
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / MessageTemplateTest.php
CommitLineData
d53da69a
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19namespace api\v4\Entity;
20
21use api\v4\UnitTestCase;
22use Civi\Test\DbTestTrait;
23use Civi\Test\GenericAssertionsTrait;
24use Civi\Test\TransactionalInterface;
25
26/**
27 * @group headless
28 */
29class MessageTemplateTest extends UnitTestCase implements TransactionalInterface {
30
31 use GenericAssertionsTrait;
32 use DbTestTrait;
33
34 private $baseTpl = [
35 'msg_title' => 'My Template',
36 'msg_subject' => 'My Subject',
37 'msg_text' => 'My body as text',
38 'msg_html' => '<p>My body as HTML</p>',
39 'is_reserved' => TRUE,
40 'is_default' => FALSE,
41 ];
42
43 /**
44 * Create/update a MessageTemplate with workflow_name and no corresponding workflow_id.
45 */
46 public function testWorkflowName_clean() {
47 $create = civicrm_api4('MessageTemplate', 'create', [
48 'values' => $this->baseTpl + ['workflow_name' => 'first', 'workflow_id' => NULL],
49 ])->single();
50 $this->assertDBQuery('first', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
51 $this->assertDBQuery(NULL, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
52
53 civicrm_api4('MessageTemplate', 'update', [
54 'where' => [['id', '=', $create['id']]],
55 'values' => ['workflow_name' => 'second', 'workflow_id' => NULL],
56 ])->single();
57 $this->assertDBQuery('second', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
58 $this->assertDBQuery(NULL, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
59 }
60
61 /**
62 * Create/update a MessageTemplate with workflow_name - a name which happens to have an older/corresponding workflow_id.
63 */
64 public function testWorkflowName_legacyMatch() {
65 [$firstId, $secondId] = $this->createFirstSecond();
66
67 $create = civicrm_api4('MessageTemplate', 'create', [
68 'values' => $this->baseTpl + ['workflow_name' => 'first', 'workflow_id' => NULL],
69 ])->single();
70 $this->assertDBQuery('first', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
71 $this->assertDBQuery($firstId, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
72
73 civicrm_api4('MessageTemplate', 'update', [
74 'where' => [['id', '=', $create['id']]],
75 'values' => ['workflow_name' => 'second', 'workflow_id' => NULL],
76 ])->single();
77 $this->assertDBQuery('second', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
78 $this->assertDBQuery($secondId, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
79 }
80
81 /**
82 * Create/update a MessageTempalte with workflow_id. Ensure the newer workflow_name is set.
83 */
84 public function testWorkflowId_legacyMatch() {
85 [$firstId, $secondId] = $this->createFirstSecond();
86
87 $create = civicrm_api4('MessageTemplate', 'create', [
88 'values' => $this->baseTpl + ['workflow_id' => $firstId, 'workflow_name' => NULL],
89 ])->single();
90 $this->assertDBQuery('first', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
91 $this->assertDBQuery($firstId, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
92
93 civicrm_api4('MessageTemplate', 'update', [
94 'where' => [['id', '=', $create['id']]],
95 'values' => ['workflow_id' => $secondId, 'workflow_name' => NULL],
96 ])->single();
97 $this->assertDBQuery('second', 'SELECT workflow_name FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
98 $this->assertDBQuery($secondId, 'SELECT workflow_id FROM civicrm_msg_template WHERE id = %1', [1 => [$create['id'], 'Int']]);
99 }
100
101 protected function createFirstSecond() {
102 $first = civicrm_api4('OptionValue', 'create', [
103 'values' => [
104 'option_group_id:name' => 'msg_tpl_workflow_meta',
105 'label' => 'First',
106 'name' => 'first',
107 ],
108 ]);
109 $second = civicrm_api4('OptionValue', 'create', [
110 'values' => [
111 'option_group_id:name' => 'msg_tpl_workflow_meta',
112 'label' => 'Second',
113 'name' => 'second',
114 ],
115 ]);
116 return [$first->single()['id'], $second->single()['id']];
117 }
118
119}