3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for UFField API
18 class api_v3_UFFieldTest
extends CiviUnitTestCase
{
21 * ids from the uf_group_test.xml fixture
25 protected $_ufGroupId = 11;
27 protected $_ufFieldId;
29 protected $_contactId = 69;
33 protected $_entity = 'uf_field';
40 protected function setUp() {
53 $this->loadXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml');
55 $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
58 'field_name' => 'phone',
59 'field_type' => 'Contact',
60 'visibility' => 'Public Pages and Listings',
62 'label' => 'Test Phone',
65 'location_type_id' => 1,
67 'uf_group_id' => $this->_ufGroupId
,
76 public function tearDown() {
89 * Create / updating field.
91 * @dataProvider versionThreeAndFour
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]);
105 * Failure test for field_name.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
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);
117 * Failure test for bad parameters.
118 * @param int $version
119 * @dataProvider versionThreeAndFour
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']);
128 * Create a field with 'weight=1' and then a second with 'weight=1'.
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
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'],
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'],
151 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
152 1 => [$ufField1['id'], 'Int'],
158 * @param int $version
159 * @dataProvider versionThreeAndFour
161 public function testDeleteUFField($version) {
162 $this->_apiversion
= $version;
163 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
165 'field_id' => $ufField['id'],
167 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
171 * Test getting ufField.
172 * @param int $version
173 * @dataProvider versionThreeAndFour
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
);
183 * Create / updating field.
185 public function testReplaceUFFields() {
188 'field_name' => 'first_name',
189 'field_type' => 'Contact',
190 'visibility' => 'Public Pages and Listings',
192 'label' => 'Test First Name',
193 'is_searchable' => 1,
197 'field_name' => 'country',
198 'field_type' => 'Contact',
199 'visibility' => 'Public Pages and Listings',
201 'label' => 'Test Country',
202 'is_searchable' => 1,
204 'location_type_id' => 1,
207 'field_name' => 'phone',
208 'field_type' => 'Contact',
209 'visibility' => 'Public Pages and Listings',
211 'label' => 'Test Phone',
212 'is_searchable' => 1,
214 'location_type_id' => 1,
215 'phone_type_id' => 1,
219 'uf_group_id' => $this->_ufGroupId
,
220 'option.autoweight' => FALSE,
221 'values' => $baseFields,
222 'check_permissions' => TRUE,
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'],
245 * Check Profile API permission without ACL.
246 * @param int $version
247 * @dataProvider versionThreeAndFour
249 public function testProfilesWithoutACL($version) {
250 $this->_apiversion
= $version;
251 $this->createLoggedInUser();
253 'field_name' => 'first_name',
254 'field_type' => 'Contact',
255 'visibility' => 'Public Pages and Listings',
257 'label' => 'Test First Name',
258 'is_searchable' => 1,
261 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM'];
263 'uf_group_id' => $this->_ufGroupId
,
264 'option.autoweight' => FALSE,
265 'values' => $baseFields,
266 'check_permissions' => TRUE,
268 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
269 $this->callAPIFailure('uf_field', 'replace', $params);
273 * Check Profile ACL for API permission.
275 public function testACLPermissionforProfiles() {
276 $this->createLoggedInUser();
277 $this->_permissionedGroup
= $this->groupCreate([
278 'title' => 'Edit Profiles',
280 'name' => 'edit-profiles',
282 $this->setupACL(TRUE);
283 $this->testReplaceUFFields();