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