Merge branch 'getting_started' of https://github.com/cividesk/civicrm-core into civid...
[civicrm-core.git] / tests / phpunit / WebTest / Case / CaseCustomFieldsTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 public 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(
68 'edit-2-access-all-cases-and-activities',
69 'edit-2-access-my-cases-and-activities',
70 'edit-2-administer-civicase',
71 'edit-2-delete-in-civicase',
72 );
73 $this->changePermissions($permission);
74
75 // Log in as normal user
76 $this->webtestLogin();
77
78 // Go to reserved New Individual Profile to set value for logged in user's contact name (we'll need that later)
79 $this->openCiviPage('profile/edit', 'reset=1&gid=4', '_qf_Edit_next');
80 $testUserFirstName = "Testuserfirst";
81 $testUserLastName = "Testuserlast";
82 $this->type("first_name", $testUserFirstName);
83 $this->type("last_name", $testUserLastName);
84 $this->clickLink("_qf_Edit_next", "profilewrap4");
85
86 $this->openCiviPage('case/add', 'reset=1&action=add&atype=13&context=standalone', '_qf_Case_upload-bottom');
87
88 // Adding contact with randomized first name (so we can then select that contact when creating case)
89 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
90 $custMname = "Dav" . substr(sha1(rand()), 0, 7);
91 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
92 // We're using pop-up New Contact dialog
93 $client = $this->createDialogContact("client_id");
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
98 $caseStatusLabel = "Ongoing";
99 $subject = "Safe daytime setting - senior female";
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}");
108 $this->waitForAjaxContent();
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
116 $this->type("duration", "20");
117 $this->type("{$cusId_1}", $custFname);
118 $this->type("{$cusId_2}", $custMname);
119 $this->type("{$cusId_3}", $custLname);
120 $this->clickLink("_qf_Case_upload-bottom", "_qf_CaseView_cancel-bottom");
121
122 // Is status message correct?
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']");
126 $this->clickPopupLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
127
128 $openCaseData = array(
129 "Client" => $client['display_name'],
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);
141 $this->click("xpath=//span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']");
142
143 // verify if custom data is present
144 $this->openCiviPage('case', 'reset=1');
145 $this->clickLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[9]/span/a[1][text()='Manage']");
146
147 $this->clickAjaxLink("css=#{$customGrp1} .crm-accordion-header", "css=#{$customGrp1} a.button");
148 $cusId_1 = 'custom_' . $customId[0] . '_1';
149 $cusId_2 = 'custom_' . $customId[1] . '_1';
150 $cusId_3 = 'custom_' . $customId[2] . '_1';
151 $this->clickAjaxLink("css=#{$customGrp1} a.button", $cusId_1);
152
153 $custFname = "Miky" . substr(sha1(rand()), 0, 7);
154 $custMname = "Davy" . substr(sha1(rand()), 0, 7);
155 $custLname = "Kristy" . substr(sha1(rand()), 0, 7);
156 $this->type("{$cusId_1}", $custFname);
157 $this->type("{$cusId_2}", $custMname);
158 $this->type("{$cusId_3}", $custLname);
159 $this->clickAjaxLink("_qf_CustomData_upload");
160
161 $this->openCiviPage('case', 'reset=1');
162 $this->clickAjaxLink("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Change Custom Data']");
163
164 $openCaseChangeData = array(
165 "Client" => $client['display_name'],
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);
175 $this->_testAdvansearchCaseData($customId, $custFname, $custMname, $custLname);
176 $this->_testDeleteCustomData($customGrpId1, $customId);
177 }
178
179 /**
180 * @param $customGrpId1
181 * @param bool $noteRichEditor
182 *
183 * @return array
184 */
185 public function _testGetCustomFieldId($customGrpId1, $noteRichEditor = FALSE) {
186 $customId = array();
187 $this->openCiviPage('admin/custom/group/field/add', array('reset' => 1, 'action' => 'add', 'gid' => $customGrpId1));
188
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
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
209 $this->openCiviPage("admin/custom/group/field", array(
210 'reset' => 1,
211 'action' => 'browse',
212 'gid' => $customGrpId1,
213 ));
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];
219 array_push($customId, $custom2);
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
228 $this->type("label", $field1);
229 $this->check("is_searchable");
230 $this->clickLink("_qf_Field_next_new-bottom");
231
232 $this->type("label", $field2);
233 $this->check("is_searchable");
234 $this->clickLink("_qf_Field_next_new-bottom");
235
236 $this->type("label", $field3);
237 $this->check("is_searchable");
238 $this->clickLink("_qf_Field_done-bottom");
239
240 // get id of custom fields
241 $this->openCiviPage("admin/custom/group/field", array(
242 'reset' => 1,
243 'action' => 'browse',
244 'gid' => $customGrpId1,
245 ));
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 }
256
257 return $customId;
258 }
259
260 /**
261 * @param $customGrpId1
262 * @param array $customId
263 */
264 public function _testDeleteCustomData($customGrpId1, $customId) {
265 // delete all custom data
266 foreach ($customId as $cKey => $cValue) {
267 $this->openCiviPage("admin/custom/group/field", array(
268 'action' => 'delete',
269 'reset' => '1',
270 'gid' => $customGrpId1,
271 'id' => $cValue,
272 ));
273 $this->clickLink("_qf_DeleteField_next-bottom");
274 }
275
276 // delete custom group
277 $this->openCiviPage("admin/custom/group", "action=delete&reset=1&id=" . $customGrpId1);
278 $this->clickLink("_qf_DeleteGroup_next-bottom");
279 }
280
281 /**
282 * CRM-12812
283 */
284 public function testCaseCustomNoteRichEditor() {
285 $this->webtestLogin('admin');
286
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
292 // Enable CiviCase module if necessary
293 $this->enableComponents("CiviCase");
294
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.
313 $permission = array(
314 'edit-2-access-all-cases-and-activities',
315 'edit-2-access-my-cases-and-activities',
316 'edit-2-administer-civicase',
317 'edit-2-delete-in-civicase',
318 );
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
334 // Adding contact with randomized first name (so we can then select that contact when creating case)
335 $custFname = "Mike" . substr(sha1(rand()), 0, 7);
336 $custLname = "Krist" . substr(sha1(rand()), 0, 7);
337 // We're using pop-up New Contact dialog
338 $client = $this->createDialogContact("client_id");
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
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?
366 $this->checkCRMAlert("Case opened successfully.");
367 $this->clickLink("_qf_CaseView_cancel-bottom");
368
369 $this->openCiviPage('case', 'reset=1');
370 $this->waitForElementPresent("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
371
372 $this->click("xpath=//table[@class='caseSelector']/tbody//tr/td[2]/a[text()='{$client['sort_name']}']/../../td[8]/a[text()='Open Case']");
373
374 $openCaseData = array(
375 "Client" => $client['display_name'],
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);
391
392 // verify if custom data is present
393 $this->openCiviPage('case', 'reset=1');
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");
395
396 $this->click("css=#{$customGrp1} .crm-accordion-header");
397
398 $cusId_1 = 'custom_' . $customId[0] . '_1';
399 $cusId_2 = 'custom_' . $customId[1] . '_1';
400 $this->clickLink("css=#{$customGrp1} a.button", '_qf_CustomData_cancel-bottom', FALSE);
401
402 $this->assertElementContainsText("xpath=//span[@class='ui-dialog-title']", "Edit $customGrp1");
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);
415 }
416
417 /**
418 * @param int $customId
419 * @param string $custFname
420 * @param string $custMname
421 * @param $custLname
422 */
423 public function _testAdvansearchCaseData($customId, $custFname, $custMname, $custLname) {
424 // search casecontact
425 $this->openCiviPage('contact/search/advanced', 'reset=1', '_qf_Advanced_refresh');
426 $this->click("CiviCase");
427 $this->waitForElementPresent('case_from_relative');
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());
436 $this->assertElementContainsText('crm-container', '1 Contact');
437 }
438
439 }