Merge pull request #4629 from systopia/CRM-15665
[civicrm-core.git] / tests / phpunit / WebTest / Case / CaseCustomFieldsTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Case_CaseCustomFieldsTest
31 */
32 class WebTest_Case_CaseCustomFieldsTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testAddCase() {
39 $this->webtestLogin('admin');
40
41 // Enable CiviCase module if necessary
42 $this->enableComponents("CiviCase");
43
44 $customGrp1 = "CaseCustom_Data1_" . substr(sha1(rand()), 0, 7);
45
46 // create custom group1
47 $this->openCiviPage('admin/custom/group', 'reset=1');
48 $this->clickLink("newCustomDataGroup");
49 $this->type("title", $customGrp1);
50 $this->select("extends[0]", "value=Case");
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
54 sleep(1);
55 $this->select("extends_1", "value=2");
56 $this->clickLink("_qf_Group_next-bottom");
57
58 // get custom group id
59 $customGrpId1 = $this->urlArg('gid');
60
61 $customId = $this->_testGetCustomFieldId($customGrpId1);
62 $cusId_1 = 'custom_' . $customId[0] . '_-1';
63 $cusId_2 = 'custom_' . $customId[1] . '_-1';
64 $cusId_3 = 'custom_' . $customId[2] . '_-1';
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
70 // Log in as normal user
71 $this->webtestLogin();
72
73 // Go to reserved New Individual Profile to set value for logged in user's contact name (we'll need that later)
74 $this->openCiviPage('profile/edit', 'reset=1&gid=4', '_qf_Edit_next');
75 $testUserFirstName = "Testuserfirst";
76 $testUserLastName = "Testuserlast";
77 $this->type("first_name", $testUserFirstName);
78 $this->type("last_name", $testUserLastName);
79 $this->clickLink("_qf_Edit_next", "profilewrap4");
80
81 $this->openCiviPage('case/add', 'reset=1&action=add&atype=13&context=standalone', '_qf_Case_upload-bottom');
82
83 // Adding contact with randomized first name (so we can then select that contact when creating case)
84 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
85 $custMname = "Dav" . substr(sha1(rand()), 0, 7);
86 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
87 // We're using pop-up New Contact dialog
88 $client = $this->createDialogContact("client_id");
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
93 $caseStatusLabel = "Ongoing";
94 $subject = "Safe daytime setting - senior female";
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}");
103 $this->waitForAjaxContent();
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
111 $this->type("duration", "20");
112 $this->type("{$cusId_1}", $custFname);
113 $this->type("{$cusId_2}", $custMname);
114 $this->type("{$cusId_3}", $custLname);
115 $this->clickLink("_qf_Case_upload-bottom", "_qf_CaseView_cancel-bottom");
116
117 // Is status message correct?
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']");
121 $this->clickPopupLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
122
123 $openCaseData = array(
124 "Client" => $client['display_name'],
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);
136 $this->click("xpath=//span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']");
137
138 // verify if custom data is present
139 $this->openCiviPage('case', 'reset=1');
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");
143 $cusId_1 = 'custom_' . $customId[0] . '_1';
144 $cusId_2 = 'custom_' . $customId[1] . '_1';
145 $cusId_3 = 'custom_' . $customId[2] . '_1';
146 $this->clickAjaxLink("css=#{$customGrp1} a.button", $cusId_1);
147
148 $custFname = "Miky" . substr(sha1(rand()), 0, 7);
149 $custMname = "Davy" . substr(sha1(rand()), 0, 7);
150 $custLname = "Kristy" . substr(sha1(rand()), 0, 7);
151 $this->type("{$cusId_1}", $custFname);
152 $this->type("{$cusId_2}", $custMname);
153 $this->type("{$cusId_3}", $custLname);
154 $this->clickAjaxLink("_qf_CustomData_upload");
155
156 $this->openCiviPage('case', 'reset=1');
157 $this->clickAjaxLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Change Custom Data']");
158
159 $openCaseChangeData = array(
160 "Client" => $client['display_name'],
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);
170 $this->_testAdvansearchCaseData($customId, $custFname, $custMname, $custLname);
171 $this->_testDeleteCustomData($customGrpId1, $customId);
172 }
173
174 /**
175 * @param $customGrpId1
176 * @param bool $noteRichEditor
177 *
178 * @return array
179 */
180 function _testGetCustomFieldId($customGrpId1, $noteRichEditor=FALSE) {
181 $customId = array();
182 $this->openCiviPage('admin/custom/group/field/add', array('reset' => 1, 'action' => 'add', 'gid' => $customGrpId1));
183
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
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];
210 array_push($customId, $custom2);
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
219 $this->type("label", $field1);
220 $this->check("is_searchable");
221 $this->clickLink("_qf_Field_next_new-bottom");
222
223 $this->type("label", $field2);
224 $this->check("is_searchable");
225 $this->clickLink("_qf_Field_next_new-bottom");
226
227 $this->type("label", $field3);
228 $this->check("is_searchable");
229 $this->clickLink("_qf_Field_done-bottom");
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 }
243
244 return $customId;
245 }
246
247 /**
248 * @param $customGrpId1
249 * @param int $customId
250 */
251 function _testDeleteCustomData($customGrpId1, $customId) {
252 // delete all custom data
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 }
257
258 // delete custom group
259 $this->openCiviPage("admin/custom/group", "action=delete&reset=1&id=" . $customGrpId1);
260 $this->clickLink("_qf_DeleteGroup_next-bottom");
261 }
262
263 /**
264 * CRM-12812
265 */
266 function testCaseCustomNoteRichEditor() {
267 $this->webtestLogin('admin');
268
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
274 // Enable CiviCase module if necessary
275 $this->enableComponents("CiviCase");
276
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
311 // Adding contact with randomized first name (so we can then select that contact when creating case)
312 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
313 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
314 // We're using pop-up New Contact dialog
315 $client = $this->createDialogContact("client_id");
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
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?
343 $this->checkCRMAlert("Case opened successfully.");
344 $this->clickLink("_qf_CaseView_cancel-bottom");
345
346 $this->openCiviPage('case', 'reset=1');
347 $this->waitForElementPresent("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
348
349 $this->click("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
350
351 $openCaseData = array(
352 "Client" => $client['display_name'],
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);
368
369 // verify if custom data is present
370 $this->openCiviPage('case', 'reset=1');
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");
372
373 $this->click("css=#{$customGrp1} .crm-accordion-header");
374
375 $cusId_1 = 'custom_' . $customId[0] . '_1';
376 $cusId_2 = 'custom_' . $customId[1] . '_1';
377 $this->clickLink("css=#{$customGrp1} a.button", '_qf_CustomData_cancel-bottom', FALSE);
378
379 $this->assertElementContainsText("xpath=//span[@class='ui-dialog-title']", "Edit $customGrp1");
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);
392 }
393
394 /**
395 * @param int $customId
396 * @param string $custFname
397 * @param string $custMname
398 * @param $custLname
399 */
400 function _testAdvansearchCaseData($customId, $custFname, $custMname, $custLname) {
401 // search casecontact
402 $this->openCiviPage('contact/search/advanced', 'reset=1', '_qf_Advanced_refresh');
403 $this->click("CiviCase");
404 $this->waitForElementPresent('case_from_relative');
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());
413 $this->assertElementContainsText('crm-container', '1 Contact');
414 }
415 }
416