Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / api / v3 / UFGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for UFGroup API - civicrm_uf_*
14 * @todo Split UFGroup and UFJoin tests
15 *
16 * @package CiviCRM
17 * @group headless
18 */
19 class api_v3_UFGroupTest extends CiviUnitTestCase {
20 /**
21 * ids from the uf_group_test.xml fixture
22 * @var int
23 */
24 protected $_ufGroupId;
25 protected $_ufFieldId;
26 protected $_contactId;
27 protected $_groupId;
28 protected $params;
29
30 protected function setUp(): void {
31 parent::setUp();
32 $this->_groupId = $this->groupCreate();
33 $this->_contactId = $this->individualCreate();
34 $this->createLoggedInUser();
35 $ufGroup = $this->callAPISuccess('uf_group', 'create', [
36 'group_type' => 'Contact',
37 'help_pre' => 'Profile to Test API',
38 'title' => 'Test Profile',
39 ]);
40 $this->_ufGroupId = $ufGroup['id'];
41 $ufMatch = $this->callAPISuccess('uf_match', 'create', [
42 'contact_id' => $this->_contactId,
43 'uf_id' => 42,
44 'uf_name' => 'email@mail.com',
45 ]);
46 $this->_ufMatchId = $ufMatch['id'];
47 $this->params = [
48 'add_captcha' => 1,
49 'add_contact_to_group' => $this->_groupId,
50 'group' => $this->_groupId,
51 'cancel_URL' => 'http://example.org/cancel',
52 'created_date' => '2009-06-27 00:00:00',
53 'created_id' => $this->_contactId,
54 'group_type' => 'Individual,Contact',
55 'help_post' => 'help post',
56 'help_pre' => 'help pre',
57 'is_active' => 0,
58 'is_cms_user' => 1,
59 'is_edit_link' => 1,
60 'is_map' => 1,
61 'is_reserved' => 1,
62 'is_uf_link' => 1,
63 'is_update_dupe' => 1,
64 'name' => 'Test_Group',
65 'notify' => 'admin@example.org',
66 'post_URL' => 'http://example.org/post',
67 'title' => 'Test Group',
68 ];
69 }
70
71 public function tearDown(): void {
72 // Truncate the tables
73 $this->quickCleanup(
74 [
75 'civicrm_group',
76 'civicrm_contact',
77 'civicrm_uf_group',
78 'civicrm_uf_join',
79 'civicrm_uf_match',
80 ]
81 );
82 }
83
84 /**
85 * @param int $version
86 * @dataProvider versionThreeAndFour
87 */
88 public function testUpdateUFGroup($version) {
89 $this->_apiversion = $version;
90 $params = [
91 'title' => 'Edited Test Profile',
92 'help_post' => 'Profile Pro help text.',
93 'is_active' => 1,
94 'id' => $this->_ufGroupId,
95 ];
96
97 $result = $this->callAPISuccess('uf_group', 'create', $params);
98 foreach ($params as $key => $value) {
99 $this->assertEquals($result['values'][$result['id']][$key], $value);
100 }
101 }
102
103 /**
104 * @param int $version
105 * @dataProvider versionThreeAndFour
106 */
107 public function testUFGroupCreate($version) {
108 $this->_apiversion = $version;
109 $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__);
110 $this->assertAPISuccess($result);
111 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
112 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
113 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
114 foreach ($this->params as $key => $value) {
115 if ($key == 'add_contact_to_group' or $key == 'group') {
116 continue;
117 }
118 $received = $result['values'][$result['id']][$key];
119 if ($key == 'group_type' && $version == 4) {
120 $received = implode(',', $received);
121 }
122 $expected = $this->params[$key];
123 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
124 }
125 }
126
127 /**
128 * @param int $version
129 * @dataProvider versionThreeAndFour
130 */
131 public function testUFGroupCreateWithWrongParams($version) {
132 $this->_apiversion = $version;
133 $result = $this->callAPIFailure('uf_group', 'create', ['name' => 'A title-less group']);
134 }
135
136 /**
137 * @param int $version
138 * @dataProvider versionThreeAndFour
139 */
140 public function testUFGroupUpdate($version) {
141 $this->_apiversion = $version;
142 $params = [
143 'id' => $this->_ufGroupId,
144 'add_captcha' => 1,
145 'add_contact_to_group' => $this->_groupId,
146 'cancel_URL' => 'http://example.org/cancel',
147 'created_date' => '2009-06-27',
148 'created_id' => $this->_contactId,
149 'group' => $this->_groupId,
150 'group_type' => 'Individual,Contact',
151 'help_post' => 'help post',
152 'help_pre' => 'help pre',
153 'is_active' => 0,
154 'is_cms_user' => 1,
155 'is_edit_link' => 1,
156 'is_map' => 1,
157 'is_reserved' => 1,
158 'is_uf_link' => 1,
159 'is_update_dupe' => 1,
160 'name' => 'test_group',
161 'notify' => 'admin@example.org',
162 'post_URL' => 'http://example.org/post',
163 'title' => 'Test Group',
164 ];
165 $result = $this->callAPISuccess('uf_group', 'create', $params);
166 $params['created_date'] = date('YmdHis', strtotime($params['created_date']));
167 foreach ($params as $key => $value) {
168 if ($key == 'add_contact_to_group' or $key == 'group') {
169 continue;
170 }
171 $received = $result['values'][$result['id']][$key];
172 if ($key == 'group_type' && $version == 4) {
173 $received = implode(',', $received);
174 }
175 $this->assertEquals($received, $params[$key], $key . " doesn't match " . $value);
176 }
177
178 $this->assertEquals($result['values'][$this->_ufGroupId]['add_to_group_id'], $params['add_contact_to_group']);
179 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $params['group']);
180 }
181
182 /**
183 * @param int $version
184 * @dataProvider versionThreeAndFour
185 */
186 public function testUFGroupGet($version) {
187 $this->_apiversion = $version;
188 $result = $this->callAPISuccess('uf_group', 'create', $this->params);
189 $params = ['id' => $result['id']];
190 $result = $this->callAPIAndDocument('uf_group', 'get', $params, __FUNCTION__, __FILE__);
191 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
192 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
193 foreach ($this->params as $key => $value) {
194 // skip created date because it doesn't seem to be working properly & fixing date handling is for another day
195 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'created_date') {
196 continue;
197 }
198 $expected = $this->params[$key];
199 $received = $result['values'][$result['id']][$key];
200 // Api4 auto-splits serialized fields, v3 sometimes does but not in this case
201 if ($version == 4 && is_array($received)) {
202 $received = implode(',', $received);
203 }
204 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
205 }
206 }
207
208 /**
209 * @param int $version
210 * @dataProvider versionThreeAndFour
211 */
212 public function testUFGroupUpdateWithEmptyParams($version) {
213 $this->_apiversion = $version;
214 $result = $this->callAPIFailure('uf_group', 'create', [], 'title');
215 }
216
217 /**
218 * @param int $version
219 * @dataProvider versionThreeAndFour
220 */
221 public function testUFGroupDelete($version) {
222 $this->_apiversion = $version;
223 $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params);
224 $params = ['id' => $ufGroup['id']];
225 $this->assertEquals(1, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
226 $result = $this->callAPIAndDocument('uf_group', 'delete', $params, __FUNCTION__, __FILE__);
227 $this->assertEquals(0, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
228 }
229
230 }