Add in Country and StateProvince APIv4 Entities
[civicrm-core.git] / tests / phpunit / api / v3 / UFFieldTest.php
CommitLineData
b6708aeb 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
b6708aeb 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 |
b6708aeb 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
b6708aeb 11
b6708aeb 12/**
2d932085 13 * Test class for UFField API
b6708aeb 14 *
15 * @package CiviCRM
acb109b7 16 * @group headless
b6708aeb 17 */
18class api_v3_UFFieldTest extends CiviUnitTestCase {
b8ba5485 19
39b959db
SL
20 /**
21 * ids from the uf_group_test.xml fixture
b8ba5485 22 *
39b959db
SL
23 * @var int
24 */
b6708aeb 25 protected $_ufGroupId = 11;
b8ba5485 26
b6708aeb 27 protected $_ufFieldId;
b8ba5485 28
b6708aeb 29 protected $_contactId = 69;
b8ba5485 30
b6708aeb 31 protected $_params;
b8ba5485 32
9443c775 33 protected $_entity = 'uf_field';
b7c9bc4c 34
0042841c 35 /**
36 * Set up for test.
37 *
38 * @throws \Exception
39 */
b6708aeb 40 protected function setUp() {
41 parent::setUp();
42 $this->quickCleanup(
b8ba5485 43 [
b6708aeb 44 'civicrm_group',
45 'civicrm_contact',
46 'civicrm_uf_group',
47 'civicrm_uf_field',
48 'civicrm_uf_join',
49 'civicrm_uf_match',
b8ba5485 50 ]
b6708aeb 51 );
52
1d291c60 53 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
b6708aeb 54
b8ba5485 55 $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
b4bb913e 56
b8ba5485 57 $this->_params = [
b6708aeb 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,
b6708aeb 67 'uf_group_id' => $this->_ufGroupId,
b8ba5485 68 ];
b6708aeb 69 }
70
0042841c 71 /**
72 * Tear down function.
73 *
74 * @throws \Exception
75 */
00be9182 76 public function tearDown() {
b6708aeb 77 $this->quickCleanup(
b8ba5485 78 [
b6708aeb 79 'civicrm_group',
80 'civicrm_contact',
81 'civicrm_uf_group',
82 'civicrm_uf_join',
83 'civicrm_uf_match',
b8ba5485 84 ]
b6708aeb 85 );
86 }
87
88 /**
0042841c 89 * Create / updating field.
2d932085
CW
90 * @param int $version
91 * @dataProvider versionThreeAndFour
b6708aeb 92 */
2d932085
CW
93 public function testCreateUFField($version) {
94 $this->_apiversion = $version;
0042841c 95 $params = $this->_params;
e6ff1593 96 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
b6708aeb 97 unset($params['uf_group_id']);
98 $this->_ufFieldId = $ufField['id'];
b6708aeb 99 foreach ($params as $key => $value) {
100 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
101 }
102 }
103
0042841c 104 /**
105 * Failure test for field_name.
2d932085
CW
106 * @param int $version
107 * @dataProvider versionThreeAndFour
0042841c 108 */
2d932085
CW
109 public function testCreateUFFieldWithBadFieldName($version) {
110 $this->_apiversion = $version;
0042841c 111 $params = $this->_params;
112 $params['field_name'] = 'custom_98789';
9443c775 113 $this->callAPIFailure('uf_field', 'create', $params);
b6708aeb 114 }
115
0042841c 116 /**
117 * Failure test for bad parameters.
2d932085
CW
118 * @param int $version
119 * @dataProvider versionThreeAndFour
0042841c 120 */
2d932085
CW
121 public function testCreateUFFieldWithWrongParams($version) {
122 $this->_apiversion = $version;
b8ba5485 123 $this->callAPIFailure('uf_field', 'create', ['field_name' => 'test field']);
124 $this->callAPIFailure('uf_field', 'create', ['label' => 'name-less field']);
b6708aeb 125 }
92915c55 126
b6708aeb 127 /**
0042841c 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'.
2d932085
CW
131 * @param int $version
132 * @dataProvider versionThreeAndFour
b6708aeb 133 */
2d932085
CW
134 public function testCreateUFFieldWithDefaultAutoWeight($version) {
135 $this->_apiversion = $version;
0042841c 136 $params1 = $this->_params;
9443c775 137 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
b6708aeb 138 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
b8ba5485 139 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
140 1 => [$ufField1['id'], 'Int'],
141 ]);
b6708aeb 142
0042841c 143 $params2 = $this->_params;
144 // needs to be a different field
145 $params2['location_type_id'] = 2;
9443c775 146 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
b6708aeb 147 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
b8ba5485 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 ]);
b6708aeb 154 }
155
156 /**
eceb18cc 157 * Deleting field.
2d932085
CW
158 * @param int $version
159 * @dataProvider versionThreeAndFour
b6708aeb 160 */
2d932085
CW
161 public function testDeleteUFField($version) {
162 $this->_apiversion = $version;
9443c775 163 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
b8ba5485 164 $params = [
b6708aeb 165 'field_id' => $ufField['id'],
b8ba5485 166 ];
0042841c 167 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
b6708aeb 168 }
169
0042841c 170 /**
171 * Test getting ufField.
2d932085
CW
172 * @param int $version
173 * @dataProvider versionThreeAndFour
0042841c 174 */
2d932085
CW
175 public function testGetUFFieldSuccess($version) {
176 $this->_apiversion = $version;
54bd1003 177 $this->callAPISuccess($this->_entity, 'create', $this->_params);
b8ba5485 178 $result = $this->callAPIAndDocument($this->_entity, 'get', [], __FUNCTION__, __FILE__);
b6708aeb 179 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
180 }
181
182 /**
0042841c 183 * Create / updating field.
b6708aeb 184 */
185 public function testReplaceUFFields() {
b8ba5485 186 $baseFields = [];
187 $baseFields[] = [
b6708aeb 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,
b8ba5485 195 ];
196 $baseFields[] = [
b6708aeb 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,
b8ba5485 205 ];
206 $baseFields[] = [
b6708aeb 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,
b8ba5485 216 ];
b6708aeb 217
b8ba5485 218 $params = [
b6708aeb 219 'uf_group_id' => $this->_ufGroupId,
220 'option.autoweight' => FALSE,
221 'values' => $baseFields,
181f536c 222 'check_permissions' => TRUE,
b8ba5485 223 ];
b6708aeb 224
e6ff1593 225 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__, __FILE__);
b8ba5485 226 $inputsByName = CRM_Utils_Array::index(['field_name'], $params['values']);
b6708aeb 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 }
96025800 243
181f536c 244 /**
245 * Check Profile API permission without ACL.
2d932085
CW
246 * @param int $version
247 * @dataProvider versionThreeAndFour
181f536c 248 */
2d932085
CW
249 public function testProfilesWithoutACL($version) {
250 $this->_apiversion = $version;
181f536c 251 $this->createLoggedInUser();
b8ba5485 252 $baseFields[] = [
181f536c 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,
b8ba5485 260 ];
261 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
262 $params = [
181f536c 263 'uf_group_id' => $this->_ufGroupId,
264 'option.autoweight' => FALSE,
265 'values' => $baseFields,
266 'check_permissions' => TRUE,
b8ba5485 267 ];
181f536c 268 $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
0042841c 269 $this->callAPIFailure('uf_field', 'replace', $params);
181f536c 270 }
271
272 /**
273 * Check Profile ACL for API permission.
274 */
275 public function testACLPermissionforProfiles() {
276 $this->createLoggedInUser();
b8ba5485 277 $this->_permissionedGroup = $this->groupCreate([
181f536c 278 'title' => 'Edit Profiles',
279 'is_active' => 1,
280 'name' => 'edit-profiles',
b8ba5485 281 ]);
181f536c 282 $this->setupACL(TRUE);
283 $this->testReplaceUFFields();
284 }
285
b6708aeb 286}