5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 require_once 'CiviTest/CiviUnitTestCase.php';
34 * Test class for UFGroup API - civicrm_uf_*
35 * @todo Split UFGroup and UFJoin tests
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;
46 protected $_entity = 'UFField';
47 public $_eNoticeCompliant = TRUE;
49 protected function setUp() {
62 $this->_apiversion
= 3;
63 $op = new PHPUnit_Extensions_Database_Operation_Insert
;
66 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml')
68 $this->_sethtmlGlobals();
70 $this->_params
= array(
71 'field_name' => 'phone',
72 'field_type' => 'Contact',
73 'visibility' => 'Public Pages and Listings',
75 'label' => 'Test Phone',
78 'location_type_id' => 1,
80 'version' => $this->_apiversion
,
81 'uf_group_id' => $this->_ufGroupId
,
98 * create / updating field
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]);
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);
120 function testCreateUFFieldWithEmptyParams() {
122 $result = civicrm_api('uf_field', 'create', $params);
123 $this->assertEquals($result['is_error'], 1);
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);
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'.
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'),
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'),
153 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
154 1 => array($ufField1['id'], 'Int'),
161 public function testDeleteUFField() {
163 $ufField = civicrm_api('uf_field', 'create', $this->_params
);
164 $this->assertAPISuccess($ufField, 'in line' . __LINE__
);
165 $this->_ufFieldId
= $ufField['id'];
167 'version' => $this->_apiversion
,
168 'field_id' => $ufField['id'],
170 $result = civicrm_api('uf_field', 'delete', $params);
171 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
172 $this->assertAPISuccess($result, 0, 'in line' . __LINE__
);
175 public function testGetUFFieldSuccess() {
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
);
186 * create / updating field
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',
195 'label' => 'Test First Name',
196 'is_searchable' => 1,
199 $baseFields[] = array(
200 'field_name' => 'country',
201 'field_type' => 'Contact',
202 'visibility' => 'Public Pages and Listings',
204 'label' => 'Test Country',
205 'is_searchable' => 1,
207 'location_type_id' => 1,
209 $baseFields[] = array(
210 'field_name' => 'phone',
211 'field_type' => 'Contact',
212 'visibility' => 'Public Pages and Listings',
214 'label' => 'Test Phone',
215 'is_searchable' => 1,
217 'location_type_id' => 1,
218 'phone_type_id' => 1,
222 'version' => $this->_apiversion
,
223 'uf_group_id' => $this->_ufGroupId
,
224 'option.autoweight' => FALSE,
225 'values' => $baseFields,
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'],
250 * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
251 * (NB unclear if this is still required)
253 function _sethtmlGlobals() {
254 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
256 'html_quickform_rule_required',
257 'HTML/QuickForm/Rule/Required.php'
259 'maxlength' => array(
260 'html_quickform_rule_range',
261 'HTML/QuickForm/Rule/Range.php'
263 'minlength' => array(
264 'html_quickform_rule_range',
265 'HTML/QuickForm/Rule/Range.php'
267 'rangelength' => array(
268 'html_quickform_rule_range',
269 'HTML/QuickForm/Rule/Range.php'
272 'html_quickform_rule_email',
273 'HTML/QuickForm/Rule/Email.php'
276 'html_quickform_rule_regex',
277 'HTML/QuickForm/Rule/Regex.php'
279 'lettersonly' => array(
280 'html_quickform_rule_regex',
281 'HTML/QuickForm/Rule/Regex.php'
283 'alphanumeric' => array(
284 'html_quickform_rule_regex',
285 'HTML/QuickForm/Rule/Regex.php'
288 'html_quickform_rule_regex',
289 'HTML/QuickForm/Rule/Regex.php'
291 'nopunctuation' => array(
292 'html_quickform_rule_regex',
293 'HTML/QuickForm/Rule/Regex.php'
296 'html_quickform_rule_regex',
297 'HTML/QuickForm/Rule/Regex.php'
300 'html_quickform_rule_callback',
301 'HTML/QuickForm/Rule/Callback.php'
304 'html_quickform_rule_compare',
305 'HTML/QuickForm/Rule/Compare.php'
308 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
309 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array(
311 'HTML/QuickForm/group.php',
312 'HTML_QuickForm_group'
315 'HTML/QuickForm/hidden.php',
316 'HTML_QuickForm_hidden'
319 'HTML/QuickForm/reset.php',
320 'HTML_QuickForm_reset'
323 'HTML/QuickForm/checkbox.php',
324 'HTML_QuickForm_checkbox'
327 'HTML/QuickForm/file.php',
328 'HTML_QuickForm_file'
331 'HTML/QuickForm/image.php',
332 'HTML_QuickForm_image'
335 'HTML/QuickForm/password.php',
336 'HTML_QuickForm_password'
339 'HTML/QuickForm/radio.php',
340 'HTML_QuickForm_radio'
343 'HTML/QuickForm/button.php',
344 'HTML_QuickForm_button'
347 'HTML/QuickForm/submit.php',
348 'HTML_QuickForm_submit'
351 'HTML/QuickForm/select.php',
352 'HTML_QuickForm_select'
354 'hiddenselect' => array(
355 'HTML/QuickForm/hiddenselect.php',
356 'HTML_QuickForm_hiddenselect'
359 'HTML/QuickForm/text.php',
360 'HTML_QuickForm_text'
363 'HTML/QuickForm/textarea.php',
364 'HTML_QuickForm_textarea'
366 'fckeditor' => array(
367 'HTML/QuickForm/fckeditor.php',
368 'HTML_QuickForm_FCKEditor'
371 'HTML/QuickForm/tinymce.php',
372 'HTML_QuickForm_TinyMCE'
374 'dojoeditor' => array(
375 'HTML/QuickForm/dojoeditor.php',
376 'HTML_QuickForm_dojoeditor'
379 'HTML/QuickForm/link.php',
380 'HTML_QuickForm_link'
382 'advcheckbox' => array(
383 'HTML/QuickForm/advcheckbox.php',
384 'HTML_QuickForm_advcheckbox'
387 'HTML/QuickForm/date.php',
388 'HTML_QuickForm_date'
391 'HTML/QuickForm/static.php',
392 'HTML_QuickForm_static'
395 'HTML/QuickForm/header.php',
396 'HTML_QuickForm_header'
399 'HTML/QuickForm/html.php',
400 'HTML_QuickForm_html'
402 'hierselect' => array(
403 'HTML/QuickForm/hierselect.php',
404 'HTML_QuickForm_hierselect'
406 'autocomplete' => array(
407 'HTML/QuickForm/autocomplete.php',
408 'HTML_QuickForm_autocomplete'
411 'HTML/QuickForm/xbutton.php',
412 'HTML_QuickForm_xbutton'
414 'advmultiselect' => array(
415 'HTML/QuickForm/advmultiselect.php',
416 'HTML_QuickForm_advmultiselect'