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