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