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