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