Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / tests / phpunit / WebTest / Admin / MoveCustomDataTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 27require_once 'CiviTest/CiviSeleniumTestCase.php';
e9479dcf
EM
28
29/**
30 * Class WebTest_Admin_MoveCustomDataTest
31 */
6a488035
TO
32class WebTest_Admin_MoveCustomDataTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testCreateCustomFields() {
6a488035
TO
39 $this->webtestLogin();
40
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");
44
45 $from_group_id = $this->_buildCustomFieldSet("source");
b3748c57 46 CRM_Utils_System::flushCache();
6a488035
TO
47 $to_group_id = $this->_buildCustomFieldSet("destination");
48
49 $this->_fillCustomDataForContact($cid_all, $from_group_id);
50 $this->_fillCustomDataForContact($cid_to_missing, $from_group_id);
51
52 $this->_fillCustomDataForContact($cid_all, $to_group_id);
53 $this->_fillCustomDataForContact($cid_from_missing, $to_group_id);
54
55 //to verify data hasn't been lost, we load the values for each contact
56 $pre_move_values = array();
57 $pre_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id);
58 $pre_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id);
59 $pre_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id);
60 $pre_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id);
61 $pre_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id);
62 $pre_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id);
63
6a488035
TO
64 //ok, so after all that setup, we are now good to actually move a field
65
66 //first, pick a random field from the source group to move
67 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $from_group_id));
68 $field_to_move = array_rand($fields['values']);
69
70 //move the field
71 $this->_moveCustomField($field_to_move, $from_group_id, $to_group_id);
72
73 //now lets verify the data, load up the new values from the api...
74 $post_move_values = array();
75 $post_move_values[$cid_all]['source'] = $this->_loadDataFromApi($cid_all, $from_group_id, TRUE);
76 $post_move_values[$cid_all]['destination'] = $this->_loadDataFromApi($cid_all, $to_group_id);
77 $post_move_values[$cid_from_missing]['source'] = $this->_loadDataFromApi($cid_from_missing, $from_group_id);
78 $post_move_values[$cid_from_missing]['destination'] = $this->_loadDataFromApi($cid_from_missing, $to_group_id);
79 $post_move_values[$cid_to_missing]['source'] = $this->_loadDataFromApi($cid_to_missing, $from_group_id);
80 $post_move_values[$cid_to_missing]['destination'] = $this->_loadDataFromApi($cid_to_missing, $to_group_id);
81
6a488035
TO
82 //Make sure that only the appropriate values have changed
83 foreach (array(
84 $cid_all, $cid_from_missing, $cid_to_missing) as $cid) {
85 foreach (array(
86 'source', 'destination') as $fieldset) {
87 foreach ($pre_move_values[$cid][$fieldset] as $id => $value) {
88 if ($id != $field_to_move) {
89 //All fields that were there should still be there
90 $this->assertTrue(isset($post_move_values[$cid][$fieldset][$id]), "A custom field that was not moved is missing!");
91 //All fields should have the same value as when we started
92 $this->assertTrue($post_move_values[$cid][$fieldset][$id] == $value, "A custom field value has changed in the source custom field set");
93 }
94 }
95 }
96 //check that the field is actually moved
97 $this->assertTrue(!isset($post_move_values[$cid]['source'][$field_to_move]), "Moved field is still present in the source fieldset");
98 $this->assertTrue(isset($post_move_values[$cid]['destination'][$field_to_move]), "Moved field is not present in the destination fieldset");
99 $this->assertTrue($pre_move_values[$cid]['source'][$field_to_move] == $post_move_values[$cid]['destination'][$field_to_move], "The moved field has changed values!");
100 }
101
102 //Go to the contacts page and check that the custom field is in the right group
8f737a72 103 $this->openCiviPage('contact/view',"reset=1&cid={$cid_all}");
6a488035
TO
104
105 //load the names of the custom fieldsets
106 $source = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $from_group_id));
107 $source = $source['values'][$from_group_id];
108 $destination = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $to_group_id));
109 $destination = $destination['values'][$to_group_id];
110
111 //assert that the moved custom field is missing from the source fieldset
112 $this->assertElementNotContainsText("css=div." . $source['name'], $fields['values'][$field_to_move]['label'], "Moved value still displays in the old fieldset on the contact record");
113 $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");
114 }
115
116 //moves a field from one field to another
4cbe18b8
EM
117 /**
118 * @param $field_to_move
100fef9d
CW
119 * @param int $from_group_id
120 * @param int $to_group_id
4cbe18b8 121 */
6a488035
TO
122 function _moveCustomField($field_to_move, $from_group_id, $to_group_id) {
123 //go to the move field page
8f737a72 124 $this->openCiviPage('admin/custom/group/field/move', "reset=1&fid={$field_to_move}");
6a488035
TO
125
126 //select the destination field set from select box
127 $this->click("dst_group_id");
128 $this->select("dst_group_id", "value=" . $to_group_id);
129 $this->click("//option[@value='" . $to_group_id . "']");
130
131 //click the save button
132 $this->click("_qf_MoveField_next");
6a488035 133
8f737a72 134 //assert that the success text is present
6c5f7368 135 $this->waitForText('crm-notification-container', "has been moved");
6a488035 136
76e86fd8
CW
137 //assert that the custom field not on old data set page
138
6a488035
TO
139 $this->assertTrue(!$this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field still displays on the old fieldset page");
140
141 //go to the destination fieldset and make sure the field is present
8f737a72 142 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$to_group_id}");
6a488035
TO
143 $this->assertTrue($this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field does not display on the new fieldset page");
144 }
145
146 //create a contact and return the contact id
4cbe18b8
EM
147 /**
148 * @param string $firstName
149 * @param string $lastName
150 *
151 * @return mixed
152 */
6a488035
TO
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);
160 return $cid;
161 }
162
163 //Get all custom field values for a given contact and custom group id using the api
4cbe18b8 164 /**
100fef9d
CW
165 * @param int $contact_id
166 * @param int $group_id
4cbe18b8
EM
167 * @param bool $reset_cache
168 *
169 * @return array
170 */
6a488035
TO
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();
174
175 if ($reset_cache) {
176 $field_ids = array();
177 }
178
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;
185 }
186 }
187
6a488035
TO
188 $params = array('contact_id' => $contact_id);
189 foreach ($field_ids[$group_id] as $id) {
190 $params['return.custom_' . $id] = 1;
191 }
192
193 $contact = $this->webtest_civicrm_api("Contact", "get", $params);
194
195 //clean up the api results a bit....
196 $results = array();
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];
200 }
201 }
202
203 return $results;
204 }
205
6a488035 206 //creates a new custom group and fields in that group, and returns the group Id
4cbe18b8
EM
207 /**
208 * @param $prefix
209 *
210 * @return null
211 */
6a488035
TO
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);
219 return $group_id;
220 }
221
222 //Creates a custom field group for a specific entity type and returns the custom group Id
4cbe18b8
EM
223 /**
224 * @param string $prefix
225 * @param string $entity
226 *
227 * @return null
228 */
6a488035 229 function _createCustomGroup($prefix = "custom", $entity = "Contact") {
42daf119 230
8f737a72 231 $this->openCiviPage('admin/custom/group', 'action=add&reset=1');
6a488035
TO
232
233 //fill custom group title
234 $customGroupTitle = $prefix . '_' . substr(sha1(rand()), 0, 7);
235 $this->click("title");
236 $this->type("title", $customGroupTitle);
237
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");
244
245 //Is custom group created?
6c5f7368 246 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.");
6a488035 247
a471a3b6 248 return $this->urlArg('gid');
6a488035
TO
249 }
250
251 //Adds a new custom field to a specfied custom field group, using the given
252 //datatype and widget.
4cbe18b8 253 /**
100fef9d 254 * @param int $group_id
4cbe18b8
EM
255 * @param string $type
256 * @param string $widget
257 * @param string $prefix
258 *
259 * @return mixed
260 * @throws PHPUnit_Framework_AssertionFailedError
261 */
6a488035
TO
262 function _addCustomFieldToGroup($group_id, $type = 'Alphanumeric', $widget = 'CheckBox', $prefix = '') {
263 //A mapping of data type names to integer keys
264 $type_map = array(
265 'alphanumeric' => array(
266 'id' => 0,
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'),
269 ),
270 'integer' => array(
271 'id' => 1,
272 'widgets' => array('Text', 'Select', 'Radio'),
273 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
274 ),
275 'number' => array(
276 'id' => 2,
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),
279 ),
280 'money' => array(
281 'id' => 3,
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),
284 ),
285 'note' => array(
286 'id' => 4,
287 'widgets' => array('TextArea'),
288 ),
289 'date' => array(
290 'id' => 5,
291 'widgets' => array('Date'),
292 ),
293 'yes or no' => array(
294 'id' => 6,
295 'widgets' => array('Radio'),
296 ),
297 //'state/province' => array(
298 // 'id' => 7,
299 //),
300 //'country' => array(
301 // 'id' => 8,
302 //),
303 //'link' => array(
304 // 'id' => 10,
305 //),
306 //'contact reference' => array(
307 // 'id' => 11,
308 //),
309 //'file' => 9, hahaha im not doing files.
310 );
311
312 //downcase the type
313 $type = strtolower($type);
314
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)));
318 }
319 $html_type_id = $type_map[$type]['id'];
320
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];
326 }
327 else {
328 $this->fail("Cannot use $widget for $type fields. Available widgets are: " . implode(", ", $type_map[$type]['widgets']));
329 }
330 }
331
332 //Go to the add custom field page for the given group id
8f737a72 333 $this->openCiviPage('admin/custom/group/field/add', "action=add&reset=1&gid={$group_id}");
6a488035
TO
334
335 //Do common setup for all field types
336
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);
341
342 //enter pre help message
343 $this->type("help_pre", "this is field pre help for " . $fieldLabel);
344
345 //enter post help message
346 $this->type("help_post", "this field post help for " . $fieldLabel);
347
348 //Is searchable?
349 $this->click("is_searchable");
350
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 . "']");
358
359 //fill in specific elements for different widgets
360 switch ($widget) {
361 case 'CheckBox':
362 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
363 $this->type("options_per_line", "2");
364 break;
365
366 case 'Radio':
367 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
368 $this->type("options_per_line", "1");
369 break;
370
371 case 'Date':
372 $this->click("date_format");
373 $this->select("date_format", "value=yy-mm-dd");
374 $this->click("//option[@value='yy-mm-dd']");
375 break;
376 //TODO allow for more things....
377 }
378
379 //clicking save
280dc0fe 380 $this->click("_qf_Field_done-bottom");
6a488035
TO
381 $this->waitForPageToLoad($this->getTimeoutMsec());
382
383 //Is custom field created?
44c45561 384 $this->waitForText('crm-notification-container', "Custom field '$fieldLabel' has been saved.");
6a488035
TO
385
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'];
392 }
393
394 //Populates $count options for a custom field on the add custom field page
4cbe18b8
EM
395 /**
396 * @param int $count
397 * @param string $prefix
398 * @param array $values
399 */
6a488035
TO
400 function _createFieldOptions($count = 3, $prefix = "option", $values = array(
401 )) {
402 // Only support up to 10 options on the creation form
403 $count = $count > 10 ? 10 : $count;
404
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));
409 }
410 }
411
412 //randomly generates data for a specific custom field
4cbe18b8 413 /**
100fef9d
CW
414 * @param int $contact_id
415 * @param int $group_id
4cbe18b8 416 */
6a488035
TO
417 function _fillCustomDataForContact($contact_id, $group_id) {
418 //edit the given contact
76e86fd8 419 $this->openCiviPage('contact/add', "reset=1&action=update&cid={$contact_id}");
6a488035
TO
420
421 $this->click("expand");
422 $this->waitForElementPresent("address_1_street_address");
423
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'];
427
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));
434
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;
440 }
441 else {
442 $table_pk = -1;
443 }
444
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
280dc0fe 448 if ($field['data_type'] == 'Date') {
449 $this->webtestFillDate("custom_" . $field['id'] . "_" . $table_pk, "+1 week");
450 }
451 elseif (isset($field['option_group_id'])) {
6a488035
TO
452 $options = $this->webtest_civicrm_api("OptionValue", "get", array('option_group_id' => $field['option_group_id']));
453 $options = $options['values'];
454 $pick_me = $options[array_rand($options)]['label'];
455 $this->click("xpath=//table//tr/td/label[text()=\"$pick_me\"]");
456 }
457 else {
458 //gonna go ahead and assume its an alphanumeric text question. This
459 //will really only work if the custom data group has not yet been
460 //filled out for this contact
461 $this->type("custom_" . $field['id'] . '_' . $table_pk, sha1(rand()));
462 }
463 }
464
465 //save the form
466 $this->click("_qf_Contact_upload_view");
467 $this->waitForPageToLoad($this->getTimeoutMsec());
468
469 //assert success
6c5f7368 470 $this->waitForText('crm-notification-container', "has been updated");
6a488035
TO
471 }
472}
473