Merge pull request #5550 from civicrm/4.5
[civicrm-core.git] / tests / phpunit / WebTest / Report / LoggingReportTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_Report_LoggingReportTest
31 */
32 class WebTest_Report_LoggingReportTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testLoggingReport() {
39 $this->webtestLogin();
40
41 //enable the logging
42 $this->openCiviPage('admin/setting/misc', 'reset=1');
43 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='Yes']");
44 $this->click("_qf_Miscellaneous_next-top");
45 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
46 // FIXME: good to do waitForText here but enabling log is time consuming and status may fade out by the time we do the check.
47
48 //enable CiviCase component
49 $this->enableComponents("CiviCase");
50
51 //add new contact
52 $originalFirstName = $firstName = 'Anthony' . substr(sha1(rand()), 0, 7);
53 $lastName = 'Anderson' . substr(sha1(rand()), 0, 7);
54
55 $this->webtestAddContact($firstName, $lastName);
56 $cid = $this->urlArg('cid');
57
58 //add contact to group
59 $this->waitForElementPresent("xpath=//li[@id='tab_group']/a");
60 $this->click("xpath=//li[@id='tab_group']/a");
61 // Because it tends to cause problems, all uses of sleep() must be justified in comments
62 // Sleep should never be used for wait for anything to load from the server
63 // Justification for this instance: FIXME
64 sleep(3);
65 $this->select("group_id", "label=Case Resources");
66 $this->click("_qf_GroupContact_next");
67 $this->waitForElementPresent("xpath=//form[@id='GroupContact']//div[@class='view-content view-contact-groups']//div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td[4]/a");
68 $this->click("xpath=//form[@id='GroupContact']//div[@class='view-content view-contact-groups']//div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td[4]/a");
69
70 // Check confirmation alert.
71 $this->waitForText("xpath=//div[@class='crm-confirm-dialog ui-dialog-content ui-widget-content modal-dialog']", "Remove $firstName $lastName from Case Resources?");
72 $this->click("xpath=//div[@class='ui-dialog-buttonset']//button//span[text()='Continue']");
73
74 //tag addition
75 $this->waitForElementPresent("xpath=//li[@id='tab_tag']/a");
76 $this->click("xpath=//li[@id='tab_tag']/a");
77 // Because it tends to cause problems, all uses of sleep() must be justified in comments
78 // Sleep should never be used for wait for anything to load from the server
79 // Justification for this instance: FIXME
80 sleep(3);
81 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Company']");
82 $this->waitForTextPresent("Saved");
83 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Government Entity']");
84 $this->waitForTextPresent("Saved");
85 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Company']");
86 $this->waitForTextPresent("Saved");
87
88 //add new note
89 $this->waitForElementPresent("xpath=//li[@id='tab_note']/a");
90 $this->click("xpath=//li[@id='tab_note']/a");
91 // Because it tends to cause problems, all uses of sleep() must be justified in comments
92 // Sleep should never be used for wait for anything to load from the server
93 // Justification for this instance: FIXME
94 sleep(3);
95 $this->click("xpath=//div[@class='view-content']//div[@class='action-link']/a");
96
97 $this->waitForElementPresent("_qf_Note_upload-top");
98 $noteSubject = "test note" . substr(sha1(rand()), 0, 7);
99 $noteText = "test note text" . substr(sha1(rand()), 0, 7);
100 $this->type('subject', $noteSubject);
101 $this->type('note', $noteText);
102 $this->click("_qf_Note_upload-top");
103 $this->waitForElementPresent("xpath=//div[@id='notes']/div/table/tbody/tr/td[7]/span[1]/a[2][text()='Edit']");
104 $this->click("xpath=//div[@id='notes']/div/table/tbody/tr/td[7]/span[1]/a[2][text()='Edit']");
105 $this->waitForElementPresent("_qf_Note_upload-top");
106 $this->type('subject', $noteSubject . "_edited");
107 $this->type('note', $noteText . "_edited");
108 $this->clickLink("_qf_Note_upload-top", "xpath=//div[@class='crm-results-block']/div[@id='notes']/div/table/tbody/tr//td/span[2]/ul/li[2]/a[text()='Delete']", FALSE);
109
110 $this->click("xpath=//div[@id='notes']/div/table/tbody/tr/td[7]/span[2]/ul/li[2]/a[text()='Delete']");
111 // Check confirmation alert.
112 $this->waitForText("xpath=//form[@id='Note']/div[@class='view-content']/div[@class='status']", "Are you sure you want to delete the note ''?");
113 $this->click("xpath=//input[@id='_qf_Note_next']");
114 $this->waitForText('crm-notification-container', "Selected Note has been deleted successfully.");
115
116 //add new relationship , disable it , delete it
117 $this->waitForElementPresent("xpath=//li[@id='tab_rel']/a");
118 $this->click("css=li#tab_rel a");
119 $this->waitForElementPresent("link=Add Relationship");
120 $this->click("link=Add Relationship");
121 $this->waitForElementPresent("_qf_Relationship_cancel");
122 $this->select("relationship_type_id", "label=Employee of");
123 $this->select2('related_contact_id', 'Default', TRUE);
124 $this->click('_qf_Relationship_upload-bottom');
125 $this->waitForElementPresent("xpath=//div[@id='contact-summary-relationship-tab']/div[2]/div/table/tbody/tr/td[9]/span[2][text()='more']/ul/li[1]/a[text()='Disable']");
126
127 $this->click("xpath=//div[@id='contact-summary-relationship-tab']/div[2]/div/table/tbody/tr/td[9]/span[2][text()='more']/ul/li[1]/a[text()='Disable']");
128 $this->waitForText("xpath=//div[@class='crm-confirm-dialog ui-dialog-content ui-widget-content modal-dialog crm-ajax-container']", 'Are you sure you want to disable this relationship?');
129 $this->click("xpath=//div[@class='ui-dialog-buttonset']//button//span[text()='Yes']");
130 $this->waitForElementPresent("xpath=//div[@class='crm-contact-relationship-past']/div//table/tbody//tr/td[9]/span[2][text()='more']/ul/li[2]/a[text()='Delete']");
131 $this->click("xpath=//div[@class='crm-contact-relationship-past']/div//table/tbody//tr/td[9]/span[2][text()='more']/ul/li[2]/a[text()='Delete']");
132 $this->waitForText("xpath=//form[@id='Relationship']/div[@class='status']", "Are you sure you want to delete this Relationship?");
133 $this->click("_qf_Relationship_next-bottom");
134 $this->waitForElementPresent("link=Add Relationship");
135
136 //update existing contact
137 $this->click("xpath=//ul[@id='actions']/li[2]/a");
138 $this->waitForElementPresent("_qf_Contact_upload_view-top");
139 $firstName = "{$firstName}_edited";
140 $this->type("first_name", $firstName);
141 $this->click("_qf_Contact_upload_view-top");
142 $this->waitForPageToLoad($this->getTimeoutMsec());
143
144 //add an activity
145 $this->click("xpath=//li[@id='tab_activity']/a");
146 $this->waitForElementPresent("other_activity");
147 $this->select("other_activity", "label=Interview");
148 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
149 $this->click('_qf_Activity_upload-bottom');
150 $this->waitForElementPresent("xpath=//table[@class='contact-activity-selector-activity dataTable no-footer']/tbody/tr/td[8]/span/a[2]");
151 $this->click("xpath=//table[@class='contact-activity-selector-activity dataTable no-footer']/tbody/tr/td[8]/span/a[2]");
152 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
153 $this->select("status_id", "value=2");
154 $this->click('_qf_Activity_upload-bottom');
155 $this->waitForText("crm-notification-container", "Activity has been saved.");
156 $this->waitForElementPresent("xpath=//table[@class='contact-activity-selector-activity dataTable no-footer']/tbody/tr/td[7][text()='Completed']");
157
158 //add a case
159 $this->waitForAjaxContent();
160 $this->click("xpath=//li[@id='tab_case']/a");
161 $this->waitForElementPresent("xpath=//div[@class='view-content']//div[@class='action-link']/a");
162 $this->click("xpath=//div[@class='view-content']//div[@class='action-link']/a");
163 $this->waitForElementPresent("_qf_Case_cancel-bottom");
164 $this->type('activity_subject', "subject" . rand());
165 $this->select('case_type_id', 'value=1');
166 $this->click('_qf_Case_upload-bottom');
167 $this->waitForElementPresent("xpath=//form[@id='Search']/div[2]/div/table/tbody/tr[2]/td[9]/span[1]/a[1][text()='Manage']");
168 $this->click("xpath=//form[@id='Search']/div[2]/div/table/tbody/tr[2]/td[9]/span[1]/a[1][text()='Manage']");
169 $this->waitForElementPresent("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
170 $this->click("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
171 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
172 $this->select("case_status_id", "value=2");
173 $this->click("_qf_Activity_upload-top");
174 $this->waitForElementPresent("_qf_CaseView_cancel-bottom");
175 $this->click("_qf_CaseView_cancel-bottom");
176 $this->waitForPageToLoad($this->getTimeoutMsec());
177
178 //visit the logging contact summary report
179 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
180 $this->waitForElementPresent('altered_contact_value');
181 $this->type('altered_contact_value', $firstName);
182 $this->click("_qf_LoggingSummary_submit");
183 $this->waitForPageToLoad($this->getTimeoutMsec());
184
185 $data = array(
186 //contact data check
187 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
188 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
189 //relationship data check
190 array(
191 "log_type" => "Relationship",
192 "altered_contact" => "{$firstName} {$lastName} [Employee of]",
193 "action" => "Update",
194 ),
195 array(
196 "log_type" => "Relationship",
197 "altered_contact" => "{$firstName} {$lastName} [Employee of]",
198 "action" => "Insert",
199 ),
200 array(
201 "log_type" => "Relationship",
202 "altered_contact" => "{$firstName} {$lastName} [Employee of]",
203 "action" => "Delete",
204 ),
205 //group data check
206 array(
207 "log_type" => "Group",
208 "altered_contact" => "{$firstName} {$lastName} [Case Resources]",
209 "action" => "Added",
210 ),
211 array(
212 "log_type" => "Group",
213 "altered_contact" => "{$firstName} {$lastName} [Case Resources]",
214 "action" => "Removed",
215 ),
216 //note data check
217 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
218 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
219 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete"),
220 //tags data check
221 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Insert"),
222 array(
223 "log_type" => "Tag",
224 "altered_contact" => "{$firstName} {$lastName} [Government Entity]",
225 "action" => "Insert",
226 ),
227 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Delete"),
228 //case data check
229 array(
230 "log_type" => "Case",
231 "altered_contact" => "{$firstName} {$lastName} [Housing Support]",
232 "action" => "Update",
233 ),
234 array(
235 "log_type" => "Case",
236 "altered_contact" => "{$firstName} {$lastName} [Housing Support]",
237 "action" => "Insert",
238 ),
239 //case activity check
240 array(
241 "log_type" => "Activity",
242 "altered_contact" => "{$firstName} {$lastName} [Interview]",
243 "action" => "Update",
244 ),
245 array(
246 "log_type" => "Activity",
247 "altered_contact" => "{$firstName} {$lastName} [Interview]",
248 "action" => "Insert",
249 ),
250 );
251 $this->verifyReportData($data);
252
253 //update link (logging details report check)
254 $contactInfo = array();
255 $contactInfo['data'] = array(
256 array(
257 'field' => 'Sort Name',
258 'changed_from' => "{$lastName}, {$originalFirstName}",
259 'changed_to' => "{$lastName}, {$firstName}",
260 ),
261 array(
262 'field' => 'Display Name',
263 'changed_from' => "{$originalFirstName} {$lastName}",
264 'changed_to' => "{$firstName} {$lastName}",
265 ),
266 array('field' => 'First Name', 'changed_from' => $originalFirstName, 'changed_to' => $firstName),
267 // array('field' => 'Email Greeting', 'changed_from' => "Dear {$originalFirstName}", 'changed_to' => "Dear {$firstName}"),
268 // array('field' => 'Postal Greeting', 'changed_from' => "Dear {$originalFirstName}", 'changed_to' => "Dear {$firstName}"),
269 // array('field' => 'Addressee', 'changed_from' => "{$originalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
270 );
271 $contactInfo = array_merge($contactInfo, $data[0]);
272
273 $relationshipInfo = array();
274 $relationshipInfo['data'] = array(
275 array('field' => 'Relationship Is Active', 'changed_from' => 'true', 'changed_to' => 'false'),
276 );
277 $relationshipInfo = array_merge($relationshipInfo, $data[2]);
278
279 $noteInfo = array();
280 $noteInfo['data'] = array(
281 array('field' => 'Note', 'changed_from' => $noteText, 'changed_to' => "{$noteText}_edited"),
282 array('field' => 'Subject', 'changed_from' => $noteSubject, 'changed_to' => "{$noteSubject}_edited"),
283 );
284 $noteInfo = array_merge($noteInfo, $data[7]);
285
286 $caseInfo = array();
287 $caseInfo['data'] = array(
288 array('field' => 'Case Status Id', 'changed_from' => 'Ongoing', 'changed_to' => "Resolved"),
289 );
290 $caseInfo = array_merge($caseInfo, $data[13]);
291
292 $activityInfo = array();
293 $activityInfo['data'] = array(
294 array('field' => 'Activity Status Id', 'changed_from' => 'Scheduled', 'changed_to' => 'Completed'),
295 );
296 $activityInfo = array_merge($activityInfo, $data[15]);
297
298 $dataForReportDetail = array($contactInfo, $relationshipInfo, $noteInfo, $caseInfo, $activityInfo);
299 $filters = array(
300 'text' => array('altered_contact_value' => "{$firstName} {$lastName}"),
301 );
302 $this->detailReportCheck($dataForReportDetail, $filters);
303
304 //delete contact check
305 $this->openCiviPage('contact/view/delete', "reset=1&delete=1&cid=$cid");
306 $this->click("_qf_Delete_done");
307 $this->waitForPageToLoad($this->getTimeoutMsec());
308
309 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
310 $this->click("_qf_LoggingSummary_submit");
311 $this->waitForPageToLoad($this->getTimeoutMsec());
312
313 $contactDataDelete = array(
314 array(
315 "log_type" => "Contact",
316 "altered_contact" => "{$firstName} {$lastName}",
317 "action" => "Delete (to trash)",
318 ),
319 );
320 $this->verifyReportData($contactDataDelete);
321
322 //disable the logging
323 $this->openCiviPage('admin/setting/misc', 'reset=1');
324 $this->waitForElementPresent("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='No']");
325 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='No']");
326 $this->click("_qf_Miscellaneous_next-top");
327 $this->waitForTextPresent("Changes Saved");
328 }
329
330 /**
331 * @param $data
332 */
333 public function verifyReportData($data) {
334 foreach ($data as $value) {
335 // check for the row contains proper data
336 $actionPath = ($value['action'] == 'Update') ? "td[1]/a[2]" : "td[1][contains(text(), '{$value['action']}')]";
337 $contactCheck = ($value['action'] == 'Delete (to trash)') ? "td[4][contains(text(), '{$value['altered_contact']}')]" : "td[4]/a[contains(text(), '{$value['altered_contact']}')]/..";
338
339 $this->assertTrue($this->isElementPresent("xpath=//table/tbody//tr/td[2][contains(text(), '{$value['log_type']}')]/../{$contactCheck}/../{$actionPath}"), "The proper record not present for (log type : {$value['log_type']}, altered contact : {$value['altered_contact']}, action as {$value['action']})");
340
341 if ($value['action'] == 'Update') {
342 $this->assertTrue(($value['action'] == $this->getText("xpath=//table/tbody//tr/td[2][contains(text(), '{$value['log_type']}')]/../td[4]/a[contains(text(), '{$value['altered_contact']}')]/../../{$actionPath}")), "The proper record action {$value['action']} not present for (log type : {$value['log_type']}, altered contact : {$value['altered_contact']} record)");
343 }
344 }
345 }
346
347 /**
348 * @param $dataForReportDetail
349 * @param array $filters
350 */
351 public function detailReportCheck($dataForReportDetail, $filters = array()) {
352 foreach ($dataForReportDetail as $value) {
353 $this->waitForElementPresent("xpath=//table/tbody//tr/td[2][contains(text(), '{$value['log_type']}')]/../td[4]/a[contains(text(), '{$value['altered_contact']}')]/../../td[1]/a[2]");
354 $this->click("xpath=//table/tbody//tr/td[2][contains(text(), '{$value['log_type']}')]/../td[4]/a[contains(text(), '{$value['altered_contact']}')]/../../td[1]/a[2]");
355 $this->waitForPageToLoad($this->getTimeoutMsec());
356
357 foreach ($value['data'] as $key => $data) {
358 $rowCount = $this->getXpathCount("//table[@class='report-layout display']/tbody/tr");
359 for ($i = 1; $i <= $rowCount; $i++) {
360 $field = $data['field'];
361 if ($this->isElementPresent("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field'][text()='$field']")) {
362 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field']", preg_quote($data['field']));
363 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-from']", preg_quote($data['changed_from']));
364 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-to']", preg_quote($data['changed_to']));
365 }
366 }
367 }
368
369 //visit the logging contact summary report
370 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
371 foreach ($filters as $type => $filter) {
372 if ($type == 'text') {
373 foreach ($filter as $filterName => $filterValue) {
374 $this->type($filterName, $filterValue);
375 }
376 }
377 }
378 $this->click("_qf_LoggingSummary_submit");
379 $this->waitForPageToLoad($this->getTimeoutMsec());
380 }
381 }
382
383 }