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 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 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28 class WebTest_Admin_MoveCustomDataTest
extends CiviSeleniumTestCase
{
30 protected function setUp() {
34 function testCreateCustomFields() {
35 $this->webtestLogin();
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");
41 $from_group_id = $this->_buildCustomFieldSet("source");
42 $to_group_id = $this->_buildCustomFieldSet("destination");
44 $this->_fillCustomDataForContact($cid_all, $from_group_id);
45 $this->_fillCustomDataForContact($cid_to_missing, $from_group_id);
47 $this->_fillCustomDataForContact($cid_all, $to_group_id);
48 $this->_fillCustomDataForContact($cid_from_missing, $to_group_id);
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);
59 //ok, so after all that setup, we are now good to actually move a field
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']);
66 $this->_moveCustomField($field_to_move, $from_group_id, $to_group_id);
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);
77 //Make sure that only the appropriate values have changed
79 $cid_all, $cid_from_missing, $cid_to_missing) as $cid) {
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");
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!");
97 //Go to the contacts page and check that the custom field is in the right group
98 $this->openCiviPage('contact/view',"reset=1&cid={$cid_all}");
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];
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");
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
114 $this->openCiviPage('admin/custom/group/field/move', "reset=1&fid={$field_to_move}");
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 . "']");
121 //click the save button
122 $this->click("_qf_MoveField_next");
123 $this->waitForPageToLoad($this->getTimeoutMsec());
125 //assert that the success text is present
126 $this->waitForText('crm-notification-container', "has been moved");
128 //assert that the custom field not on old data set page
130 $this->assertTrue(!$this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field still displays on the old fieldset page");
132 //go to the destination fieldset and make sure the field is present
133 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$to_group_id}");
134 $this->assertTrue($this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field does not display on the new fieldset page");
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);
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();
154 $field_ids = array();
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;
166 $params = array('contact_id' => $contact_id);
167 foreach ($field_ids[$group_id] as $id) {
168 $params['return.custom_' . $id] = 1;
171 $contact = $this->webtest_civicrm_api("Contact", "get", $params);
173 //clean up the api results a bit....
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];
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);
195 //Creates a custom field group for a specific entity type and returns the custom group Id
196 function _createCustomGroup($prefix = "custom", $entity = "Contact") {
198 $this->openCiviPage('admin/custom/group', 'action=add&reset=1');
200 //fill custom group title
201 $customGroupTitle = $prefix . '_' . substr(sha1(rand()), 0, 7);
202 $this->click("title");
203 $this->type("title", $customGroupTitle);
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");
212 //Is custom group created?
213 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.");
215 $url = $this->parseURL();
216 $group_id = $url['queryString']['gid'];
217 $this->assertType('numeric', $group_id);
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
227 'alphanumeric' => array(
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'),
234 'widgets' => array('Text', 'Select', 'Radio'),
235 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
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),
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),
249 'widgets' => array('TextArea'),
253 'widgets' => array('Date'),
255 'yes or no' => array(
257 'widgets' => array('Radio'),
259 //'state/province' => array(
262 //'country' => array(
268 //'contact reference' => array(
271 //'file' => 9, hahaha im not doing files.
275 $type = strtolower($type);
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)));
281 $html_type_id = $type_map[$type]['id'];
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];
290 $this->fail("Cannot use $widget for $type fields. Available widgets are: " . implode(", ", $type_map[$type]['widgets']));
294 //Go to the add custom field page for the given group id
295 $this->openCiviPage('admin/custom/group/field/add', "action=add&reset=1&gid={$group_id}");
297 //Do common setup for all field types
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);
304 //enter pre help message
305 $this->type("help_pre", "this is field pre help for " . $fieldLabel);
307 //enter post help message
308 $this->type("help_post", "this field post help for " . $fieldLabel);
311 $this->click("is_searchable");
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 . "']");
321 //fill in specific elements for different widgets
324 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
325 $this->type("options_per_line", "2");
329 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
330 $this->type("options_per_line", "1");
334 $this->click("date_format");
335 $this->select("date_format", "value=yy-mm-dd");
336 $this->click("//option[@value='yy-mm-dd']");
338 //TODO allow for more things....
342 $this->click("_qf_Field_next");
343 $this->waitForPageToLoad($this->getTimeoutMsec());
345 //Is custom field created?
346 $this->waitForText('crm-notification-container', "Your custom field '$fieldLabel' has been saved.");
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'];
356 //Populates $count options for a custom field on the add custom field page
357 function _createFieldOptions($count = 3, $prefix = "option", $values = array(
359 // Only support up to 10 options on the creation form
360 $count = $count > 10 ?
10 : $count;
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));
369 //randomly generates data for a specific custom field
370 function _fillCustomDataForContact($contact_id, $group_id) {
371 //edit the given contact
372 $this->openCiviPage('contact/add', "reset=1&action=update&cid={$contact_id}");
374 $this->click("expand");
375 $this->waitForElementPresent("address_1_street_address");
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'];
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));
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;
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\"]");
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()));
416 $this->click("_qf_Contact_upload_view");
417 $this->waitForPageToLoad($this->getTimeoutMsec());
420 $this->waitForText('crm-notification-container', "has been updated");