Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / UFGroupTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035
TO
12/**
13 * Test class for UFGroup API - civicrm_uf_*
14 * @todo Split UFGroup and UFJoin tests
15 *
6c6e6187 16 * @package CiviCRM
acb109b7 17 * @group headless
6a488035
TO
18 */
19class api_v3_UFGroupTest extends CiviUnitTestCase {
39b959db
SL
20 /**
21 * ids from the uf_group_test.xml fixture
22 * @var int
23 */
7fbb4198 24 protected $_ufGroupId;
6a488035 25 protected $_ufFieldId;
7fbb4198 26 protected $_contactId;
27 protected $_groupId;
6a488035
TO
28 protected $params;
29
30 protected function setUp() {
31 parent::setUp();
7fbb4198 32 $this->_groupId = $this->groupCreate();
33 $this->_contactId = $this->individualCreate();
34 $this->createLoggedInUser();
9099cab3 35 $ufGroup = $this->callAPISuccess('uf_group', 'create', [
7fbb4198 36 'group_type' => 'Contact',
92915c55 37 'help_pre' => 'Profile to Test API',
6c6e6187 38 'title' => 'Test Profile',
9099cab3 39 ]);
7fbb4198 40 $this->_ufGroupId = $ufGroup['id'];
9099cab3 41 $ufMatch = $this->callAPISuccess('uf_match', 'create', [
7fbb4198 42 'contact_id' => $this->_contactId,
43 'uf_id' => 42,
44 'uf_name' => 'email@mail.com',
9099cab3 45 ]);
08fe8c7e 46 $this->_ufMatchId = $ufMatch['id'];
9099cab3 47 $this->params = [
6a488035 48 'add_captcha' => 1,
7fbb4198 49 'add_contact_to_group' => $this->_groupId,
50 'group' => $this->_groupId,
6a488035
TO
51 'cancel_URL' => 'http://example.org/cancel',
52 'created_date' => '2009-06-27 00:00:00',
7fbb4198 53 'created_id' => $this->_contactId,
6a488035
TO
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',
9099cab3 68 ];
6a488035
TO
69 }
70
00be9182 71 public function tearDown() {
6a488035
TO
72 // Truncate the tables
73 $this->quickCleanup(
9099cab3 74 [
6a488035
TO
75 'civicrm_group',
76 'civicrm_contact',
77 'civicrm_uf_group',
78 'civicrm_uf_join',
79 'civicrm_uf_match',
9099cab3 80 ]
6a488035
TO
81 );
82 }
83
84 /**
2d932085
CW
85 * @param int $version
86 * @dataProvider versionThreeAndFour
6a488035 87 */
2d932085
CW
88 public function testUpdateUFGroup($version) {
89 $this->_apiversion = $version;
9099cab3 90 $params = [
6a488035
TO
91 'title' => 'Edited Test Profile',
92 'help_post' => 'Profile Pro help text.',
93 'is_active' => 1,
21dfd5f5 94 'id' => $this->_ufGroupId,
9099cab3 95 ];
6a488035 96
7fbb4198 97 $result = $this->callAPISuccess('uf_group', 'create', $params);
6a488035
TO
98 foreach ($params as $key => $value) {
99 $this->assertEquals($result['values'][$result['id']][$key], $value);
100 }
101 }
102
2d932085
CW
103 /**
104 * @param int $version
105 * @dataProvider versionThreeAndFour
106 */
107 public function testUFGroupCreate($version) {
108 $this->_apiversion = $version;
7fbb4198 109 $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__);
6a73ef3f 110 $this->assertAPISuccess($result);
ba4a1892
TM
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']);
6a488035
TO
113 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
114 foreach ($this->params as $key => $value) {
7fbb4198 115 if ($key == 'add_contact_to_group' or $key == 'group') {
6a488035
TO
116 continue;
117 }
6a488035 118 $received = $result['values'][$result['id']][$key];
2929a8fb
CW
119 if ($key == 'group_type' && $version == 4) {
120 $received = implode(',', $received);
121 }
122 $expected = $this->params[$key];
6a488035
TO
123 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
124 }
125 }
126
2d932085
CW
127 /**
128 * @param int $version
129 * @dataProvider versionThreeAndFour
130 */
131 public function testUFGroupCreateWithWrongParams($version) {
132 $this->_apiversion = $version;
9099cab3 133 $result = $this->callAPIFailure('uf_group', 'create', ['name' => 'A title-less group']);
6a488035
TO
134 }
135
2d932085
CW
136 /**
137 * @param int $version
138 * @dataProvider versionThreeAndFour
139 */
140 public function testUFGroupUpdate($version) {
141 $this->_apiversion = $version;
9099cab3 142 $params = [
6a488035
TO
143 'id' => $this->_ufGroupId,
144 'add_captcha' => 1,
7fbb4198 145 'add_contact_to_group' => $this->_groupId,
6a488035
TO
146 'cancel_URL' => 'http://example.org/cancel',
147 'created_date' => '2009-06-27',
7fbb4198 148 'created_id' => $this->_contactId,
149 'group' => $this->_groupId,
6a488035
TO
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',
92915c55 163 'title' => 'Test Group',
9099cab3 164 ];
7fbb4198 165 $result = $this->callAPISuccess('uf_group', 'create', $params);
6a488035
TO
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 }
2929a8fb
CW
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);
6a488035
TO
176 }
177
ba4a1892
TM
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']);
6a488035
TO
180 }
181
2d932085
CW
182 /**
183 * @param int $version
184 * @dataProvider versionThreeAndFour
185 */
186 public function testUFGroupGet($version) {
187 $this->_apiversion = $version;
7fbb4198 188 $result = $this->callAPISuccess('uf_group', 'create', $this->params);
9099cab3 189 $params = ['id' => $result['id']];
7fbb4198 190 $result = $this->callAPIAndDocument('uf_group', 'get', $params, __FUNCTION__, __FILE__);
ba4a1892
TM
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']);
6a488035
TO
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
7fbb4198 195 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'created_date') {
6a488035
TO
196 continue;
197 }
198 $expected = $this->params[$key];
199 $received = $result['values'][$result['id']][$key];
2d932085
CW
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 }
6a488035
TO
204 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
205 }
206 }
207
2d932085
CW
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');
6a488035
TO
215 }
216
2d932085
CW
217 /**
218 * @param int $version
219 * @dataProvider versionThreeAndFour
220 */
221 public function testUFGroupDelete($version) {
222 $this->_apiversion = $version;
7fbb4198 223 $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params);
9099cab3 224 $params = ['id' => $ufGroup['id']];
7fbb4198 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__);
6a488035 228 }
96025800 229
6a488035 230}