3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for UFField API
34 class api_v3_UFFieldTest
extends CiviUnitTestCase
{
37 * ids from the uf_group_test.xml fixture
41 protected $_ufGroupId = 11;
43 protected $_ufFieldId;
45 protected $_contactId = 69;
49 protected $_entity = 'uf_field';
56 protected function setUp() {
69 $this->loadXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml');
71 $this->callAPISuccess('uf_field', 'getfields', ['cache_clear' => 1]);
74 'field_name' => 'phone',
75 'field_type' => 'Contact',
76 'visibility' => 'Public Pages and Listings',
78 'label' => 'Test Phone',
81 'location_type_id' => 1,
83 'uf_group_id' => $this->_ufGroupId
,
92 public function tearDown() {
105 * Create / updating field.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
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]);
121 * Failure test for field_name.
122 * @param int $version
123 * @dataProvider versionThreeAndFour
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);
133 * Failure test for bad parameters.
134 * @param int $version
135 * @dataProvider versionThreeAndFour
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']);
144 * Create a field with 'weight=1' and then a second with 'weight=1'.
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
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'],
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'],
167 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', [
168 1 => [$ufField1['id'], 'Int'],
174 * @param int $version
175 * @dataProvider versionThreeAndFour
177 public function testDeleteUFField($version) {
178 $this->_apiversion
= $version;
179 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
181 'field_id' => $ufField['id'],
183 $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
187 * Test getting ufField.
188 * @param int $version
189 * @dataProvider versionThreeAndFour
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
);
199 * Create / updating field.
201 public function testReplaceUFFields() {
204 'field_name' => 'first_name',
205 'field_type' => 'Contact',
206 'visibility' => 'Public Pages and Listings',
208 'label' => 'Test First Name',
209 'is_searchable' => 1,
213 'field_name' => 'country',
214 'field_type' => 'Contact',
215 'visibility' => 'Public Pages and Listings',
217 'label' => 'Test Country',
218 'is_searchable' => 1,
220 'location_type_id' => 1,
223 'field_name' => 'phone',
224 'field_type' => 'Contact',
225 'visibility' => 'Public Pages and Listings',
227 'label' => 'Test Phone',
228 'is_searchable' => 1,
230 'location_type_id' => 1,
231 'phone_type_id' => 1,
235 'uf_group_id' => $this->_ufGroupId
,
236 'option.autoweight' => FALSE,
237 'values' => $baseFields,
238 'check_permissions' => TRUE,
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'],
261 * Check Profile API permission without ACL.
262 * @param int $version
263 * @dataProvider versionThreeAndFour
265 public function testProfilesWithoutACL($version) {
266 $this->_apiversion
= $version;
267 $this->createLoggedInUser();
269 'field_name' => 'first_name',
270 'field_type' => 'Contact',
271 'visibility' => 'Public Pages and Listings',
273 'label' => 'Test First Name',
274 'is_searchable' => 1,
277 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM'];
279 'uf_group_id' => $this->_ufGroupId
,
280 'option.autoweight' => FALSE,
281 'values' => $baseFields,
282 'check_permissions' => TRUE,
284 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
285 $this->callAPIFailure('uf_field', 'replace', $params);
289 * Check Profile ACL for API permission.
291 public function testACLPermissionforProfiles() {
292 $this->createLoggedInUser();
293 $this->_permissionedGroup
= $this->groupCreate([
294 'title' => 'Edit Profiles',
296 'name' => 'edit-profiles',
298 $this->setupACL(TRUE);
299 $this->testReplaceUFFields();