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