3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test class for UFGroup API - civicrm_uf_*
33 * @todo Split UFGroup and UFJoin tests
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;
44 protected $_entity = 'uf_field';
45 public $_eNoticeCompliant = TRUE;
47 protected function setUp() {
60 $op = new PHPUnit_Extensions_Database_Operation_Insert
;
63 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__
) . '/dataset/uf_group_test.xml')
65 $this->_sethtmlGlobals();
67 $this->callAPISuccess('uf_field', 'getfields', array('cache_clear' => 1));
69 $this->_params
= array(
70 'field_name' => 'phone',
71 'field_type' => 'Contact',
72 'visibility' => 'Public Pages and Listings',
74 'label' => 'Test Phone',
77 'location_type_id' => 1,
79 'uf_group_id' => $this->_ufGroupId
,
96 * create / updating field
98 public function testCreateUFField() {
99 $params = $this->_params
; // copy
100 $ufField = $this->callAPIAndDocument('uf_field', 'create', $params, __FUNCTION__
, __FILE__
);
101 unset($params['uf_group_id']);
102 $this->_ufFieldId
= $ufField['id'];
103 foreach ($params as $key => $value) {
104 $this->assertEquals($ufField['values'][$ufField['id']][$key], $params[$key]);
108 public function testCreateUFFieldWithBadFieldName() {
109 $params = $this->_params
; // copy
110 $params['field_name'] = 'custom_98789'; // invalid field
111 $this->callAPIFailure('uf_field', 'create', $params);
114 function testCreateUFFieldWithWrongParams() {
115 $this->callAPIFailure('uf_field', 'create', array('field_name' => 'test field'));
116 $this->callAPIFailure('uf_field', 'create', array('label' => 'name-less field'));
119 * Create a field with 'weight=1' and then a second with 'weight=1'. The second field
120 * winds up with weight=1, and the first field gets bumped to 'weight=2'.
122 public function testCreateUFFieldWithDefaultAutoWeight() {
123 $params1 = $this->_params
; // copy
124 $ufField1 = $this->callAPISuccess('uf_field', 'create', $params1);
125 $this->assertEquals(1, $ufField1['values'][$ufField1['id']]['weight']);
126 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
127 1 => array($ufField1['id'], 'Int'),
130 $params2 = $this->_params
; // copy
131 $params2['location_type_id'] = 2; // needs to be a different field
132 $ufField2 = $this->callAPISuccess('uf_field', 'create', $params2);
133 $this->assertEquals(1, $ufField2['values'][$ufField2['id']]['weight']);
134 $this->assertDBQuery(1, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
135 1 => array($ufField2['id'], 'Int'),
137 $this->assertDBQuery(2, 'SELECT weight FROM civicrm_uf_field WHERE id = %1', array(
138 1 => array($ufField1['id'], 'Int'),
145 public function testDeleteUFField() {
146 $ufField = $this->callAPISuccess('uf_field', 'create', $this->_params
);
148 'field_id' => $ufField['id'],
150 $result = $this->callAPIAndDocument('uf_field', 'delete', $params, __FUNCTION__
, __FILE__
);
153 public function testGetUFFieldSuccess() {
154 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
155 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(), __FUNCTION__
, __FILE__
);
156 $this->getAndCheck($this->_params
, $result['id'], $this->_entity
);
160 * create / updating field
162 public function testReplaceUFFields() {
163 $baseFields = array();
164 $baseFields[] = array(
165 'field_name' => 'first_name',
166 'field_type' => 'Contact',
167 'visibility' => 'Public Pages and Listings',
169 'label' => 'Test First Name',
170 'is_searchable' => 1,
173 $baseFields[] = array(
174 'field_name' => 'country',
175 'field_type' => 'Contact',
176 'visibility' => 'Public Pages and Listings',
178 'label' => 'Test Country',
179 'is_searchable' => 1,
181 'location_type_id' => 1,
183 $baseFields[] = array(
184 'field_name' => 'phone',
185 'field_type' => 'Contact',
186 'visibility' => 'Public Pages and Listings',
188 'label' => 'Test Phone',
189 'is_searchable' => 1,
191 'location_type_id' => 1,
192 'phone_type_id' => 1,
196 'uf_group_id' => $this->_ufGroupId
,
197 'option.autoweight' => FALSE,
198 'values' => $baseFields,
201 $result = $this->callAPIAndDocument('uf_field', 'replace', $params, __FUNCTION__
, __FILE__
);
202 $inputsByName = CRM_Utils_Array
::index(array('field_name'), $params['values']);
203 $this->assertEquals(count($params['values']), count($result['values']));
204 foreach ($result['values'] as $outUfField) {
205 $this->assertTrue(is_string($outUfField['field_name']));
206 $inUfField = $inputsByName[$outUfField['field_name']];
207 foreach ($inUfField as $key => $inValue) {
208 $this->assertEquals($inValue, $outUfField[$key],
209 sprintf("field_name=[%s] key=[%s] expected=[%s] actual=[%s]",
210 $outUfField['field_name'],
221 * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
222 * (NB unclear if this is still required)
224 function _sethtmlGlobals() {
225 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
227 'html_quickform_rule_required',
228 'HTML/QuickForm/Rule/Required.php'
230 'maxlength' => array(
231 'html_quickform_rule_range',
232 'HTML/QuickForm/Rule/Range.php'
234 'minlength' => array(
235 'html_quickform_rule_range',
236 'HTML/QuickForm/Rule/Range.php'
238 'rangelength' => array(
239 'html_quickform_rule_range',
240 'HTML/QuickForm/Rule/Range.php'
243 'html_quickform_rule_email',
244 'HTML/QuickForm/Rule/Email.php'
247 'html_quickform_rule_regex',
248 'HTML/QuickForm/Rule/Regex.php'
250 'lettersonly' => array(
251 'html_quickform_rule_regex',
252 'HTML/QuickForm/Rule/Regex.php'
254 'alphanumeric' => array(
255 'html_quickform_rule_regex',
256 'HTML/QuickForm/Rule/Regex.php'
259 'html_quickform_rule_regex',
260 'HTML/QuickForm/Rule/Regex.php'
262 'nopunctuation' => array(
263 'html_quickform_rule_regex',
264 'HTML/QuickForm/Rule/Regex.php'
267 'html_quickform_rule_regex',
268 'HTML/QuickForm/Rule/Regex.php'
271 'html_quickform_rule_callback',
272 'HTML/QuickForm/Rule/Callback.php'
275 'html_quickform_rule_compare',
276 'HTML/QuickForm/Rule/Compare.php'
279 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
280 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array(
282 'HTML/QuickForm/group.php',
283 'HTML_QuickForm_group'
286 'HTML/QuickForm/hidden.php',
287 'HTML_QuickForm_hidden'
290 'HTML/QuickForm/reset.php',
291 'HTML_QuickForm_reset'
294 'HTML/QuickForm/checkbox.php',
295 'HTML_QuickForm_checkbox'
298 'HTML/QuickForm/file.php',
299 'HTML_QuickForm_file'
302 'HTML/QuickForm/image.php',
303 'HTML_QuickForm_image'
306 'HTML/QuickForm/password.php',
307 'HTML_QuickForm_password'
310 'HTML/QuickForm/radio.php',
311 'HTML_QuickForm_radio'
314 'HTML/QuickForm/button.php',
315 'HTML_QuickForm_button'
318 'HTML/QuickForm/submit.php',
319 'HTML_QuickForm_submit'
322 'HTML/QuickForm/select.php',
323 'HTML_QuickForm_select'
325 'hiddenselect' => array(
326 'HTML/QuickForm/hiddenselect.php',
327 'HTML_QuickForm_hiddenselect'
330 'HTML/QuickForm/text.php',
331 'HTML_QuickForm_text'
334 'HTML/QuickForm/textarea.php',
335 'HTML_QuickForm_textarea'
337 'fckeditor' => array(
338 'HTML/QuickForm/fckeditor.php',
339 'HTML_QuickForm_FCKEditor'
342 'HTML/QuickForm/tinymce.php',
343 'HTML_QuickForm_TinyMCE'
345 'dojoeditor' => array(
346 'HTML/QuickForm/dojoeditor.php',
347 'HTML_QuickForm_dojoeditor'
350 'HTML/QuickForm/link.php',
351 'HTML_QuickForm_link'
353 'advcheckbox' => array(
354 'HTML/QuickForm/advcheckbox.php',
355 'HTML_QuickForm_advcheckbox'
358 'HTML/QuickForm/date.php',
359 'HTML_QuickForm_date'
362 'HTML/QuickForm/static.php',
363 'HTML_QuickForm_static'
366 'HTML/QuickForm/header.php',
367 'HTML_QuickForm_header'
370 'HTML/QuickForm/html.php',
371 'HTML_QuickForm_html'
373 'hierselect' => array(
374 'HTML/QuickForm/hierselect.php',
375 'HTML_QuickForm_hierselect'
377 'autocomplete' => array(
378 'HTML/QuickForm/autocomplete.php',
379 'HTML_QuickForm_autocomplete'
382 'HTML/QuickForm/xbutton.php',
383 'HTML_QuickForm_xbutton'
385 'advmultiselect' => array(
386 'HTML/QuickForm/advmultiselect.php',
387 'HTML_QuickForm_advmultiselect'