CRM-13014 support userLoginFinalize() for UF classes (fix civimobile)
[civicrm-core.git] / tests / phpunit / api / v3 / UFFieldTest.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30
31 require_once 'CiviTest/CiviUnitTestCase.php';
32
33 /**
34 * Test class for UFGroup API - civicrm_uf_*
35 * @todo Split UFGroup and UFJoin tests
36 *
37 * @package CiviCRM
38 */
39 class api_v3_UFFieldTest extends CiviUnitTestCase {
40 // ids from the uf_group_test.xml fixture
41 protected $_ufGroupId = 11;
42 protected $_ufFieldId;
43 protected $_contactId = 69;
44 protected $_apiversion;
45 protected $_params;
46 protected $_entity = 'UFField';
47 public $_eNoticeCompliant = TRUE;
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 $this->_apiversion = 3;
63 $op = new PHPUnit_Extensions_Database_Operation_Insert;
64 $op->execute(
65 $this->_dbconn,
66 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
67 );
68 $this->_sethtmlGlobals();
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 'version' => $this->_apiversion,
81 'uf_group_id' => $this->_ufGroupId,
82 );
83 }
84
85 function tearDown() {
86 $this->quickCleanup(
87 array(
88 'civicrm_group',
89 'civicrm_contact',
90 'civicrm_uf_group',
91 'civicrm_uf_join',
92 'civicrm_uf_match',
93 )
94 );
95 }
96
97 /**
98 * create / updating field
99 */
100 public function testCreateUFField() {
101 $params = $this->_params; // copy
102 $ufField = civicrm_api('uf_field', 'create', $params);
103 $this->documentMe($params, $ufField, __FUNCTION__, __FILE__);
104 unset($params['version']);
105 unset($params['uf_group_id']);
106 $this->_ufFieldId = $ufField['id'];
107 $this->assertEquals(0, $ufField['is_error'], " in line " . __LINE__);
108 foreach ($params as $key => $value) {
109 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
110 }
111 }
112
113 public function testCreateUFFieldWithBadFieldName() {
114 $params = $this->_params; // copy
115 $params['field_name'] = 'custom_98789'; // invalid field
116 $result = civicrm_api('uf_field', 'create', $params);
117 $this->assertEquals($result['is_error'], 1);
118 }
119
120 function testCreateUFFieldWithEmptyParams() {
121 $params = array();
122 $result = civicrm_api('uf_field', 'create', $params);
123 $this->assertEquals($result['is_error'], 1);
124 }
125
126 function testCreateUFFieldWithWrongParams() {
127 $result = civicrm_api('uf_field', 'create', array('field_name' => 'test field'));
128 $this->assertEquals($result['is_error'], 1);
129 $result = civicrm_api('uf_field', 'create', 'a string');
130 $this->assertEquals($result['is_error'], 1);
131 $result = civicrm_api('uf_field', 'create', array('label' => 'name-less field'));
132 $this->assertEquals($result['is_error'], 1);
133 }
134 /**
135 * Create a field with 'weight=1' and then a second with 'weight=1'. The second field
136 * winds up with weight=1, and the first field gets bumped to 'weight=2'.
137 */
138 public function testCreateUFFieldWithDefaultAutoWeight() {
139 $params1 = $this->_params; // copy
140 $ufField1 = civicrm_api('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; // copy
147 $params2['location_type_id'] = 2; // needs to be a different field
148 $ufField2 = civicrm_api('uf_field', 'create', $params2);
149 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
150 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
151 1 => array($ufField2['id'], 'Int'),
152 ));
153 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
154 1 => array($ufField1['id'], 'Int'),
155 ));
156 }
157
158 /**
159 * deleting field
160 */
161 public function testDeleteUFField() {
162
163 $ufField = civicrm_api('uf_field', 'create', $this->_params);
164 $this->assertAPISuccess($ufField, 'in line' . __LINE__);
165 $this->_ufFieldId = $ufField['id'];
166 $params = array(
167 'version' => $this->_apiversion,
168 'field_id' => $ufField['id'],
169 );
170 $result = civicrm_api('uf_field', 'delete', $params);
171 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
172 $this->assertAPISuccess($result, 0, 'in line' . __LINE__);
173 }
174
175 public function testGetUFFieldSuccess() {
176
177 civicrm_api($this->_entity, 'create', $this->_params);
178 $params = array('version' => 3);
179 $result = civicrm_api($this->_entity, 'get', $params);
180 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
181 $this->assertAPISuccess($result, 0, 'in line' . __LINE__);
182 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
183 }
184
185 /**
186 * create / updating field
187 */
188 public function testReplaceUFFields() {
189 $baseFields = array();
190 $baseFields[] = array(
191 'field_name' => 'first_name',
192 'field_type' => 'Contact',
193 'visibility' => 'Public Pages and Listings',
194 'weight' => 3,
195 'label' => 'Test First Name',
196 'is_searchable' => 1,
197 'is_active' => 1,
198 );
199 $baseFields[] = array(
200 'field_name' => 'country',
201 'field_type' => 'Contact',
202 'visibility' => 'Public Pages and Listings',
203 'weight' => 2,
204 'label' => 'Test Country',
205 'is_searchable' => 1,
206 'is_active' => 1,
207 'location_type_id' => 1,
208 );
209 $baseFields[] = array(
210 'field_name' => 'phone',
211 'field_type' => 'Contact',
212 'visibility' => 'Public Pages and Listings',
213 'weight' => 1,
214 'label' => 'Test Phone',
215 'is_searchable' => 1,
216 'is_active' => 1,
217 'location_type_id' => 1,
218 'phone_type_id' => 1,
219 );
220
221 $params = array(
222 'version' => $this->_apiversion,
223 'uf_group_id' => $this->_ufGroupId,
224 'option.autoweight' => FALSE,
225 'values' => $baseFields,
226 );
227
228 $result = civicrm_api('uf_field', 'replace', $params);
229 $this->assertAPISuccess($result);
230 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
231 $inputsByName = CRM_Utils_Array::index(array('field_name'), $params['values']);
232 $this->assertEquals(count($params['values']), count($result['values']));
233 foreach ($result['values'] as $outUfField) {
234 $this->assertTrue(is_string($outUfField['field_name']));
235 $inUfField = $inputsByName[$outUfField['field_name']];
236 foreach ($inUfField as $key => $inValue) {
237 $this->assertEquals($inValue, $outUfField[$key],
238 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
239 $outUfField['field_name'],
240 $key,
241 $inValue,
242 $outUfField[$key]
243 )
244 );
245 }
246 }
247 }
248
249 /**
250 * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
251 * (NB unclear if this is still required)
252 */
253 function _sethtmlGlobals() {
254 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
255 'required' => array(
256 'html_quickform_rule_required',
257 'HTML/QuickForm/Rule/Required.php'
258 ),
259 'maxlength' => array(
260 'html_quickform_rule_range',
261 'HTML/QuickForm/Rule/Range.php'
262 ),
263 'minlength' => array(
264 'html_quickform_rule_range',
265 'HTML/QuickForm/Rule/Range.php'
266 ),
267 'rangelength' => array(
268 'html_quickform_rule_range',
269 'HTML/QuickForm/Rule/Range.php'
270 ),
271 'email' => array(
272 'html_quickform_rule_email',
273 'HTML/QuickForm/Rule/Email.php'
274 ),
275 'regex' => array(
276 'html_quickform_rule_regex',
277 'HTML/QuickForm/Rule/Regex.php'
278 ),
279 'lettersonly' => array(
280 'html_quickform_rule_regex',
281 'HTML/QuickForm/Rule/Regex.php'
282 ),
283 'alphanumeric' => array(
284 'html_quickform_rule_regex',
285 'HTML/QuickForm/Rule/Regex.php'
286 ),
287 'numeric' => array(
288 'html_quickform_rule_regex',
289 'HTML/QuickForm/Rule/Regex.php'
290 ),
291 'nopunctuation' => array(
292 'html_quickform_rule_regex',
293 'HTML/QuickForm/Rule/Regex.php'
294 ),
295 'nonzero' => array(
296 'html_quickform_rule_regex',
297 'HTML/QuickForm/Rule/Regex.php'
298 ),
299 'callback' => array(
300 'html_quickform_rule_callback',
301 'HTML/QuickForm/Rule/Callback.php'
302 ),
303 'compare' => array(
304 'html_quickform_rule_compare',
305 'HTML/QuickForm/Rule/Compare.php'
306 )
307 );
308 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
309 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array(
310 'group' => array(
311 'HTML/QuickForm/group.php',
312 'HTML_QuickForm_group'
313 ),
314 'hidden' => array(
315 'HTML/QuickForm/hidden.php',
316 'HTML_QuickForm_hidden'
317 ),
318 'reset' => array(
319 'HTML/QuickForm/reset.php',
320 'HTML_QuickForm_reset'
321 ),
322 'checkbox' => array(
323 'HTML/QuickForm/checkbox.php',
324 'HTML_QuickForm_checkbox'
325 ),
326 'file' => array(
327 'HTML/QuickForm/file.php',
328 'HTML_QuickForm_file'
329 ),
330 'image' => array(
331 'HTML/QuickForm/image.php',
332 'HTML_QuickForm_image'
333 ),
334 'password' => array(
335 'HTML/QuickForm/password.php',
336 'HTML_QuickForm_password'
337 ),
338 'radio' => array(
339 'HTML/QuickForm/radio.php',
340 'HTML_QuickForm_radio'
341 ),
342 'button' => array(
343 'HTML/QuickForm/button.php',
344 'HTML_QuickForm_button'
345 ),
346 'submit' => array(
347 'HTML/QuickForm/submit.php',
348 'HTML_QuickForm_submit'
349 ),
350 'select' => array(
351 'HTML/QuickForm/select.php',
352 'HTML_QuickForm_select'
353 ),
354 'hiddenselect' => array(
355 'HTML/QuickForm/hiddenselect.php',
356 'HTML_QuickForm_hiddenselect'
357 ),
358 'text' => array(
359 'HTML/QuickForm/text.php',
360 'HTML_QuickForm_text'
361 ),
362 'textarea' => array(
363 'HTML/QuickForm/textarea.php',
364 'HTML_QuickForm_textarea'
365 ),
366 'fckeditor' => array(
367 'HTML/QuickForm/fckeditor.php',
368 'HTML_QuickForm_FCKEditor'
369 ),
370 'tinymce' => array(
371 'HTML/QuickForm/tinymce.php',
372 'HTML_QuickForm_TinyMCE'
373 ),
374 'dojoeditor' => array(
375 'HTML/QuickForm/dojoeditor.php',
376 'HTML_QuickForm_dojoeditor'
377 ),
378 'link' => array(
379 'HTML/QuickForm/link.php',
380 'HTML_QuickForm_link'
381 ),
382 'advcheckbox' => array(
383 'HTML/QuickForm/advcheckbox.php',
384 'HTML_QuickForm_advcheckbox'
385 ),
386 'date' => array(
387 'HTML/QuickForm/date.php',
388 'HTML_QuickForm_date'
389 ),
390 'static' => array(
391 'HTML/QuickForm/static.php',
392 'HTML_QuickForm_static'
393 ),
394 'header' => array(
395 'HTML/QuickForm/header.php',
396 'HTML_QuickForm_header'
397 ),
398 'html' => array(
399 'HTML/QuickForm/html.php',
400 'HTML_QuickForm_html'
401 ),
402 'hierselect' => array(
403 'HTML/QuickForm/hierselect.php',
404 'HTML_QuickForm_hierselect'
405 ),
406 'autocomplete' => array(
407 'HTML/QuickForm/autocomplete.php',
408 'HTML_QuickForm_autocomplete'
409 ),
410 'xbutton' => array(
411 'HTML/QuickForm/xbutton.php',
412 'HTML_QuickForm_xbutton'
413 ),
414 'advmultiselect' => array(
415 'HTML/QuickForm/advmultiselect.php',
416 'HTML_QuickForm_advmultiselect'
417 )
418 );
419 }
420 }