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