3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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';
30 * Class WebTest_Admin_MoveCustomDataTest
32 class WebTest_Admin_MoveCustomDataTest
extends CiviSeleniumTestCase
{
34 protected function setUp() {
38 function testCreateCustomFields() {
39 $this->webtestLogin();
41 $cid_all = $this->_createContact("all_data", "move_custom_data");
42 $cid_from_missing = $this->_createContact("source_missing", "move_custom_data");
43 $cid_to_missing = $this->_createContact("destination_missing", "move_custom_data");
45 $from_group_id = $this->_buildCustomFieldSet("source");
46 $to_group_id = $this->_buildCustomFieldSet("destination");
48 $this->_fillCustomDataForContact($cid_all, $from_group_id);
49 $this->_fillCustomDataForContact($cid_to_missing, $from_group_id);
51 $this->_fillCustomDataForContact($cid_all, $to_group_id);
52 $this->_fillCustomDataForContact($cid_from_missing, $to_group_id);
54 //to verify data hasn't been lost, we load the values for each contact
55 $pre_move_values = array();
56 $pre_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id);
57 $pre_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id);
58 $pre_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id);
59 $pre_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id);
60 $pre_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id);
61 $pre_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id);
63 //ok, so after all that setup, we are now good to actually move a field
65 //first, pick a random field from the source group to move
66 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $from_group_id));
67 $field_to_move = array_rand($fields['values']);
70 $this->_moveCustomField($field_to_move, $from_group_id, $to_group_id);
72 //now lets verify the data, load up the new values from the api...
73 $post_move_values = array();
74 $post_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id, TRUE);
75 $post_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id);
76 $post_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id);
77 $post_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id);
78 $post_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id);
79 $post_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id);
81 //Make sure that only the appropriate values have changed
83 $cid_all, $cid_from_missing, $cid_to_missing) as $cid) {
85 'source', 'destination') as $fieldset) {
86 foreach ($pre_move_values[$cid][$fieldset] as $id => $value) {
87 if ($id != $field_to_move) {
88 //All fields that were there should still be there
89 $this->assertTrue(isset($post_move_values[$cid][$fieldset][$id]), "A custom field that was not moved is missing!");
90 //All fields should have the same value as when we started
91 $this->assertTrue($post_move_values[$cid][$fieldset][$id] == $value, "A custom field value has changed in the source custom field set");
95 //check that the field is actually moved
96 $this->assertTrue(!isset($post_move_values[$cid]['source'][$field_to_move]), "Moved field is still present in the source fieldset");
97 $this->assertTrue(isset($post_move_values[$cid]['destination'][$field_to_move]), "Moved field is not present in the destination fieldset");
98 $this->assertTrue($pre_move_values[$cid]['source'][$field_to_move] == $post_move_values[$cid]['destination'][$field_to_move], "The moved field has changed values!");
101 //Go to the contacts page and check that the custom field is in the right group
102 $this->openCiviPage('contact/view',"reset=1&cid={$cid_all}");
104 //load the names of the custom fieldsets
105 $source = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $from_group_id));
106 $source = $source['values'][$from_group_id];
107 $destination = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $to_group_id));
108 $destination = $destination['values'][$to_group_id];
110 //assert that the moved custom field is missing from the source fieldset
111 $this->assertElementNotContainsText("css=div." . $source['name'], $fields['values'][$field_to_move]['label'], "Moved value still displays in the old fieldset on the contact record");
112 $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");
115 //moves a field from one field to another
117 * @param $field_to_move
118 * @param $from_group_id
119 * @param $to_group_id
121 function _moveCustomField($field_to_move, $from_group_id, $to_group_id) {
122 //go to the move field page
123 $this->openCiviPage('admin/custom/group/field/move', "reset=1&fid={$field_to_move}");
125 //select the destination field set from select box
126 $this->click("dst_group_id");
127 $this->select("dst_group_id", "value=" . $to_group_id);
128 $this->click("//option[@value='" . $to_group_id . "']");
130 //click the save button
131 $this->click("_qf_MoveField_next");
132 $this->waitForPageToLoad($this->getTimeoutMsec());
134 //assert that the success text is present
135 $this->waitForText('crm-notification-container', "has been moved");
137 //assert that the custom field not on old data set page
139 $this->assertTrue(!$this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field still displays on the old fieldset page");
141 //go to the destination fieldset and make sure the field is present
142 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$to_group_id}");
143 $this->assertTrue($this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field does not display on the new fieldset page");
146 //create a contact and return the contact id
148 * @param string $firstName
149 * @param string $lastName
153 function _createContact($firstName = "John", $lastName = "Doe") {
154 $firstName .= "_" . substr(sha1(rand()), 0, 5);
155 $lastName .= "_" . substr(sha1(rand()), 0, 5);
156 $this->webtestAddContact($firstName, $lastName);
157 $url = $this->parseURL();
158 $cid = $url['queryString']['cid'];
159 $this->assertType('numeric', $cid);
163 //Get all custom field values for a given contact and custom group id using the api
167 * @param bool $reset_cache
171 function _loadDataFromApi($contact_id, $group_id, $reset_cache = FALSE) {
172 // cache the fields, just to speed things up a little
173 static $field_ids = array();
176 $field_ids = array();
179 //if the field ids havent been cached yet, grab them
180 if (!isset($field_ids[$group_id])) {
181 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id));
182 $field_ids[$group_id] = array();
183 foreach ($fields['values'] as $id => $field) {
184 $field_ids[$group_id][] = $id;
188 $params = array('contact_id' => $contact_id);
189 foreach ($field_ids[$group_id] as $id) {
190 $params['return.custom_' . $id] = 1;
193 $contact = $this->webtest_civicrm_api("Contact", "get", $params);
195 //clean up the api results a bit....
197 foreach ($field_ids[$group_id] as $id) {
198 if (isset($contact['values'][$contact_id]['custom_' . $id])) {
199 $results[$id] = $contact['values'][$contact_id]['custom_' . $id];
206 //creates a new custom group and fields in that group, and returns the group Id
212 function _buildCustomFieldset($prefix) {
213 $group_id = $this->_createCustomGroup($prefix);
214 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "CheckBox", $prefix);
215 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Radio", $prefix);
216 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Text", $prefix);
217 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Note', "Text", $prefix);
218 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Date', "Date", $prefix);
222 //Creates a custom field group for a specific entity type and returns the custom group Id
224 * @param string $prefix
225 * @param string $entity
229 function _createCustomGroup($prefix = "custom", $entity = "Contact") {
231 $this->openCiviPage('admin/custom/group', 'action=add&reset=1');
233 //fill custom group title
234 $customGroupTitle = $prefix . '_' . substr(sha1(rand()), 0, 7);
235 $this->click("title");
236 $this->type("title", $customGroupTitle);
238 //custom group extends
239 $this->click("extends[0]");
240 $this->select("extends[0]", "value=" . $entity);
241 $this->click("//option[@value='" . $entity . "']");
242 $this->click("_qf_Group_next-bottom");
243 $this->waitForElementPresent("_qf_Field_cancel-bottom");
245 //Is custom group created?
246 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.");
248 return $this->urlArg('gid');
251 //Adds a new custom field to a specfied custom field group, using the given
252 //datatype and widget.
255 * @param string $type
256 * @param string $widget
257 * @param string $prefix
260 * @throws PHPUnit_Framework_AssertionFailedError
262 function _addCustomFieldToGroup($group_id, $type = 'Alphanumeric', $widget = 'CheckBox', $prefix = '') {
263 //A mapping of data type names to integer keys
265 'alphanumeric' => array(
267 'widgets' => array('Text', 'Select', 'Radio', 'CheckBox', 'Multi-Select'),
268 'options' => array('option_01', 'option_02', 'option_03', 'option_04', 'option_05', 'option_06', 'option_07', 'option_08', 'option_09', 'option_10'),
272 'widgets' => array('Text', 'Select', 'Radio'),
273 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
277 'widgets' => array('Text', 'Select', 'Radio'),
278 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1),
282 'widgets' => array('Text', 'Select', 'Radio'),
283 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1),
287 'widgets' => array('TextArea'),
291 'widgets' => array('Date'),
293 'yes or no' => array(
295 'widgets' => array('Radio'),
297 //'state/province' => array(
300 //'country' => array(
306 //'contact reference' => array(
309 //'file' => 9, hahaha im not doing files.
313 $type = strtolower($type);
315 //make sure that a supported html type was entered
316 if (!isset($type_map[$type])) {
317 $this->fail("The custom field html type $type is not supported. Supported types are: " . implode(", ", array_keys($type_map)));
319 $html_type_id = $type_map[$type]['id'];
321 //make sure the widget type can be used for this data type
322 //if an invalid widget is selected and only 1 widget is available, use that
323 if (!in_array($widget, $type_map[$type]['widgets'])) {
324 if (count($type_map[$type]['widgets']) == 1) {
325 $widget = $type_map[$type]['widgets'][0];
328 $this->fail("Cannot use $widget for $type fields. Available widgets are: " . implode(", ", $type_map[$type]['widgets']));
332 //Go to the add custom field page for the given group id
333 $this->openCiviPage('admin/custom/group/field/add', "action=add&reset=1&gid={$group_id}");
335 //Do common setup for all field types
337 //set the field label
338 $fieldLabel = (isset($prefix) ?
$prefix . "_" : "") . $widget . "_" . substr(sha1(rand()), 0, 6);
339 $this->click("label");
340 $this->type("label", $fieldLabel);
342 //enter pre help message
343 $this->type("help_pre", "this is field pre help for " . $fieldLabel);
345 //enter post help message
346 $this->type("help_post", "this field post help for " . $fieldLabel);
349 $this->click("is_searchable");
351 //Fill in the html type and widget type
352 $this->click("data_type[0]");
353 $this->select("data_type[0]", "value=" . $html_type_id);
354 $this->click("//option[@value='" . $html_type_id . "']");
355 $this->click("data_type[1]");
356 $this->select("data_type[1]", "value=" . $widget);
357 $this->click("//option[@value='" . $widget . "']");
359 //fill in specific elements for different widgets
362 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
363 $this->type("options_per_line", "2");
367 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
368 $this->type("options_per_line", "1");
372 $this->click("date_format");
373 $this->select("date_format", "value=yy-mm-dd");
374 $this->click("//option[@value='yy-mm-dd']");
376 //TODO allow for more things....
380 $this->click("_qf_Field_next");
381 $this->waitForPageToLoad($this->getTimeoutMsec());
383 //Is custom field created?
384 $this->waitForText('crm-notification-container', "Custom field '$fieldLabel' has been saved.");
386 //get the custom id of the custom field that was just created
387 $results = $this->webtest_civicrm_api("CustomField", "get", array('label' => $fieldLabel, 'custom_group_id' => $group_id));
388 //While there _technically_ could be two fields with the same name, its highly unlikely
389 //so assert that exactly one result is return
390 $this->assertTrue($results['count'] == 1, "Could not uniquely get custom field id");
391 return $results['id'];
394 //Populates $count options for a custom field on the add custom field page
397 * @param string $prefix
398 * @param array $values
400 function _createFieldOptions($count = 3, $prefix = "option", $values = array(
402 // Only support up to 10 options on the creation form
403 $count = $count > 10 ?
10 : $count;
405 for ($i = 1; $i <= $count; $i++
) {
406 $label = $prefix . '_' . substr(sha1(rand()), 0, 6);
407 $this->type("option_label_" . $i, $label);
408 $this->type("option_value_" . $i, (isset($values[$i]) ?
$values[$i] : $i));
412 //randomly generates data for a specific custom field
417 function _fillCustomDataForContact($contact_id, $group_id) {
418 //edit the given contact
419 $this->openCiviPage('contact/add', "reset=1&action=update&cid={$contact_id}");
421 $this->click("expand");
422 $this->waitForElementPresent("address_1_street_address");
424 //get the custom fields for the group
425 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id));
426 $fields = $fields['values'];
428 //we need the id the contact's record in the table for this custom group.
429 //Recent (4.0.6+, i think?) versions of the api return this when getting
430 //custom data for a contact. So we do that.
431 $field_ids = array_keys($fields);
432 $contact = $this->webtest_civicrm_api("Contact", "get", array('contact_id' => $contact_id, 'return.custom_' . $field_ids[0] => 1));
433 $group = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $group_id, 'return.table_name' => 1));
435 //if the contact has not been saved since this fieldset has been creative,
436 //the form uses id = -1. In this case the table pk wont be in the api results
437 $customValueId = $contact['values'][$contact_id][$group['values'][$group_id]['table_name'] . "_id"];
438 if (isset($customValueId) && !empty($customValueId)) {
439 $table_pk = $customValueId;
445 //fill a value in for each field
446 foreach ($fields as $field_id => $field) {
447 //if there is an option group id, we grab the labels and select on randomly
448 if (isset($field['option_group_id'])) {
449 $options = $this->webtest_civicrm_api("OptionValue", "get", array('option_group_id' => $field['option_group_id']));
450 $options = $options['values'];
451 $pick_me = $options[array_rand($options)]['label'];
452 $this->click("xpath=//table//tr/td/label[text()=\"$pick_me\"]");
455 //gonna go ahead and assume its an alphanumeric text question. This
456 //will really only work if the custom data group has not yet been
457 //filled out for this contact
458 $this->type("custom_" . $field['id'] . '_' . $table_pk, sha1(rand()));
463 $this->click("_qf_Contact_upload_view");
464 $this->waitForPageToLoad($this->getTimeoutMsec());
467 $this->waitForText('crm-notification-container', "has been updated");