Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 along with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | */ | |
26 | ||
6a488035 TO |
27 | require_once 'CiviTest/CiviSeleniumTestCase.php'; |
28 | class WebTest_Admin_MoveCustomDataTest extends CiviSeleniumTestCase { | |
29 | ||
30 | protected function setUp() { | |
31 | parent::setUp(); | |
32 | } | |
33 | ||
34 | function testCreateCustomFields() { | |
6a488035 TO |
35 | $this->webtestLogin(); |
36 | ||
37 | $cid_all = $this->_createContact("all_data", "move_custom_data"); | |
38 | $cid_from_missing = $this->_createContact("source_missing", "move_custom_data"); | |
39 | $cid_to_missing = $this->_createContact("destination_missing", "move_custom_data"); | |
40 | ||
41 | $from_group_id = $this->_buildCustomFieldSet("source"); | |
42 | $to_group_id = $this->_buildCustomFieldSet("destination"); | |
43 | ||
44 | $this->_fillCustomDataForContact($cid_all, $from_group_id); | |
45 | $this->_fillCustomDataForContact($cid_to_missing, $from_group_id); | |
46 | ||
47 | $this->_fillCustomDataForContact($cid_all, $to_group_id); | |
48 | $this->_fillCustomDataForContact($cid_from_missing, $to_group_id); | |
49 | ||
50 | //to verify data hasn't been lost, we load the values for each contact | |
51 | $pre_move_values = array(); | |
52 | $pre_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id); | |
53 | $pre_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id); | |
54 | $pre_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id); | |
55 | $pre_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id); | |
56 | $pre_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id); | |
57 | $pre_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id); | |
58 | ||
6a488035 TO |
59 | //ok, so after all that setup, we are now good to actually move a field |
60 | ||
61 | //first, pick a random field from the source group to move | |
62 | $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $from_group_id)); | |
63 | $field_to_move = array_rand($fields['values']); | |
64 | ||
65 | //move the field | |
66 | $this->_moveCustomField($field_to_move, $from_group_id, $to_group_id); | |
67 | ||
68 | //now lets verify the data, load up the new values from the api... | |
69 | $post_move_values = array(); | |
70 | $post_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id, TRUE); | |
71 | $post_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id); | |
72 | $post_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id); | |
73 | $post_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id); | |
74 | $post_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id); | |
75 | $post_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id); | |
76 | ||
6a488035 TO |
77 | //Make sure that only the appropriate values have changed |
78 | foreach (array( | |
79 | $cid_all, $cid_from_missing, $cid_to_missing) as $cid) { | |
80 | foreach (array( | |
81 | 'source', 'destination') as $fieldset) { | |
82 | foreach ($pre_move_values[$cid][$fieldset] as $id => $value) { | |
83 | if ($id != $field_to_move) { | |
84 | //All fields that were there should still be there | |
85 | $this->assertTrue(isset($post_move_values[$cid][$fieldset][$id]), "A custom field that was not moved is missing!"); | |
86 | //All fields should have the same value as when we started | |
87 | $this->assertTrue($post_move_values[$cid][$fieldset][$id] == $value, "A custom field value has changed in the source custom field set"); | |
88 | } | |
89 | } | |
90 | } | |
91 | //check that the field is actually moved | |
92 | $this->assertTrue(!isset($post_move_values[$cid]['source'][$field_to_move]), "Moved field is still present in the source fieldset"); | |
93 | $this->assertTrue(isset($post_move_values[$cid]['destination'][$field_to_move]), "Moved field is not present in the destination fieldset"); | |
94 | $this->assertTrue($pre_move_values[$cid]['source'][$field_to_move] == $post_move_values[$cid]['destination'][$field_to_move], "The moved field has changed values!"); | |
95 | } | |
96 | ||
97 | //Go to the contacts page and check that the custom field is in the right group | |
8f737a72 | 98 | $this->openCiviPage('contact/view',"reset=1&cid={$cid_all}"); |
6a488035 TO |
99 | |
100 | //load the names of the custom fieldsets | |
101 | $source = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $from_group_id)); | |
102 | $source = $source['values'][$from_group_id]; | |
103 | $destination = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $to_group_id)); | |
104 | $destination = $destination['values'][$to_group_id]; | |
105 | ||
106 | //assert that the moved custom field is missing from the source fieldset | |
107 | $this->assertElementNotContainsText("css=div." . $source['name'], $fields['values'][$field_to_move]['label'], "Moved value still displays in the old fieldset on the contact record"); | |
108 | $this->assertElementContainsText("css=div." . $destination['name'], $fields['values'][$field_to_move]['label'], "Moved value does not display in the new fieldset on the contact record"); | |
109 | } | |
110 | ||
111 | //moves a field from one field to another | |
112 | function _moveCustomField($field_to_move, $from_group_id, $to_group_id) { | |
113 | //go to the move field page | |
8f737a72 | 114 | $this->openCiviPage('admin/custom/group/field/move', "reset=1&fid={$field_to_move}"); |
6a488035 TO |
115 | |
116 | //select the destination field set from select box | |
117 | $this->click("dst_group_id"); | |
118 | $this->select("dst_group_id", "value=" . $to_group_id); | |
119 | $this->click("//option[@value='" . $to_group_id . "']"); | |
120 | ||
121 | //click the save button | |
122 | $this->click("_qf_MoveField_next"); | |
123 | $this->waitForPageToLoad($this->getTimeoutMsec()); | |
124 | ||
8f737a72 RN |
125 | //assert that the success text is present |
126 | $this->assertElementContainsText('crm-notification-container', "has been moved", "Move field success message not displayed"); | |
6a488035 | 127 | |
76e86fd8 CW |
128 | //assert that the custom field not on old data set page |
129 | ||
6a488035 TO |
130 | $this->assertTrue(!$this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field still displays on the old fieldset page"); |
131 | ||
132 | //go to the destination fieldset and make sure the field is present | |
8f737a72 | 133 | $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$to_group_id}"); |
6a488035 TO |
134 | $this->assertTrue($this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field does not display on the new fieldset page"); |
135 | } | |
136 | ||
137 | //create a contact and return the contact id | |
138 | function _createContact($firstName = "John", $lastName = "Doe") { | |
139 | $firstName .= "_" . substr(sha1(rand()), 0, 5); | |
140 | $lastName .= "_" . substr(sha1(rand()), 0, 5); | |
141 | $this->webtestAddContact($firstName, $lastName); | |
142 | $url = $this->parseURL(); | |
143 | $cid = $url['queryString']['cid']; | |
144 | $this->assertType('numeric', $cid); | |
145 | return $cid; | |
146 | } | |
147 | ||
148 | //Get all custom field values for a given contact and custom group id using the api | |
149 | function _loadDataFromApi($contact_id, $group_id, $reset_cache = FALSE) { | |
150 | // cache the fields, just to speed things up a little | |
151 | static $field_ids = array(); | |
152 | ||
153 | if ($reset_cache) { | |
154 | $field_ids = array(); | |
155 | } | |
156 | ||
157 | //if the field ids havent been cached yet, grab them | |
158 | if (!isset($field_ids[$group_id])) { | |
159 | $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id)); | |
160 | $field_ids[$group_id] = array(); | |
161 | foreach ($fields['values'] as $id => $field) { | |
162 | $field_ids[$group_id][] = $id; | |
163 | } | |
164 | } | |
165 | ||
6a488035 TO |
166 | $params = array('contact_id' => $contact_id); |
167 | foreach ($field_ids[$group_id] as $id) { | |
168 | $params['return.custom_' . $id] = 1; | |
169 | } | |
170 | ||
171 | $contact = $this->webtest_civicrm_api("Contact", "get", $params); | |
172 | ||
173 | //clean up the api results a bit.... | |
174 | $results = array(); | |
175 | foreach ($field_ids[$group_id] as $id) { | |
176 | if (isset($contact['values'][$contact_id]['custom_' . $id])) { | |
177 | $results[$id] = $contact['values'][$contact_id]['custom_' . $id]; | |
178 | } | |
179 | } | |
180 | ||
181 | return $results; | |
182 | } | |
183 | ||
6a488035 TO |
184 | //creates a new custom group and fields in that group, and returns the group Id |
185 | function _buildCustomFieldset($prefix) { | |
186 | $group_id = $this->_createCustomGroup($prefix); | |
187 | $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "CheckBox", $prefix); | |
188 | $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Radio", $prefix); | |
189 | $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Text", $prefix); | |
190 | $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Note', "Text", $prefix); | |
191 | $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Date', "Date", $prefix); | |
192 | return $group_id; | |
193 | } | |
194 | ||
195 | //Creates a custom field group for a specific entity type and returns the custom group Id | |
196 | function _createCustomGroup($prefix = "custom", $entity = "Contact") { | |
42daf119 | 197 | |
8f737a72 | 198 | $this->openCiviPage('admin/custom/group', 'action=add&reset=1'); |
6a488035 TO |
199 | |
200 | //fill custom group title | |
201 | $customGroupTitle = $prefix . '_' . substr(sha1(rand()), 0, 7); | |
202 | $this->click("title"); | |
203 | $this->type("title", $customGroupTitle); | |
204 | ||
205 | //custom group extends | |
206 | $this->click("extends[0]"); | |
207 | $this->select("extends[0]", "value=" . $entity); | |
208 | $this->click("//option[@value='" . $entity . "']"); | |
209 | $this->click("_qf_Group_next-bottom"); | |
210 | $this->waitForElementPresent("_qf_Field_cancel-bottom"); | |
211 | ||
212 | //Is custom group created? | |
8f737a72 | 213 | $this->assertElementContainsText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.", "Group title missing"); |
6a488035 TO |
214 | |
215 | $url = $this->parseURL(); | |
216 | $group_id = $url['queryString']['gid']; | |
217 | $this->assertType('numeric', $group_id); | |
218 | ||
219 | return $group_id; | |
220 | } | |
221 | ||
222 | //Adds a new custom field to a specfied custom field group, using the given | |
223 | //datatype and widget. | |
224 | function _addCustomFieldToGroup($group_id, $type = 'Alphanumeric', $widget = 'CheckBox', $prefix = '') { | |
225 | //A mapping of data type names to integer keys | |
226 | $type_map = array( | |
227 | 'alphanumeric' => array( | |
228 | 'id' => 0, | |
229 | 'widgets' => array('Text', 'Select', 'Radio', 'CheckBox', 'Multi-Select'), | |
230 | 'options' => array('option_01', 'option_02', 'option_03', 'option_04', 'option_05', 'option_06', 'option_07', 'option_08', 'option_09', 'option_10'), | |
231 | ), | |
232 | 'integer' => array( | |
233 | 'id' => 1, | |
234 | 'widgets' => array('Text', 'Select', 'Radio'), | |
235 | 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), | |
236 | ), | |
237 | 'number' => array( | |
238 | 'id' => 2, | |
239 | 'widgets' => array('Text', 'Select', 'Radio'), | |
240 | 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1), | |
241 | ), | |
242 | 'money' => array( | |
243 | 'id' => 3, | |
244 | 'widgets' => array('Text', 'Select', 'Radio'), | |
245 | 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1), | |
246 | ), | |
247 | 'note' => array( | |
248 | 'id' => 4, | |
249 | 'widgets' => array('TextArea'), | |
250 | ), | |
251 | 'date' => array( | |
252 | 'id' => 5, | |
253 | 'widgets' => array('Date'), | |
254 | ), | |
255 | 'yes or no' => array( | |
256 | 'id' => 6, | |
257 | 'widgets' => array('Radio'), | |
258 | ), | |
259 | //'state/province' => array( | |
260 | // 'id' => 7, | |
261 | //), | |
262 | //'country' => array( | |
263 | // 'id' => 8, | |
264 | //), | |
265 | //'link' => array( | |
266 | // 'id' => 10, | |
267 | //), | |
268 | //'contact reference' => array( | |
269 | // 'id' => 11, | |
270 | //), | |
271 | //'file' => 9, hahaha im not doing files. | |
272 | ); | |
273 | ||
274 | //downcase the type | |
275 | $type = strtolower($type); | |
276 | ||
277 | //make sure that a supported html type was entered | |
278 | if (!isset($type_map[$type])) { | |
279 | $this->fail("The custom field html type $type is not supported. Supported types are: " . implode(", ", array_keys($type_map))); | |
280 | } | |
281 | $html_type_id = $type_map[$type]['id']; | |
282 | ||
283 | //make sure the widget type can be used for this data type | |
284 | //if an invalid widget is selected and only 1 widget is available, use that | |
285 | if (!in_array($widget, $type_map[$type]['widgets'])) { | |
286 | if (count($type_map[$type]['widgets']) == 1) { | |
287 | $widget = $type_map[$type]['widgets'][0]; | |
288 | } | |
289 | else { | |
290 | $this->fail("Cannot use $widget for $type fields. Available widgets are: " . implode(", ", $type_map[$type]['widgets'])); | |
291 | } | |
292 | } | |
293 | ||
294 | //Go to the add custom field page for the given group id | |
8f737a72 | 295 | $this->openCiviPage('admin/custom/group/field/add', "action=add&reset=1&gid={$group_id}"); |
6a488035 TO |
296 | |
297 | //Do common setup for all field types | |
298 | ||
299 | //set the field label | |
300 | $fieldLabel = (isset($prefix) ? $prefix . "_" : "") . $widget . "_" . substr(sha1(rand()), 0, 6); | |
301 | $this->click("label"); | |
302 | $this->type("label", $fieldLabel); | |
303 | ||
304 | //enter pre help message | |
305 | $this->type("help_pre", "this is field pre help for " . $fieldLabel); | |
306 | ||
307 | //enter post help message | |
308 | $this->type("help_post", "this field post help for " . $fieldLabel); | |
309 | ||
310 | //Is searchable? | |
311 | $this->click("is_searchable"); | |
312 | ||
313 | //Fill in the html type and widget type | |
314 | $this->click("data_type[0]"); | |
315 | $this->select("data_type[0]", "value=" . $html_type_id); | |
316 | $this->click("//option[@value='" . $html_type_id . "']"); | |
317 | $this->click("data_type[1]"); | |
318 | $this->select("data_type[1]", "value=" . $widget); | |
319 | $this->click("//option[@value='" . $widget . "']"); | |
320 | ||
321 | //fill in specific elements for different widgets | |
322 | switch ($widget) { | |
323 | case 'CheckBox': | |
324 | $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']); | |
325 | $this->type("options_per_line", "2"); | |
326 | break; | |
327 | ||
328 | case 'Radio': | |
329 | $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']); | |
330 | $this->type("options_per_line", "1"); | |
331 | break; | |
332 | ||
333 | case 'Date': | |
334 | $this->click("date_format"); | |
335 | $this->select("date_format", "value=yy-mm-dd"); | |
336 | $this->click("//option[@value='yy-mm-dd']"); | |
337 | break; | |
338 | //TODO allow for more things.... | |
339 | } | |
340 | ||
341 | //clicking save | |
342 | $this->click("_qf_Field_next"); | |
343 | $this->waitForPageToLoad($this->getTimeoutMsec()); | |
344 | ||
345 | //Is custom field created? | |
76e86fd8 | 346 | $this->assertElementContainsText('crm-notification-container', "Your custom field '$fieldLabel' has been saved.", "Field was not created successfully"); |
6a488035 TO |
347 | |
348 | //get the custom id of the custom field that was just created | |
349 | $results = $this->webtest_civicrm_api("CustomField", "get", array('label' => $fieldLabel, 'custom_group_id' => $group_id)); | |
350 | //While there _technically_ could be two fields with the same name, its highly unlikely | |
351 | //so assert that exactly one result is return | |
352 | $this->assertTrue($results['count'] == 1, "Could not uniquely get custom field id"); | |
353 | return $results['id']; | |
354 | } | |
355 | ||
356 | //Populates $count options for a custom field on the add custom field page | |
357 | function _createFieldOptions($count = 3, $prefix = "option", $values = array( | |
358 | )) { | |
359 | // Only support up to 10 options on the creation form | |
360 | $count = $count > 10 ? 10 : $count; | |
361 | ||
362 | for ($i = 1; $i <= $count; $i++) { | |
363 | $label = $prefix . '_' . substr(sha1(rand()), 0, 6); | |
364 | $this->type("option_label_" . $i, $label); | |
365 | $this->type("option_value_" . $i, (isset($values[$i]) ? $values[$i] : $i)); | |
366 | } | |
367 | } | |
368 | ||
369 | //randomly generates data for a specific custom field | |
370 | function _fillCustomDataForContact($contact_id, $group_id) { | |
371 | //edit the given contact | |
76e86fd8 | 372 | $this->openCiviPage('contact/add', "reset=1&action=update&cid={$contact_id}"); |
6a488035 TO |
373 | |
374 | $this->click("expand"); | |
375 | $this->waitForElementPresent("address_1_street_address"); | |
376 | ||
377 | //get the custom fields for the group | |
378 | $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id)); | |
379 | $fields = $fields['values']; | |
380 | ||
381 | //we need the id the contact's record in the table for this custom group. | |
382 | //Recent (4.0.6+, i think?) versions of the api return this when getting | |
383 | //custom data for a contact. So we do that. | |
384 | $field_ids = array_keys($fields); | |
385 | $contact = $this->webtest_civicrm_api("Contact", "get", array('contact_id' => $contact_id, 'return.custom_' . $field_ids[0] => 1)); | |
386 | $group = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $group_id, 'return.table_name' => 1)); | |
387 | ||
388 | //if the contact has not been saved since this fieldset has been creative, | |
389 | //the form uses id = -1. In this case the table pk wont be in the api results | |
390 | $customValueId = $contact['values'][$contact_id][$group['values'][$group_id]['table_name'] . "_id"]; | |
391 | if (isset($customValueId) && !empty($customValueId)) { | |
392 | $table_pk = $customValueId; | |
393 | } | |
394 | else { | |
395 | $table_pk = -1; | |
396 | } | |
397 | ||
398 | //fill a value in for each field | |
399 | foreach ($fields as $field_id => $field) { | |
400 | //if there is an option group id, we grab the labels and select on randomly | |
401 | if (isset($field['option_group_id'])) { | |
402 | $options = $this->webtest_civicrm_api("OptionValue", "get", array('option_group_id' => $field['option_group_id'])); | |
403 | $options = $options['values']; | |
404 | $pick_me = $options[array_rand($options)]['label']; | |
405 | $this->click("xpath=//table//tr/td/label[text()=\"$pick_me\"]"); | |
406 | } | |
407 | else { | |
408 | //gonna go ahead and assume its an alphanumeric text question. This | |
409 | //will really only work if the custom data group has not yet been | |
410 | //filled out for this contact | |
411 | $this->type("custom_" . $field['id'] . '_' . $table_pk, sha1(rand())); | |
412 | } | |
413 | } | |
414 | ||
415 | //save the form | |
416 | $this->click("_qf_Contact_upload_view"); | |
417 | $this->waitForPageToLoad($this->getTimeoutMsec()); | |
418 | ||
419 | //assert success | |
8f737a72 | 420 | $this->assertElementContainsText('crm-notification-container', "has been updated", "Contact Record could not be saved"); |
6a488035 TO |
421 | } |
422 | } | |
423 |