Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / tests / phpunit / api / v3 / UFFieldTest.php
CommitLineData
b6708aeb 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
b6708aeb 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
b6708aeb 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
b6708aeb 27
b6708aeb 28/**
29 * Test class for UFGroup API - civicrm_uf_*
b8ba5485 30 *
b6708aeb 31 * @todo Split UFGroup and UFJoin tests
32 *
33 * @package CiviCRM
acb109b7 34 * @group headless
b6708aeb 35 */
36class api_v3_UFFieldTest extends CiviUnitTestCase {
b8ba5485 37
39b959db
SL
38 /**
39 * ids from the uf_group_test.xml fixture
b8ba5485 40 *
39b959db
SL
41 * @var int
42 */
b6708aeb 43 protected $_ufGroupId = 11;
b8ba5485 44
b6708aeb 45 protected $_ufFieldId;
b8ba5485 46
b6708aeb 47 protected $_contactId = 69;
b8ba5485 48
b6708aeb 49 protected $_params;
b8ba5485 50
9443c775 51 protected $_entity = 'uf_field';
b7c9bc4c 52
0042841c 53 /**
54 * Set up for test.
55 *
56 * @throws \Exception
57 */
b6708aeb 58 protected function setUp() {
59 parent::setUp();
60 $this->quickCleanup(
b8ba5485 61 [
b6708aeb 62 'civicrm_group',
63 'civicrm_contact',
64 'civicrm_uf_group',
65 'civicrm_uf_field',
66 'civicrm_uf_join',
67 'civicrm_uf_match',
b8ba5485 68 ]
b6708aeb 69 );
70
1d291c60 71 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
b6708aeb 72
b8ba5485 73 $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
b4bb913e 74
b8ba5485 75 $this->_params = [
b6708aeb 76 'field_name' => 'phone',
77 'field_type' => 'Contact',
78 'visibility' => 'Public Pages and Listings',
79 'weight' => 1,
80 'label' => 'Test Phone',
81 'is_searchable' => 1,
82 'is_active' => 1,
83 'location_type_id' => 1,
84 'phone_type_id' => 1,
b6708aeb 85 'uf_group_id' => $this->_ufGroupId,
b8ba5485 86 ];
b6708aeb 87 }
88
0042841c 89 /**
90 * Tear down function.
91 *
92 * @throws \Exception
93 */
00be9182 94 public function tearDown() {
b6708aeb 95 $this->quickCleanup(
b8ba5485 96 [
b6708aeb 97 'civicrm_group',
98 'civicrm_contact',
99 'civicrm_uf_group',
100 'civicrm_uf_join',
101 'civicrm_uf_match',
b8ba5485 102 ]
b6708aeb 103 );
104 }
105
106 /**
0042841c 107 * Create / updating field.
b6708aeb 108 */
109 public function testCreateUFField() {
0042841c 110 $params = $this->_params;
e6ff1593 111 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__, __FILE__);
b6708aeb 112 unset($params['uf_group_id']);
113 $this->_ufFieldId = $ufField['id'];
b6708aeb 114 foreach ($params as $key => $value) {
115 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
116 }
117 }
118
0042841c 119 /**
120 * Failure test for field_name.
121 */
b6708aeb 122 public function testCreateUFFieldWithBadFieldName() {
0042841c 123 $params = $this->_params;
124 $params['field_name'] = 'custom_98789';
9443c775 125 $this->callAPIFailure('uf_field', 'create', $params);
b6708aeb 126 }
127
0042841c 128 /**
129 * Failure test for bad parameters.
130 */
00be9182 131 public function testCreateUFFieldWithWrongParams() {
b8ba5485 132 $this->callAPIFailure('uf_field', 'create', ['field_name' => 'test field']);
133 $this->callAPIFailure('uf_field', 'create', ['label' => 'name-less field']);
b6708aeb 134 }
92915c55 135
b6708aeb 136 /**
0042841c 137 * Create a field with 'weight=1' and then a second with 'weight=1'.
138 *
139 * The second field winds up with weight=1, and the first field gets bumped to 'weight=2'.
b6708aeb 140 */
141 public function testCreateUFFieldWithDefaultAutoWeight() {
0042841c 142 $params1 = $this->_params;
9443c775 143 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
b6708aeb 144 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
b8ba5485 145 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
146 1 => [$ufField1['id'], 'Int'],
147 ]);
b6708aeb 148
0042841c 149 $params2 = $this->_params;
150 // needs to be a different field
151 $params2['location_type_id'] = 2;
9443c775 152 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
b6708aeb 153 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
b8ba5485 154 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
155 1 => [$ufField2['id'], 'Int'],
156 ]);
157 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
158 1 => [$ufField1['id'], 'Int'],
159 ]);
b6708aeb 160 }
161
162 /**
eceb18cc 163 * Deleting field.
b6708aeb 164 */
165 public function testDeleteUFField() {
9443c775 166 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params);
b8ba5485 167 $params = [
b6708aeb 168 'field_id' => $ufField['id'],
b8ba5485 169 ];
0042841c 170 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__, __FILE__);
b6708aeb 171 }
172
0042841c 173 /**
174 * Test getting ufField.
175 */
b6708aeb 176 public function testGetUFFieldSuccess() {
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.
246 */
247 public function testProfilesWithoutACL() {
248 $this->createLoggedInUser();
b8ba5485 249 $baseFields[] = [
181f536c 250 'field_name' => 'first_name',
251 'field_type' => 'Contact',
252 'visibility' => 'Public Pages and Listings',
253 'weight' => 3,
254 'label' => 'Test First Name',
255 'is_searchable' => 1,
256 'is_active' => 1,
b8ba5485 257 ];
258 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
259 $params = [
181f536c 260 'uf_group_id' => $this->_ufGroupId,
261 'option.autoweight' => FALSE,
262 'values' => $baseFields,
263 'check_permissions' => TRUE,
b8ba5485 264 ];
181f536c 265 $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID');
0042841c 266 $this->callAPIFailure('uf_field', 'replace', $params);
181f536c 267 }
268
269 /**
270 * Check Profile ACL for API permission.
271 */
272 public function testACLPermissionforProfiles() {
273 $this->createLoggedInUser();
b8ba5485 274 $this->_permissionedGroup = $this->groupCreate([
181f536c 275 'title' => 'Edit Profiles',
276 'is_active' => 1,
277 'name' => 'edit-profiles',
b8ba5485 278 ]);
181f536c 279 $this->setupACL(TRUE);
280 $this->testReplaceUFFields();
281 }
282
b6708aeb 283}