Merge pull request #4193 from colemanw/CRM-15342
[civicrm-core.git] / tests / phpunit / WebTest / Case / AddCaseTest.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_AddCaseTest
31 */
32 class WebTest_Case_AddCaseTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testStandaloneCaseAdd() {
39 // Log in as admin first to verify permissions for CiviCase
40 $this->webtestLogin('admin');
41
42 // Enable CiviCase module if necessary
43 $this->enableComponents("CiviCase");
44
45 // let's give full CiviCase permissions to demo user (registered user).
46 $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');
47 $this->changePermissions($permission);
48
49 // Log in as normal user
50 $this->webtestLogin();
51
52 // Go to reserved New Individual Profile to set value for logged in user's contact name (we'll need that later)
53 $this->openCiviPage('profile/edit', 'reset=1&gid=4', NULL);
54 $testUserFirstName = "Testuserfirst";
55 $testUserLastName = "Testuserlast";
56 $this->waitForPageToLoad($this->getTimeoutMsec());
57 $this->waitForElementPresent("_qf_Edit_next");
58 $this->type("first_name", $testUserFirstName);
59 $this->type("last_name", $testUserLastName);
60 $this->clickLink("_qf_Edit_next", "profilewrap4");
61 // Is status message correct?
62 $this->assertElementContainsText('crm-container', "Thank you. Your information has been saved.", "Save successful status message didn't show up after saving profile to update testUserName!");
63
64 $this->openCiviPage('case/add', 'reset=1&action=add&atype=13&context=standalone', '_qf_Case_upload-bottom');
65
66 // Try submitting the form without creating or selecting a contact (test for CRM-7971)
67 $this->clickLink("_qf_Case_upload-bottom", "css=span.crm-error");
68 $this->assertElementContainsText('Case', "Client is a required field.", "Expected form rule error for submit without selecting contact did not show up after clicking Save.");
69
70 // Adding contact with randomized first name (so we can then select that contact when creating case)
71 // We're using pop-up New Contact dialog
72 $client = $this->createDialogContact("client_id");
73
74 // Fill in other form values. We'll use a case type which is included in CiviCase sample data / xml files.
75 $caseTypeLabel = "Adult Day Care Referral";
76 // activity types we expect for this case type
77 $activityTypes = array("ADC referral", "Follow up", "Medical evaluation", "Mental health evaluation");
78 $caseRoles = array("Senior Services Coordinator", "Health Services Coordinator", "Benefits Specialist", "Client");
79 $caseStatusLabel = "Ongoing";
80 $subject = "Safe daytime setting - senior female";
81 $this->select("medium_id", "value=1");
82 $location = "Main offices";
83 $this->type("activity_location", $location);
84 $details = "65 year old female needs safe location during the day for herself and her dog. She is in good health but somewhat disoriented.";
85 $this->fireEvent('activity_details', 'focus');
86 $this->fillRichTextField("activity_details", $details, 'CKEditor');
87 $this->type("activity_subject", $subject);
88
89 $this->select("case_type_id", "label={$caseTypeLabel}");
90 $this->select("status_id", "label={$caseStatusLabel}");
91 // Choose Case Start Date.
92 // Using helper webtestFillDate function.
93 $this->webtestFillDate('start_date', 'now');
94 $today = date('F jS, Y', strtotime('now'));
95
96 $this->type("duration", "20");
97 $this->clickLink("_qf_Case_upload-bottom", "_qf_CaseView_cancel-bottom");
98
99 // Is status message correct?
100 $this->waitForText('crm-notification-container', "Case opened successfully.");
101
102 $summaryStrings = array(
103 "Summary",
104 $client['display_name'],
105 "Type: {$caseTypeLabel}",
106 "Open Date: {$today}",
107 "Status: {$caseStatusLabel}",
108 );
109
110 $this->_testVerifyCaseSummary($summaryStrings, $activityTypes);
111 $this->_testVerifyCaseRoles($caseRoles, "{$testUserLastName}, {$testUserFirstName}");
112 $this->_testVerifyCaseActivities($activityTypes);
113
114 $openCaseData = array(
115 "Client" => $client['display_name'],
116 "Activity Type" => "Open Case",
117 "Subject" => $subject,
118 "Created By" => "{$testUserFirstName} {$testUserLastName}",
119 "Reported By" => "{$testUserFirstName} {$testUserLastName}",
120 "Medium" => "In Person",
121 "Location" => $location,
122 "Date and Time" => $today,
123 "Details" => $details,
124 "Status" => "Completed",
125 "Priority" => "Normal",
126 );
127
128 $this->_testVerifyOpenCaseActivity($subject, $openCaseData);
129
130 //change the case status to Resolved to get the end date
131 $this->click("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
132 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
133 $this->select("case_status_id","value=2");
134 $this->click("_qf_Activity_upload-top");
135
136 $this->_testSearchbyDate($client['first_name'], $client['last_name'], "this.quarter");
137 $this->_testSearchbyDate($client['first_name'], $client['last_name'], "0");
138 $this->_testSearchbyDate($client['first_name'], $client['last_name'], "this.year");
139 $this->_testAssignToClient($client['first_name'], $client['last_name'], $caseTypeLabel);
140 }
141
142 function testAjaxCustomGroupLoad() {
143 $this->webtestLogin();
144
145 // Enable CiviCase module if necessary
146 $this->enableComponents("CiviCase");
147
148 $triggerElement = array('name' => 'case_type_id', 'type' => 'select');
149 $customSets = array(
150 array('entity' => 'Case', 'subEntity' => 'Housing Support', 'triggerElement' => $triggerElement),
151 );
152
153 $pageUrl = array('url' => 'case/add', 'args' => "reset=1&action=add&atype=13&context=standalone");
154 $this->customFieldSetLoadOnTheFlyCheck($customSets, $pageUrl);
155 }
156
157 /**
158 * @param $validateStrings
159 * @param $activityTypes
160 */
161 function _testVerifyCaseSummary($validateStrings, $activityTypes) {
162 $this->assertStringsPresent($validateStrings);
163 foreach ($activityTypes as $aType) {
164 $this->assertText("add_activity_type_id", $aType);
165 }
166 $this->assertElementPresent("link=Assign to Another Client", "Assign to Another Client link is missing.");
167 $this->assertElementPresent("xpath=//a[text()=' Print Report']", "Print Case Summary button is missing.");
168 }
169
170 /**
171 * @param $caseRoles
172 * @param $creatorName
173 */
174 function _testVerifyCaseRoles($caseRoles, $creatorName) {
175 $id = $this->urlArg('id');
176 $this->waitForElementPresent("xpath=//table[@id='caseRoles-selector-$id']/tbody/tr[4]/td[2]/a");
177 // check that expected roles are listed in the Case Roles pane
178 foreach ($caseRoles as $role) {
179 $this->assertText("css=div.crm-case-roles-block", $role);
180 }
181 // check that case creator role has been assigned to logged in user
182 $this->verifyText("xpath=//table[@id='caseRoles-selector-$id']/tbody/tr[4]/td[2]", $creatorName);
183 }
184
185 /**
186 * @param $activityTypes
187 */
188 function _testVerifyCaseActivities($activityTypes) {
189 $id = $this->urlArg('id');
190 // check that expected auto-created activities are listed in the Case Activities table
191 foreach ($activityTypes as $aType) {
192 $this->assertText("case_id_$id", $aType);
193 }
194 }
195
196 /**
197 * @param $subject
198 * @param $openCaseData
199 */
200 function _testVerifyOpenCaseActivity($subject, $openCaseData) {
201 $id = $this->urlArg('id');
202 // check that open case subject is present
203 $this->assertText("case_id_$id", $subject);
204 // click open case activity pop-up dialog
205 $this->click("link=$subject");
206 $this->waitForElementPresent("ActivityView");
207 $this->waitForElementPresent("css=tr.crm-case-activity-view-Activity");
208 // set page location of table containing activity view data
209 $activityViewPrefix = "//*[@id='ActivityView']";
210 $activityViewTableId = "crm-activity-view-table";
211 // Probably don't need both tableId and prefix - but good examples for other situations where only one can be used
212
213 $this->webtestVerifyTabularData($openCaseData, '', $activityViewTableId);
214 $this->click("xpath=//span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']");
215 }
216
217 /**
218 * @param $firstName
219 * @param $lastName
220 * @param $action
221 */
222 function _testSearchbyDate($firstName, $lastName, $action) {
223 // Find Cases
224 if ($action != "0") {
225 $this->openCiviPage('case/search', 'reset=1');
226 $this->select("case_from_relative", "value=$action");
227 $this->select("case_to_relative", "value=$action");
228 $this->click("_qf_Search_refresh");
229 $this->waitForPageToLoad($this->getTimeoutMsec());
230 $this->assertElementContainsText('Search', "$lastName, $firstName");
231 }
232 else {
233 //select date range
234 $this->openCiviPage('case/search', 'reset=1', '_qf_Search_refresh-bottom');
235 $this->select("case_from_relative", "value=$action");
236 $this->webtestFillDate("case_from_start_date_low", "-1 month");
237 $this->webtestFillDate("case_from_start_date_high", "+1 month");
238 $this->select("case_to_relative", "value=$action");
239 $this->webtestFillDate("case_to_end_date_low", "-1 month");
240 $this->webtestFillDate("case_to_end_date_high", "+1 month");
241 $this->click("_qf_Search_refresh-bottom");
242 $this->waitForPageToLoad($this->getTimeoutMsec());
243 $this->assertElementContainsText('Search', "$lastName, $firstName");
244 }
245
246 //Advanced Search
247 $this->openCiviPage('contact/search/advanced', 'reset=1', '_qf_Advanced_refresh');
248 $this->click("CiviCase");
249 $this->waitForElementPresent("xpath=//div[@id='case-search']/table/tbody/tr[3]/td[2]/input[3]");
250 if ($action != "0") {
251 $this->select("case_from_relative", "value=$action");
252 $this->select("case_to_relative", "value=$action");
253 }
254 else {
255 $this->select("case_from_relative", "value=$action");
256 $this->webtestFillDate("case_from_start_date_low", "-1 month");
257 $this->webtestFillDate("case_from_start_date_high", "+1 month");
258 $this->select("case_to_relative", "value=$action");
259 $this->webtestFillDate("case_to_end_date_low", "-1 month");
260 $this->webtestFillDate("case_to_end_date_high", "+1 month");
261 }
262 $this->click("_qf_Advanced_refresh");
263 $this->waitForPageToLoad($this->getTimeoutMsec());
264 $this->assertElementContainsText('Advanced', "$lastName, $firstName");
265 }
266
267 /**
268 * @param $firstName
269 * @param $lastName
270 * @param $caseTypeLabel
271 *
272 * test for assign case to another client
273 */
274 function _testAssignToClient($firstName, $lastName, $caseTypeLabel) {
275 $this->openCiviPage('case/search', 'reset=1', '_qf_Search_refresh-bottom');
276 $this->type('sort_name', $firstName);
277 $this->clickLink('_qf_Search_refresh-bottom');
278 $this->waitForElementPresent("xpath=//table[@class='caseSelector']/tbody//tr/td[3]/a[text()='{$lastName}, {$firstName}']");
279
280 $this->clickPopupLink("xpath=//table[@class='caseSelector']/tbody//tr/td[3]/a[text()='{$lastName}, {$firstName}']/../../td[11]/span[2]/ul/li/a[contains(text(),'Assign to Another Client')]");
281 $client = $this->createDialogContact("reassign_contact_id");
282 $this->clickLink('_qf_EditClient_done-bottom');
283 $this->assertElementContainsText('page-title', "{$client['display_name']} - $caseTypeLabel");
284 }
285 }
286