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