Merge pull request #6340 from jitendrapurohit/CRM-16752
[civicrm-core.git] / tests / phpunit / WebTest / Admin / MoveCustomDataTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 25 */
6a488035 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
00be9182 38 public function testCreateCustomFields() {
6a488035
TO
39 $this->webtestLogin();
40
92915c55 41 $cid_all = $this->_createContact("all_data", "move_custom_data");
6a488035 42 $cid_from_missing = $this->_createContact("source_missing", "move_custom_data");
92915c55 43 $cid_to_missing = $this->_createContact("destination_missing", "move_custom_data");
6a488035
TO
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
8d7a9d07 82 // Make sure that only the appropriate values have changed.
6a488035 83 foreach (array(
92915c55
TO
84 $cid_all,
85 $cid_from_missing,
8d7a9d07 86 $cid_to_missing,
92915c55 87 ) as $cid) {
6a488035 88 foreach (array(
92915c55 89 'source',
8d7a9d07 90 'destination',
92915c55 91 ) as $fieldset) {
6a488035
TO
92 foreach ($pre_move_values[$cid][$fieldset] as $id => $value) {
93 if ($id != $field_to_move) {
94 //All fields that were there should still be there
95 $this->assertTrue(isset($post_move_values[$cid][$fieldset][$id]), "A custom field that was not moved is missing!");
96 //All fields should have the same value as when we started
97 $this->assertTrue($post_move_values[$cid][$fieldset][$id] == $value, "A custom field value has changed in the source custom field set");
98 }
99 }
100 }
101 //check that the field is actually moved
102 $this->assertTrue(!isset($post_move_values[$cid]['source'][$field_to_move]), "Moved field is still present in the source fieldset");
103 $this->assertTrue(isset($post_move_values[$cid]['destination'][$field_to_move]), "Moved field is not present in the destination fieldset");
104 $this->assertTrue($pre_move_values[$cid]['source'][$field_to_move] == $post_move_values[$cid]['destination'][$field_to_move], "The moved field has changed values!");
105 }
106
107 //Go to the contacts page and check that the custom field is in the right group
6c6e6187 108 $this->openCiviPage('contact/view', "reset=1&cid={$cid_all}");
6a488035
TO
109
110 //load the names of the custom fieldsets
92915c55
TO
111 $source = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $from_group_id));
112 $source = $source['values'][$from_group_id];
6a488035
TO
113 $destination = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $to_group_id));
114 $destination = $destination['values'][$to_group_id];
115
116 //assert that the moved custom field is missing from the source fieldset
117 $this->assertElementNotContainsText("css=div." . $source['name'], $fields['values'][$field_to_move]['label'], "Moved value still displays in the old fieldset on the contact record");
118 $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");
119 }
120
4cbe18b8 121 /**
eceb18cc 122 * moves a field from one field to another.
4cbe18b8 123 * @param $field_to_move
100fef9d
CW
124 * @param int $from_group_id
125 * @param int $to_group_id
4cbe18b8 126 */
00be9182 127 public function _moveCustomField($field_to_move, $from_group_id, $to_group_id) {
6a488035 128 //go to the move field page
8f737a72 129 $this->openCiviPage('admin/custom/group/field/move', "reset=1&fid={$field_to_move}");
6a488035
TO
130
131 //select the destination field set from select box
132 $this->click("dst_group_id");
133 $this->select("dst_group_id", "value=" . $to_group_id);
134 $this->click("//option[@value='" . $to_group_id . "']");
135
136 //click the save button
137 $this->click("_qf_MoveField_next");
6a488035 138
8f737a72 139 //assert that the success text is present
6c5f7368 140 $this->waitForText('crm-notification-container', "has been moved");
6a488035 141
76e86fd8
CW
142 //assert that the custom field not on old data set page
143
6a488035
TO
144 $this->assertTrue(!$this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field still displays on the old fieldset page");
145
146 //go to the destination fieldset and make sure the field is present
8f737a72 147 $this->openCiviPage('admin/custom/group/field', "reset=1&action=browse&gid={$to_group_id}");
6a488035
TO
148 $this->assertTrue($this->isElementPresent("CustomField-" . $field_to_move), "The moved custom field does not display on the new fieldset page");
149 }
150
4cbe18b8 151 /**
eceb18cc 152 * create a contact and return the contact id.
4cbe18b8
EM
153 * @param string $firstName
154 * @param string $lastName
155 *
156 * @return mixed
157 */
00be9182 158 public function _createContact($firstName = "John", $lastName = "Doe") {
6a488035
TO
159 $firstName .= "_" . substr(sha1(rand()), 0, 5);
160 $lastName .= "_" . substr(sha1(rand()), 0, 5);
161 $this->webtestAddContact($firstName, $lastName);
162 $url = $this->parseURL();
163 $cid = $url['queryString']['cid'];
164 $this->assertType('numeric', $cid);
165 return $cid;
166 }
167
4cbe18b8 168 /**
eceb18cc 169 * Get all custom field values for a given contact and custom group id using the api.
100fef9d
CW
170 * @param int $contact_id
171 * @param int $group_id
4cbe18b8
EM
172 * @param bool $reset_cache
173 *
174 * @return array
175 */
00be9182 176 public function _loadDataFromApi($contact_id, $group_id, $reset_cache = FALSE) {
6a488035
TO
177 // cache the fields, just to speed things up a little
178 static $field_ids = array();
179
180 if ($reset_cache) {
181 $field_ids = array();
182 }
183
184 //if the field ids havent been cached yet, grab them
185 if (!isset($field_ids[$group_id])) {
186 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id));
187 $field_ids[$group_id] = array();
188 foreach ($fields['values'] as $id => $field) {
189 $field_ids[$group_id][] = $id;
190 }
191 }
192
6a488035
TO
193 $params = array('contact_id' => $contact_id);
194 foreach ($field_ids[$group_id] as $id) {
195 $params['return.custom_' . $id] = 1;
196 }
197
198 $contact = $this->webtest_civicrm_api("Contact", "get", $params);
199
200 //clean up the api results a bit....
201 $results = array();
202 foreach ($field_ids[$group_id] as $id) {
203 if (isset($contact['values'][$contact_id]['custom_' . $id])) {
204 $results[$id] = $contact['values'][$contact_id]['custom_' . $id];
205 }
206 }
207
208 return $results;
209 }
210
4cbe18b8 211 /**
4f1f1f2a 212 * creates a new custom group and fields in that group, and returns the group Id
4cbe18b8
EM
213 * @param $prefix
214 *
215 * @return null
216 */
00be9182 217 public function _buildCustomFieldset($prefix) {
92915c55 218 $group_id = $this->_createCustomGroup($prefix);
6a488035
TO
219 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "CheckBox", $prefix);
220 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Radio", $prefix);
221 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Alphanumeric', "Text", $prefix);
222 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Note', "Text", $prefix);
223 $field_ids[] = $this->_addCustomFieldToGroup($group_id, 'Date', "Date", $prefix);
224 return $group_id;
225 }
226
4cbe18b8 227 /**
eceb18cc 228 * Creates a custom field group for a specific entity type and returns the custom group Id.
4cbe18b8
EM
229 * @param string $prefix
230 * @param string $entity
231 *
232 * @return null
233 */
00be9182 234 public function _createCustomGroup($prefix = "custom", $entity = "Contact") {
42daf119 235
8f737a72 236 $this->openCiviPage('admin/custom/group', 'action=add&reset=1');
6a488035
TO
237
238 //fill custom group title
239 $customGroupTitle = $prefix . '_' . substr(sha1(rand()), 0, 7);
240 $this->click("title");
241 $this->type("title", $customGroupTitle);
242
243 //custom group extends
244 $this->click("extends[0]");
245 $this->select("extends[0]", "value=" . $entity);
246 $this->click("//option[@value='" . $entity . "']");
247 $this->click("_qf_Group_next-bottom");
248 $this->waitForElementPresent("_qf_Field_cancel-bottom");
249
250 //Is custom group created?
6c5f7368 251 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added. You can add custom fields now.");
6a488035 252
a471a3b6 253 return $this->urlArg('gid');
6a488035
TO
254 }
255
4cbe18b8 256 /**
4f1f1f2a
CW
257 * Adds a new custom field to a specfied custom field group, using the given
258 * datatype and widget.
100fef9d 259 * @param int $group_id
4cbe18b8
EM
260 * @param string $type
261 * @param string $widget
262 * @param string $prefix
263 *
264 * @return mixed
265 * @throws PHPUnit_Framework_AssertionFailedError
266 */
00be9182 267 public function _addCustomFieldToGroup($group_id, $type = 'Alphanumeric', $widget = 'CheckBox', $prefix = '') {
6a488035
TO
268 //A mapping of data type names to integer keys
269 $type_map = array(
270 'alphanumeric' => array(
271 'id' => 0,
272 'widgets' => array('Text', 'Select', 'Radio', 'CheckBox', 'Multi-Select'),
92915c55
TO
273 'options' => array(
274 'option_01',
275 'option_02',
276 'option_03',
277 'option_04',
278 'option_05',
279 'option_06',
280 'option_07',
281 'option_08',
282 'option_09',
8d7a9d07 283 'option_10',
92915c55 284 ),
6a488035
TO
285 ),
286 'integer' => array(
287 'id' => 1,
288 'widgets' => array('Text', 'Select', 'Radio'),
289 'options' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
290 ),
291 'number' => array(
292 'id' => 2,
293 'widgets' => array('Text', 'Select', 'Radio'),
294 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1),
295 ),
296 'money' => array(
297 'id' => 3,
298 'widgets' => array('Text', 'Select', 'Radio'),
299 'options' => array(1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08 . 9.09, 10.1),
300 ),
301 'note' => array(
302 'id' => 4,
303 'widgets' => array('TextArea'),
304 ),
305 'date' => array(
306 'id' => 5,
307 'widgets' => array('Date'),
308 ),
309 'yes or no' => array(
310 'id' => 6,
311 'widgets' => array('Radio'),
312 ),
313 //'state/province' => array(
314 // 'id' => 7,
315 //),
316 //'country' => array(
317 // 'id' => 8,
318 //),
319 //'link' => array(
320 // 'id' => 10,
321 //),
322 //'contact reference' => array(
323 // 'id' => 11,
324 //),
325 //'file' => 9, hahaha im not doing files.
326 );
327
328 //downcase the type
329 $type = strtolower($type);
330
331 //make sure that a supported html type was entered
332 if (!isset($type_map[$type])) {
333 $this->fail("The custom field html type $type is not supported. Supported types are: " . implode(", ", array_keys($type_map)));
334 }
335 $html_type_id = $type_map[$type]['id'];
336
337 //make sure the widget type can be used for this data type
338 //if an invalid widget is selected and only 1 widget is available, use that
339 if (!in_array($widget, $type_map[$type]['widgets'])) {
340 if (count($type_map[$type]['widgets']) == 1) {
341 $widget = $type_map[$type]['widgets'][0];
342 }
343 else {
344 $this->fail("Cannot use $widget for $type fields. Available widgets are: " . implode(", ", $type_map[$type]['widgets']));
345 }
346 }
347
348 //Go to the add custom field page for the given group id
8f737a72 349 $this->openCiviPage('admin/custom/group/field/add', "action=add&reset=1&gid={$group_id}");
6a488035
TO
350
351 //Do common setup for all field types
352
353 //set the field label
354 $fieldLabel = (isset($prefix) ? $prefix . "_" : "") . $widget . "_" . substr(sha1(rand()), 0, 6);
355 $this->click("label");
356 $this->type("label", $fieldLabel);
357
358 //enter pre help message
359 $this->type("help_pre", "this is field pre help for " . $fieldLabel);
360
361 //enter post help message
362 $this->type("help_post", "this field post help for " . $fieldLabel);
363
364 //Is searchable?
365 $this->click("is_searchable");
366
367 //Fill in the html type and widget type
368 $this->click("data_type[0]");
369 $this->select("data_type[0]", "value=" . $html_type_id);
370 $this->click("//option[@value='" . $html_type_id . "']");
371 $this->click("data_type[1]");
372 $this->select("data_type[1]", "value=" . $widget);
373 $this->click("//option[@value='" . $widget . "']");
374
375 //fill in specific elements for different widgets
376 switch ($widget) {
377 case 'CheckBox':
378 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
379 $this->type("options_per_line", "2");
380 break;
381
382 case 'Radio':
383 $this->_createFieldOptions(rand(3, 7), 'option', $type_map[$type]['options']);
384 $this->type("options_per_line", "1");
385 break;
386
387 case 'Date':
388 $this->click("date_format");
389 $this->select("date_format", "value=yy-mm-dd");
390 $this->click("//option[@value='yy-mm-dd']");
391 break;
6c6e6187 392
6a488035
TO
393 //TODO allow for more things....
394 }
395
396 //clicking save
280dc0fe 397 $this->click("_qf_Field_done-bottom");
6a488035
TO
398 $this->waitForPageToLoad($this->getTimeoutMsec());
399
400 //Is custom field created?
44c45561 401 $this->waitForText('crm-notification-container', "Custom field '$fieldLabel' has been saved.");
6a488035
TO
402
403 //get the custom id of the custom field that was just created
92915c55
TO
404 $results = $this->webtest_civicrm_api("CustomField", "get", array(
405 'label' => $fieldLabel,
8d7a9d07 406 'custom_group_id' => $group_id,
92915c55 407 ));
6a488035
TO
408 //While there _technically_ could be two fields with the same name, its highly unlikely
409 //so assert that exactly one result is return
410 $this->assertTrue($results['count'] == 1, "Could not uniquely get custom field id");
411 return $results['id'];
412 }
413
4cbe18b8 414 /**
4f1f1f2a 415 * Populates $count options for a custom field on the add custom field page
4cbe18b8
EM
416 * @param int $count
417 * @param string $prefix
418 * @param array $values
419 */
8d7a9d07 420 public function _createFieldOptions($count = 3, $prefix = "option", $values = array()) {
6a488035
TO
421 // Only support up to 10 options on the creation form
422 $count = $count > 10 ? 10 : $count;
423
424 for ($i = 1; $i <= $count; $i++) {
425 $label = $prefix . '_' . substr(sha1(rand()), 0, 6);
426 $this->type("option_label_" . $i, $label);
427 $this->type("option_value_" . $i, (isset($values[$i]) ? $values[$i] : $i));
428 }
429 }
430
4cbe18b8 431 /**
eceb18cc 432 * randomly generates data for a specific custom field.
100fef9d
CW
433 * @param int $contact_id
434 * @param int $group_id
4cbe18b8 435 */
00be9182 436 public function _fillCustomDataForContact($contact_id, $group_id) {
6a488035 437 //edit the given contact
76e86fd8 438 $this->openCiviPage('contact/add', "reset=1&action=update&cid={$contact_id}");
6a488035
TO
439
440 $this->click("expand");
441 $this->waitForElementPresent("address_1_street_address");
442
443 //get the custom fields for the group
444 $fields = $this->webtest_civicrm_api("CustomField", "get", array('custom_group_id' => $group_id));
445 $fields = $fields['values'];
446
447 //we need the id the contact's record in the table for this custom group.
448 //Recent (4.0.6+, i think?) versions of the api return this when getting
449 //custom data for a contact. So we do that.
450 $field_ids = array_keys($fields);
92915c55
TO
451 $contact = $this->webtest_civicrm_api("Contact", "get", array(
452 'contact_id' => $contact_id,
8d7a9d07 453 'return.custom_' . $field_ids[0] => 1,
92915c55
TO
454 ));
455 $group = $this->webtest_civicrm_api("CustomGroup", "get", array('id' => $group_id, 'return.table_name' => 1));
6a488035
TO
456
457 //if the contact has not been saved since this fieldset has been creative,
458 //the form uses id = -1. In this case the table pk wont be in the api results
459 $customValueId = $contact['values'][$contact_id][$group['values'][$group_id]['table_name'] . "_id"];
460 if (isset($customValueId) && !empty($customValueId)) {
461 $table_pk = $customValueId;
462 }
463 else {
464 $table_pk = -1;
465 }
466
467 //fill a value in for each field
468 foreach ($fields as $field_id => $field) {
469 //if there is an option group id, we grab the labels and select on randomly
280dc0fe 470 if ($field['data_type'] == 'Date') {
471 $this->webtestFillDate("custom_" . $field['id'] . "_" . $table_pk, "+1 week");
472 }
473 elseif (isset($field['option_group_id'])) {
6a488035
TO
474 $options = $this->webtest_civicrm_api("OptionValue", "get", array('option_group_id' => $field['option_group_id']));
475 $options = $options['values'];
476 $pick_me = $options[array_rand($options)]['label'];
477 $this->click("xpath=//table//tr/td/label[text()=\"$pick_me\"]");
478 }
479 else {
480 //gonna go ahead and assume its an alphanumeric text question. This
481 //will really only work if the custom data group has not yet been
482 //filled out for this contact
483 $this->type("custom_" . $field['id'] . '_' . $table_pk, sha1(rand()));
484 }
485 }
486
487 //save the form
488 $this->click("_qf_Contact_upload_view");
489 $this->waitForPageToLoad($this->getTimeoutMsec());
490
491 //assert success
6c5f7368 492 $this->waitForText('crm-notification-container', "has been updated");
6a488035 493 }
96025800 494
6a488035 495}