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