Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[civicrm-core.git] / tests / phpunit / api / v3 / UFFieldTest.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 UFField API
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_UFFieldTest extends CiviUnitTestCase {
19
20 /**
21 * ids from the uf_group_test.xml fixture
22 *
23 * @var int
24 */
25 protected $_ufGroupId = 11;
26
27 protected $_ufFieldId;
28
29 protected $_contactId = 69;
30
31 protected $_params;
32
33 protected $_entity = 'uf_field';
34
35 /**
36 * Set up for test.
37 *
38 * @throws \Exception
39 */
40 protected function setUp() {
41 parent::setUp();
42 $this->quickCleanup(
43 [
44 'civicrm_group',
45 'civicrm_contact',
46 'civicrm_uf_group',
47 'civicrm_uf_field',
48 'civicrm_uf_join',
49 'civicrm_uf_match',
50 ]
51 );
52
53 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
54
55 $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
56
57 $this->_params = [
58 'field_name' => 'phone',
59 'field_type' => 'Contact',
60 'visibility' => 'Public Pages and Listings',
61 'weight' => 1,
62 'label' => 'Test Phone',
63 'is_searchable' => 1,
64 'is_active' => 1,
65 'location_type_id' => 1,
66 'phone_type_id' => 1,
67 'uf_group_id' => $this->_ufGroupId,
68 ];
69 }
70
71 /**
72 * Tear down function.
73 *
74 * @throws \Exception
75 */
76 public function tearDown() {
77 $this->quickCleanup(
78 [
79 'civicrm_group',
80 'civicrm_contact',
81 'civicrm_uf_group',
82 'civicrm_uf_join',
83 'civicrm_uf_match',
84 ]
85 );
86 }
87
88 /**
89 * Create / updating field.
90 * @param int $version
91 * @dataProvider versionThreeAndFour
92 */
93 public function testCreateUFField($version) {
94 $this->_apiversion = $version;
95 $params = $this->_params;
96 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
97 unset($params['uf_group_id']);
98 $this->_ufFieldId = $ufField['id'];
99 foreach ($params as $key => $value) {
100 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
101 }
102 }
103
104 /**
105 * Failure test for field_name.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
108 */
109 public function testCreateUFFieldWithBadFieldName($version) {
110 $this->_apiversion = $version;
111 $params = $this->_params;
112 $params['field_name'] = 'custom_98789';
113 $this->callAPIFailure('uf_field', 'create', $params);
114 }
115
116 /**
117 * Failure test for bad parameters.
118 * @param int $version
119 * @dataProvider versionThreeAndFour
120 */
121 public function testCreateUFFieldWithWrongParams($version) {
122 $this->_apiversion = $version;
123 $this->callAPIFailure('uf_field', 'create', ['field_name' => 'test field']);
124 $this->callAPIFailure('uf_field', 'create', ['label' => 'name-less field']);
125 }
126
127 /**
128 * Create a field with 'weight=1' and then a second with 'weight=1'.
129 *
130 * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
131 * @param int $version
132 * @dataProvider versionThreeAndFour
133 */
134 public function testCreateUFFieldWithDefaultAutoWeight($version) {
135 $this->_apiversion = $version;
136 $params1 = $this->_params;
137 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
138 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
139 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
140 1 => [$ufField1['id'], 'Int'],
141 ]);
142
143 $params2 = $this->_params;
144 // needs to be a different field
145 $params2['location_type_id'] = 2;
146 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
147 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
148 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
149 1 => [$ufField2['id'], 'Int'],
150 ]);
151 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
152 1 => [$ufField1['id'], 'Int'],
153 ]);
154 }
155
156 /**
157 * Deleting field.
158 * @param int $version
159 * @dataProvider versionThreeAndFour
160 */
161 public function testDeleteUFField($version) {
162 $this->_apiversion = $version;
163 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
164 $params = [
165 'field_id' => $ufField['id'],
166 ];
167 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
168 }
169
170 /**
171 * Test getting ufField.
172 * @param int $version
173 * @dataProvider versionThreeAndFour
174 */
175 public function testGetUFFieldSuccess($version) {
176 $this->_apiversion = $version;
177 $this->callAPISuccess($this->_entity, 'create', $this->_params);
178 $result = $this->callAPIAndDocument($this->_entity, 'get', [], __FUNCTION__, __FILE__);
179 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
180 }
181
182 /**
183 * Create / updating field.
184 */
185 public function testReplaceUFFields() {
186 $baseFields = [];
187 $baseFields[] = [
188 'field_name' => 'first_name',
189 'field_type' => 'Contact',
190 'visibility' => 'Public Pages and Listings',
191 'weight' => 3,
192 'label' => 'Test First Name',
193 'is_searchable' => 1,
194 'is_active' => 1,
195 ];
196 $baseFields[] = [
197 'field_name' => 'country',
198 'field_type' => 'Contact',
199 'visibility' => 'Public Pages and Listings',
200 'weight' => 2,
201 'label' => 'Test Country',
202 'is_searchable' => 1,
203 'is_active' => 1,
204 'location_type_id' => 1,
205 ];
206 $baseFields[] = [
207 'field_name' => 'phone',
208 'field_type' => 'Contact',
209 'visibility' => 'Public Pages and Listings',
210 'weight' => 1,
211 'label' => 'Test Phone',
212 'is_searchable' => 1,
213 'is_active' => 1,
214 'location_type_id' => 1,
215 'phone_type_id' => 1,
216 ];
217
218 $params = [
219 'uf_group_id' => $this->_ufGroupId,
220 'option.autoweight' => FALSE,
221 'values' => $baseFields,
222 'check_permissions' => TRUE,
223 ];
224
225 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__);
226 $inputsByName = CRM_Utils_Array::index(['field_name'], $params['values']);
227 $this->assertEquals(count($params['values']), count($result['values']));
228 foreach ($result['values'] as $outUfField) {
229 $this->assertTrue(is_string($outUfField['field_name']));
230 $inUfField = $inputsByName[$outUfField['field_name']];
231 foreach ($inUfField as $key => $inValue) {
232 $this->assertEquals($inValue, $outUfField[$key],
233 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
234 $outUfField['field_name'],
235 $key,
236 $inValue,
237 $outUfField[$key]
238 )
239 );
240 }
241 }
242 }
243
244 /**
245 * Check Profile API permission without ACL.
246 * @param int $version
247 * @dataProvider versionThreeAndFour
248 */
249 public function testProfilesWithoutACL($version) {
250 $this->_apiversion = $version;
251 $this->createLoggedInUser();
252 $baseFields[] = [
253 'field_name' => 'first_name',
254 'field_type' => 'Contact',
255 'visibility' => 'Public Pages and Listings',
256 'weight' => 3,
257 'label' => 'Test First Name',
258 'is_searchable' => 1,
259 'is_active' => 1,
260 ];
261 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
262 $params = [
263 'uf_group_id' => $this->_ufGroupId,
264 'option.autoweight' => FALSE,
265 'values' => $baseFields,
266 'check_permissions' => TRUE,
267 ];
268 $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
269 $this->callAPIFailure('uf_field', 'replace', $params);
270 }
271
272 /**
273 * Check Profile ACL for API permission.
274 */
275 public function testACLPermissionforProfiles() {
276 $this->createLoggedInUser();
277 $this->_permissionedGroup = $this->groupCreate([
278 'title' => 'Edit Profiles',
279 'is_active' => 1,
280 'name' => 'edit-profiles',
281 ]);
282 $this->setupACL(TRUE);
283 $this->testReplaceUFFields();
284 }
285
286 }