Merge pull request #11691 from JKingsnorth/CRM-21773
[civicrm-core.git] / tests / phpunit / WebTest / Case / CaseCustomFieldsTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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_Case_CaseCustomFieldsTest
31 */
6a488035
TO
32class WebTest_Case_CaseCustomFieldsTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
00be9182 38 public function testAddCase() {
42daf119 39 $this->webtestLogin('admin');
6a488035
TO
40
41 // Enable CiviCase module if necessary
0bd37c06
CW
42 $this->enableComponents("CiviCase");
43
6a488035
TO
44 $customGrp1 = "CaseCustom_Data1_" . substr(sha1(rand()), 0, 7);
45
46 // create custom group1
d2771e94 47 $this->openCiviPage('admin/custom/group', 'reset=1');
80f3b91d 48 $this->clickLink("newCustomDataGroup");
6a488035
TO
49 $this->type("title", $customGrp1);
50 $this->select("extends[0]", "value=Case");
0ef7acd2 51 $this->waitForAjaxContent();
6a488035 52 $this->select("extends_1", "value=2");
80f3b91d 53 $this->clickLink("_qf_Group_next-bottom");
6a488035
TO
54
55 // get custom group id
a471a3b6 56 $customGrpId1 = $this->urlArg('gid');
6a488035
TO
57
58 $customId = $this->_testGetCustomFieldId($customGrpId1);
0bd37c06
CW
59 $cusId_1 = 'custom_' . $customId[0] . '_-1';
60 $cusId_2 = 'custom_' . $customId[1] . '_-1';
61 $cusId_3 = 'custom_' . $customId[2] . '_-1';
6a488035
TO
62
63 // let's give full CiviCase permissions.
92915c55
TO
64 $permission = array(
65 'edit-2-access-all-cases-and-activities',
66 'edit-2-access-my-cases-and-activities',
67 'edit-2-administer-civicase',
ae5ffbb7 68 'edit-2-delete-in-civicase',
92915c55 69 );
6a488035
TO
70 $this->changePermissions($permission);
71
42daf119
CW
72 // Log in as normal user
73 $this->webtestLogin();
74
6a488035 75 // Go to reserved New Individual Profile to set value for logged in user's contact name (we'll need that later)
d2771e94 76 $this->openCiviPage('profile/edit', 'reset=1&gid=4', '_qf_Edit_next');
6a488035
TO
77 $testUserFirstName = "Testuserfirst";
78 $testUserLastName = "Testuserlast";
6a488035
TO
79 $this->type("first_name", $testUserFirstName);
80 $this->type("last_name", $testUserLastName);
1fd662d2 81 $this->clickLink("_qf_Edit_next", "profilewrap4", FALSE);
6a488035 82
d2771e94 83 $this->openCiviPage('case/add', 'reset=1&action=add&atype=13&context=standalone', '_qf_Case_upload-bottom');
6a488035 84
6a488035 85 // Adding contact with randomized first name (so we can then select that contact when creating case)
6a488035
TO
86 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
87 $custMname = "Dav" . substr(sha1(rand()), 0, 7);
88 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
80f3b91d
CW
89 // We're using pop-up New Contact dialog
90 $client = $this->createDialogContact("client_id");
6a488035
TO
91
92 // Fill in other form values. We'll use a case type which is included in CiviCase sample data / xml files.
93 $caseTypeLabel = "Adult Day Care Referral";
94
6a488035 95 $caseStatusLabel = "Ongoing";
0bd37c06 96 $subject = "Safe daytime setting - senior female";
6a488035
TO
97 $this->select("medium_id", "value=1");
98 $location = "Main offices";
99 $this->type("activity_location", $location);
100 $details = "65 year old female needs safe location during the day for herself and her dog. She is in good health but somewhat disoriented.";
101 $this->fillRichTextField("activity_details", $details, 'CKEditor');
102 $this->type("activity_subject", $subject);
103
104 $this->select("case_type_id", "label={$caseTypeLabel}");
80f3b91d 105 $this->waitForAjaxContent();
6a488035
TO
106 $this->select("status_id", "label={$caseStatusLabel}");
107
108 // Choose Case Start Date.
109 // Using helper webtestFillDate function.
110 $this->webtestFillDate('start_date', 'now');
111 $today = date('F jS, Y', strtotime('now'));
112
6a488035
TO
113 $this->type("duration", "20");
114 $this->type("{$cusId_1}", $custFname);
115 $this->type("{$cusId_2}", $custMname);
116 $this->type("{$cusId_3}", $custLname);
1fd662d2 117 $this->clickLink("_qf_Case_upload-bottom", "_qf_CaseView_cancel-bottom", FALSE);
6a488035
TO
118
119 // Is status message correct?
80f3b91d 120 $this->checkCRMAlert("Case opened successfully.");
1fd662d2 121 $this->click("_qf_CaseView_cancel-bottom");
80f3b91d 122 $this->openCiviPage('case', 'reset=1', "xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
a5d61f09 123 $this->clickPopupLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
6a488035
TO
124
125 $openCaseData = array(
80f3b91d 126 "Client" => $client['display_name'],
6a488035
TO
127 "Activity Type" => "Open Case",
128 "Subject" => $subject,
129 "Created By" => "{$testUserFirstName} {$testUserLastName}",
130 "Reported By" => "{$testUserFirstName} {$testUserLastName}",
131 "Medium" => "In Person",
132 "Location" => $location,
133 "Date and Time" => $today,
134 "Status" => "Completed",
135 "Priority" => "Normal",
136 );
137 $this->webtestVerifyTabularData($openCaseData);
1fd662d2 138 $this->waitForElementPresent("xpath=//span[@class='ui-button-icon-primary ui-icon fa-times']");
b9fdba0a 139 $this->click("xpath=//span[@class='ui-button-icon-primary ui-icon fa-times']");
6a488035
TO
140
141 // verify if custom data is present
d2771e94 142 $this->openCiviPage('case', 'reset=1');
0ef7acd2 143 $this->waitForElementPresent("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[9]/span/a[1][text()='Manage']");
1361cc61 144 $this->clickLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[9]/span/a[1][text()='Manage']");
80f3b91d
CW
145
146 $this->clickAjaxLink("css=#{$customGrp1} .crm-accordion-header", "css=#{$customGrp1} a.button");
0bd37c06
CW
147 $cusId_1 = 'custom_' . $customId[0] . '_1';
148 $cusId_2 = 'custom_' . $customId[1] . '_1';
149 $cusId_3 = 'custom_' . $customId[2] . '_1';
80f3b91d
CW
150 $this->clickAjaxLink("css=#{$customGrp1} a.button", $cusId_1);
151
6a488035
TO
152 $custFname = "Miky" . substr(sha1(rand()), 0, 7);
153 $custMname = "Davy" . substr(sha1(rand()), 0, 7);
154 $custLname = "Kristy" . substr(sha1(rand()), 0, 7);
6a488035
TO
155 $this->type("{$cusId_1}", $custFname);
156 $this->type("{$cusId_2}", $custMname);
157 $this->type("{$cusId_3}", $custLname);
80f3b91d
CW
158 $this->clickAjaxLink("_qf_CustomData_upload");
159
d2771e94 160 $this->openCiviPage('case', 'reset=1');
80f3b91d
CW
161 $this->clickAjaxLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Change Custom Data']");
162
6a488035 163 $openCaseChangeData = array(
80f3b91d 164 "Client" => $client['display_name'],
6a488035
TO
165 "Activity Type" => "Change Custom Data",
166 "Subject" => $customGrp1 . " : change data",
167 "Created By" => "{$testUserFirstName} {$testUserLastName}",
168 "Reported By" => "{$testUserFirstName} {$testUserLastName}",
169 "Date and Time" => $today,
170 "Status" => "Completed",
171 "Priority" => "Normal",
172 );
173 $this->webtestVerifyTabularData($openCaseChangeData);
6a488035
TO
174 $this->_testAdvansearchCaseData($customId, $custFname, $custMname, $custLname);
175 $this->_testDeleteCustomData($customGrpId1, $customId);
176 }
177
4cbe18b8
EM
178 /**
179 * @param $customGrpId1
180 * @param bool $noteRichEditor
181 *
182 * @return array
183 */
6c6e6187 184 public function _testGetCustomFieldId($customGrpId1, $noteRichEditor = FALSE) {
6a488035 185 $customId = array();
80f3b91d 186 $this->openCiviPage('admin/custom/group/field/add', array('reset' => 1, 'action' => 'add', 'gid' => $customGrpId1));
6a488035 187
7c20a3bd
AW
188 if ($noteRichEditor) {
189 // Create a custom data to add in profile
190 $field1 = "Note_Textarea" . substr(sha1(rand()), 0, 7);
191 $field2 = "Note_Richtexteditor" . substr(sha1(rand()), 0, 7);
192
193 // add custom fields for group 1
7c20a3bd
AW
194 $this->type("label", $field1);
195 $this->select("data_type_0", "value=4");
196 $this->select("data_type_1", "value=TextArea");
197 $this->check("is_searchable");
198 $this->clickLink("_qf_Field_next_new-bottom");
199
200 $this->type("label", $field2);
201 $this->select("data_type_0", "value=4");
202 //$this->select("data_type_1", "value=TextArea");
203 $this->select("data_type_1", "value=RichTextEditor");
204 $this->check("is_searchable");
205 $this->clickLink("_qf_Field_next_new-bottom");
206
207 // get id of custom fields
92915c55
TO
208 $this->openCiviPage("admin/custom/group/field", array(
209 'reset' => 1,
210 'action' => 'browse',
ae5ffbb7 211 'gid' => $customGrpId1,
92915c55 212 ));
7c20a3bd
AW
213 $custom1 = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr[1]/td[8]/span/a[text()='Edit Field']/@href"));
214 $custom1 = $custom1[1];
215 array_push($customId, $custom1);
216 $custom2 = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr[2]/td[8]/span/a[text()='Edit Field']/@href"));
217 $custom2 = $custom2[1];
49f368d3 218 array_push($customId, $custom2);
7c20a3bd
AW
219 }
220 else {
221 // Create a custom data to add in profile
222 $field1 = "Fname" . substr(sha1(rand()), 0, 7);
223 $field2 = "Mname" . substr(sha1(rand()), 0, 7);
224 $field3 = "Lname" . substr(sha1(rand()), 0, 7);
225
226 // add custom fields for group 1
7c20a3bd
AW
227 $this->type("label", $field1);
228 $this->check("is_searchable");
80f3b91d 229 $this->clickLink("_qf_Field_next_new-bottom");
7c20a3bd
AW
230
231 $this->type("label", $field2);
232 $this->check("is_searchable");
80f3b91d 233 $this->clickLink("_qf_Field_next_new-bottom");
7c20a3bd
AW
234
235 $this->type("label", $field3);
236 $this->check("is_searchable");
80f3b91d 237 $this->clickLink("_qf_Field_done-bottom");
7c20a3bd
AW
238
239 // get id of custom fields
92915c55
TO
240 $this->openCiviPage("admin/custom/group/field", array(
241 'reset' => 1,
242 'action' => 'browse',
ae5ffbb7 243 'gid' => $customGrpId1,
92915c55 244 ));
7c20a3bd
AW
245 $custom1 = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr[1]/td[8]/span/a[text()='Edit Field']/@href"));
246 $custom1 = $custom1[1];
247 array_push($customId, $custom1);
248 $custom2 = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr[2]/td[8]/span/a[text()='Edit Field']/@href"));
249 $custom2 = $custom2[1];
250 array_push($customId, $custom2);
251 $custom3 = explode('&id=', $this->getAttribute("xpath=//div[@id='field_page']//table/tbody//tr[3]/td[8]/span/a[text()='Edit Field']/@href"));
252 $custom3 = $custom3[1];
253 array_push($customId, $custom3);
254 }
6a488035
TO
255
256 return $customId;
257 }
258
4cbe18b8
EM
259 /**
260 * @param $customGrpId1
4c16123d 261 * @param array $customId
4cbe18b8 262 */
00be9182 263 public function _testDeleteCustomData($customGrpId1, $customId) {
6a488035 264 // delete all custom data
7c20a3bd 265 foreach ($customId as $cKey => $cValue) {
92915c55
TO
266 $this->openCiviPage("admin/custom/group/field", array(
267 'action' => 'delete',
268 'reset' => '1',
269 'gid' => $customGrpId1,
ae5ffbb7 270 'id' => $cValue,
92915c55 271 ));
7c20a3bd
AW
272 $this->clickLink("_qf_DeleteField_next-bottom");
273 }
49f368d3 274
7c20a3bd
AW
275 // delete custom group
276 $this->openCiviPage("admin/custom/group", "action=delete&reset=1&id=" . $customGrpId1);
277 $this->clickLink("_qf_DeleteGroup_next-bottom");
278 }
6a488035 279
7c20a3bd
AW
280 /**
281 * CRM-12812
282 */
00be9182 283 public function testCaseCustomNoteRichEditor() {
7c20a3bd 284 $this->webtestLogin('admin');
6a488035 285
49f368d3 286 //setting ckeditor as WYSIWYG
287 $this->openCiviPage('admin/setting/preferences/display', 'reset=1', '_qf_Display_next-bottom');
288 $this->select('editor_id', 'CKEditor');
289 $this->clickLink('_qf_Display_next-bottom');
290
7c20a3bd
AW
291 // Enable CiviCase module if necessary
292 $this->enableComponents("CiviCase");
6a488035 293
7c20a3bd
AW
294 $customGrp1 = "CaseCustom_Data1_" . substr(sha1(rand()), 0, 7);
295
296 // create custom group1
297 $this->openCiviPage('admin/custom/group', 'reset=1');
298 $this->clickLink("newCustomDataGroup");
299 $this->type("title", $customGrp1);
300 $this->select("extends[0]", "value=Case");
301 $this->select("extends_1", "value=2");
302 $this->clickLink("_qf_Group_next-bottom");
303
304 // get custom group id
305 $customGrpId1 = $this->urlArg('gid');
306
307 $customId = $this->_testGetCustomFieldId($customGrpId1, TRUE);
308 $cusId_1 = 'custom_' . $customId[0] . '_-1';
309 $cusId_2 = 'custom_' . $customId[1] . '_-1';
310
311 // let's give full CiviCase permissions.
92915c55
TO
312 $permission = array(
313 'edit-2-access-all-cases-and-activities',
314 'edit-2-access-my-cases-and-activities',
315 'edit-2-administer-civicase',
ae5ffbb7 316 'edit-2-delete-in-civicase',
92915c55 317 );
7c20a3bd
AW
318 $this->changePermissions($permission);
319
320 // Log in as normal user
321 $this->webtestLogin();
322
323 // Go to reserved New Individual Profile to set value for logged in user's contact name (we'll need that later)
324 $this->openCiviPage('profile/edit', 'reset=1&gid=4', '_qf_Edit_next');
325 $testUserFirstName = "Testuserfirst";
326 $testUserLastName = "Testuserlast";
327 $this->type("first_name", $testUserFirstName);
328 $this->type("last_name", $testUserLastName);
1fd662d2 329 $this->clickLink("_qf_Edit_next", "profilewrap4", FALSE);
7c20a3bd
AW
330
331 $this->openCiviPage('case/add', 'reset=1&action=add&atype=13&context=standalone', '_qf_Case_upload-bottom');
332
7c20a3bd 333 // Adding contact with randomized first name (so we can then select that contact when creating case)
7c20a3bd
AW
334 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
335 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
80f3b91d
CW
336 // We're using pop-up New Contact dialog
337 $client = $this->createDialogContact("client_id");
7c20a3bd
AW
338
339 // Fill in other form values. We'll use a case type which is included in CiviCase sample data / xml files.
340 $caseTypeLabel = "Adult Day Care Referral";
341
7c20a3bd
AW
342 $caseStatusLabel = "Ongoing";
343 $subject = "Safe daytime setting - senior female";
344 $this->select("medium_id", "value=1");
345 $location = "Main offices";
346 $this->type("activity_location", $location);
347 $details = "65 year old female needs safe location during the day for herself and her dog. She is in good health but somewhat disoriented.";
348 $this->fillRichTextField("activity_details", $details, 'CKEditor');
349 $this->type("activity_subject", $subject);
350
351 $this->select("case_type_id", "label={$caseTypeLabel}");
352 $this->select("status_id", "label={$caseStatusLabel}");
353
354 // Choose Case Start Date.
355 // Using helper webtestFillDate function.
356 $this->webtestFillDate('start_date', 'now');
357 $today = date('F jS, Y', strtotime('now'));
358
359 $this->type("duration", "20");
360 $this->type("{$cusId_1}", $custFname);
361 $this->type("{$cusId_2}", $custLname);
1fd662d2 362 $this->clickLink("_qf_Case_upload-bottom", "_qf_CaseView_cancel-bottom", FALSE);
7c20a3bd
AW
363
364 // Is status message correct?
80f3b91d 365 $this->checkCRMAlert("Case opened successfully.");
1fd662d2 366 $this->click("_qf_CaseView_cancel-bottom");
7c20a3bd
AW
367
368 $this->openCiviPage('case', 'reset=1');
80f3b91d 369 $this->waitForElementPresent("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
7c20a3bd 370
80f3b91d 371 $this->click("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
7c20a3bd
AW
372
373 $openCaseData = array(
80f3b91d 374 "Client" => $client['display_name'],
7c20a3bd
AW
375 "Activity Type" => "Open Case",
376 "Subject" => $subject,
377 "Created By" => "{$testUserFirstName} {$testUserLastName}",
378 "Reported By" => "{$testUserFirstName} {$testUserLastName}",
379 "Medium" => "In Person",
380 "Location" => $location,
381 "Date and Time" => $today,
382 "Status" => "Completed",
383 "Priority" => "Normal",
384 );
385 // wait for elements to load
386 foreach ($openCaseData as $label => $value) {
387 $this->waitForElementPresent("xpath=//table/tbody/tr/td[text()='{$label}']/following-sibling::td");
388 }
389 $this->webtestVerifyTabularData($openCaseData);
7c20a3bd
AW
390
391 // verify if custom data is present
392 $this->openCiviPage('case', 'reset=1');
1fd662d2 393 $this->clickLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[9]/span/a[text()='Manage']", "css=#{$customGrp1} .crm-accordion-header", FALSE);
7c20a3bd
AW
394
395 $this->click("css=#{$customGrp1} .crm-accordion-header");
396
397 $cusId_1 = 'custom_' . $customId[0] . '_1';
398 $cusId_2 = 'custom_' . $customId[1] . '_1';
22cef1b4 399 $this->clickLink("css=#{$customGrp1} a.button", '_qf_CustomData_cancel-bottom', FALSE);
0ef7acd2 400 $this->waitForElementPresent("xpath=//span[@class='ui-dialog-title']");
22cef1b4 401 $this->assertElementContainsText("xpath=//span[@class='ui-dialog-title']", "Edit $customGrp1");
7c20a3bd
AW
402
403 $custFname = "Miky" . substr(sha1(rand()), 0, 7);
404 $custLname = "Kristy" . substr(sha1(rand()), 0, 7);
405 $this->type("{$cusId_1}", $custFname);
406
407 // Wait for rich text editor element
408 $this->waitForElementPresent("css=div#cke_{$cusId_2}");
409
410 $this->fillRichTextField("{$cusId_2}", $custLname, 'CKEditor');
411 $this->click("_qf_CustomData_upload");
412 // delete custom data
413 $this->_testDeleteCustomData($customGrpId1, $customId);
6a488035
TO
414 }
415
4cbe18b8 416 /**
100fef9d
CW
417 * @param int $customId
418 * @param string $custFname
419 * @param string $custMname
4cbe18b8
EM
420 * @param $custLname
421 */
00be9182 422 public function _testAdvansearchCaseData($customId, $custFname, $custMname, $custLname) {
6a488035 423 // search casecontact
d2771e94 424 $this->openCiviPage('contact/search/advanced', 'reset=1', '_qf_Advanced_refresh');
6a488035 425 $this->click("CiviCase");
0bd37c06 426 $this->waitForElementPresent('case_from_relative');
6a488035
TO
427 $cusId_1 = 'custom_' . $customId[0];
428 $cusId_2 = 'custom_' . $customId[1];
429 $cusId_3 = 'custom_' . $customId[2];
430 $this->type("{$cusId_1}", $custFname);
431 $this->type("{$cusId_2}", $custMname);
432 $this->type("{$cusId_3}", $custLname);
433 $this->click("_qf_Advanced_refresh");
434 $this->waitForPageToLoad($this->getTimeoutMsec());
d2771e94 435 $this->assertElementContainsText('crm-container', '1 Contact');
6a488035 436 }
96025800 437
6a488035 438}