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