Merge pull request #4398 from kenwest/CRM-15492-4.5
[civicrm-core.git] / tests / phpunit / WebTest / Report / LoggingReportTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
6a488035 27require_once 'CiviTest/CiviSeleniumTestCase.php';
e9479dcf
EM
28
29/**
30 * Class WebTest_Report_LoggingReportTest
31 */
6a488035
TO
32class WebTest_Report_LoggingReportTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
331a5c18 37
6a488035 38 function testLoggingReport() {
6a488035
TO
39 $this->webtestLogin();
40
41 //enable the logging
8fe1f83e 42 $this->openCiviPage('admin/setting/misc', 'reset=1');
6a488035
TO
43 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='Yes']");
44 $this->click("_qf_Miscellaneous_next-top");
57828c6e
DS
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.
331a5c18
PJ
47
48 //enable CiviCase component
b9715b8a
CW
49 $this->enableComponents("CiviCase");
50
6a488035
TO
51 //add new contact
52 $orginalFirstName = $firstName = 'Anthony' . substr(sha1(rand()), 0, 7);
53 $lastName = 'Anderson' . substr(sha1(rand()), 0, 7);
331a5c18 54
6a488035 55 $this->webtestAddContact($firstName, $lastName);
a471a3b6 56 $cid = $this->urlArg('cid');
6a488035
TO
57
58 //add contact to group
59 $this->waitForElementPresent("xpath=//li[@id='tab_group']/a");
60 $this->click("xpath=//li[@id='tab_group']/a");
efb29358
CW
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
6a488035
TO
64 sleep(3);
65 $this->select("group_id", "label=Case Resources");
e3bb9229 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");
331a5c18
PJ
69
70 // Check confirmation alert.
9445497e 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']");
6a488035 73
331a5c18 74 //tag addition
6a488035
TO
75 $this->waitForElementPresent("xpath=//li[@id='tab_tag']/a");
76 $this->click("xpath=//li[@id='tab_tag']/a");
efb29358
CW
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
6a488035 80 sleep(3);
e3bb9229 81 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Company']");
6a488035 82 $this->waitForTextPresent("Saved");
e3bb9229 83 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Government Entity']");
6a488035 84 $this->waitForTextPresent("Saved");
e3bb9229 85 $this->click("xpath=//div[@id='tagtree']/ul//li/span/label[text()='Company']");
6a488035
TO
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");
efb29358
CW
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
6a488035 94 sleep(3);
9445497e 95 $this->click("xpath=//div[@class='view-content']//div[@class='action-link']/a");
6a488035
TO
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']//a[text()='Edit']");
104 $this->click("xpath=//div[@id='notes']//a[text()='Edit']");
331a5c18 105
6a488035
TO
106 $this->waitForElementPresent("_qf_Note_upload-top");
107 $this->type('subject', $noteSubject . "_edited");
108 $this->type('note', $noteText . "_edited");
9445497e 109 $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);
331a5c18 110
6a488035 111 $this->click("xpath=//div[@class='crm-results-block']/div[@id='notes']/div/table/tbody/tr//td/span[2]/ul/li[2]/a[text()='Delete']");
331a5c18 112 // Check confirmation alert.
e3bb9229 113 $this->waitForText("xpath=//form[@id='Note']/div[@class='view-content']/div[@class='status']", "Are you sure you want to delete the note ''?");
9445497e 114 $this->click("xpath=//input[@id='_qf_Note_next']");
115 $this->waitForText('crm-notification-container', "Selected Note has been deleted successfully.");
331a5c18 116
6a488035
TO
117 //add new relationship , disable it , delete it
118 $this->waitForElementPresent("xpath=//li[@id='tab_rel']/a");
57828c6e
DS
119 $this->click("css=li#tab_rel a");
120 $this->waitForElementPresent("link=Add Relationship");
121 $this->click("link=Add Relationship");
122 $this->waitForElementPresent("_qf_Relationship_cancel");
6a488035 123 $this->select("relationship_type_id", "label=Employee of");
9445497e 124 $this->select2('related_contact_id', 'Default', TRUE);
125 $this->click('_qf_Relationship_upload-bottom');
e3bb9229 126 $this->waitForElementPresent("xpath=//div[@class='crm-contact-relationship-current']/div//table/tbody//tr/td[9]/span[2][text()='more']/ul/li[1]/a[text()='Disable']");
127 $this->click("xpath=//div[@class='crm-contact-relationship-current']/div//table/tbody//tr/td[9]/span[2][text()='more']/ul/li[1]/a[text()='Disable']");
f390456a 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?');
e3bb9229 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']");
9445497e 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");
6a488035
TO
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());
331a5c18 143
6a488035
TO
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");
9445497e 148 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
6a488035 149 $this->click('_qf_Activity_upload-bottom');
e3bb9229 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]");
9445497e 152 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
6a488035
TO
153 $this->select("status_id","value=2");
154 $this->click('_qf_Activity_upload-bottom');
9445497e 155 $this->waitForText("crm-notification-container", "Activity has been saved.");
e3bb9229 156 $this->waitForElementPresent("xpath=//table[@class='contact-activity-selector-activity dataTable no-footer']/tbody/tr/td[7][text()='Completed']");
331a5c18 157
6a488035
TO
158 //add a case
159 $this->click("xpath=//li[@id='tab_case']/a");
9445497e 160 $this->waitForElementPresent("xpath=//div[@class='view-content']//div[@class='action-link']/a");
161 $this->click("xpath=//div[@class='view-content']//div[@class='action-link']/a");
162 $this->waitForElementPresent("_qf_Case_cancel-bottom");
6a488035
TO
163 $this->type('activity_subject',"subject".rand());
164 $this->select('case_type_id','value=1');
165 $this->click('_qf_Case_upload-bottom');
9445497e 166 $this->waitForElementPresent("xpath=//div[@class='view-content']/table[@class='caseSelector']/tbody/tr[2]/td[9]//span//a[text()='Manage']");
167 $this->click("xpath=//div[@class='view-content']/table[@class='caseSelector']/tbody/tr[2]/td[9]//span//a[text()='Manage']");
168 $this->waitForElementPresent("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
6a488035
TO
169 $this->click("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
170 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
171 $this->select("case_status_id","value=2");
172 $this->click("_qf_Activity_upload-top");
9445497e 173 $this->waitForElementPresent("_qf_CaseView_cancel-bottom");
174 $this->click("_qf_CaseView_cancel-bottom");
6a488035 175 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 176
6a488035 177 //visit the logging contact summary report
8fe1f83e 178 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
179 $this->type('altered_contact_value', $firstName);
180 $this->click("_qf_LoggingSummary_submit");
181 $this->waitForPageToLoad($this->getTimeoutMsec());
182
183 $data = array(
331a5c18
PJ
184 //contact data check
185 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
186 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
187 //relationship data check
188 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Update"),
189 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Insert"),
190 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Delete"),
191 //group data check
192 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Added"),
193 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Removed"),
194 //note data check
195 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
196 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
197 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete"),
198 //tags data check
199 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Insert"),
200 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Government Entity]", "action" => "Insert"),
201 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Delete"),
202 //case data check
203 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Update"),
204 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Insert"),
205 //case activity check
206 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Update"),
207 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Insert"),
208 );
6a488035 209 $this->verifyReportData($data);
331a5c18 210
6a488035
TO
211 //update link (logging details report check)
212 $contactInfo = array();
213 $contactInfo['data'] = array(
214 array('field' => 'Sort Name', 'changed_from' => "{$lastName}, {$orginalFirstName}", 'changed_to' => "{$lastName}, {$firstName}"),
215 array('field' => 'Display Name', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
216 array('field' => 'First Name', 'changed_from' => $orginalFirstName, 'changed_to' => $firstName),
217 // array('field' => 'Email Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
218 // array('field' => 'Postal Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
219 // array('field' => 'Addressee', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
220 );
221 $contactInfo = array_merge($contactInfo, $data[0]);
331a5c18 222
6a488035
TO
223 $relationshipInfo = array();
224 $relationshipInfo['data'] = array(
331a5c18 225 array('field' => 'Relationship Is Active', 'changed_from' => 'true', 'changed_to' => 'false')
6a488035
TO
226 );
227 $relationshipInfo = array_merge($relationshipInfo, $data[2]);
228
229 $noteInfo = array();
230 $noteInfo['data'] = array(
231 array('field' => 'Note', 'changed_from' => $noteText, 'changed_to' => "{$noteText}_edited"),
232 array('field' => 'Subject', 'changed_from' => $noteSubject, 'changed_to' => "{$noteSubject}_edited"),
233 );
234 $noteInfo = array_merge($noteInfo, $data[7]);
235
236 $caseInfo = array();
237 $caseInfo['data'] = array(
238 array('field' => 'Case Status Id', 'changed_from' => 'Ongoing', 'changed_to' => "Resolved"),
239 );
240 $caseInfo = array_merge($caseInfo, $data[13]);
241
331a5c18
PJ
242 $activityInfo = array();
243 $activityInfo['data'] = array(
244 array('field' => 'Activity Status Id', 'changed_from' => 'Scheduled', 'changed_to' => 'Completed'),
245 );
246 $activityInfo = array_merge($activityInfo, $data[15]);
6a488035
TO
247
248 $dataForReportDetail = array($contactInfo, $relationshipInfo, $noteInfo, $caseInfo, $activityInfo);
249 $filters = array(
250 'text' => array('altered_contact_value' => "{$firstName} {$lastName}"),
251 );
252 $this->detailReportCheck($dataForReportDetail, $filters);
331a5c18 253
6a488035 254 //delete contact check
57828c6e 255 $this->openCiviPage('contact/view/delete', "reset=1&delete=1&cid=$cid");
6a488035
TO
256 $this->click("_qf_Delete_done");
257 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 258
8fe1f83e 259 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
260 $this->click("_qf_LoggingSummary_submit");
261 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 262
6a488035
TO
263 $contactDataDelete = array(array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete (to trash)"));
264 $this->verifyReportData($contactDataDelete);
331a5c18 265
6a488035 266 //disable the logging
8fe1f83e 267 $this->openCiviPage('admin/setting/misc', 'reset=1');
6a488035
TO
268 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='No']");
269 $this->click("_qf_Miscellaneous_next-top");
270 $this->waitForTextPresent("Changes Saved");
271 }
331a5c18 272
4cbe18b8
EM
273 /**
274 * @param $data
275 */
6a488035
TO
276 function verifyReportData($data) {
277 foreach ($data as $value) {
278 // check for the row contains proper data
279 $actionPath = ($value['action'] == 'Update') ? "td[1]/a[2]" : "td[1][contains(text(), '{$value['action']}')]";
280 $contactCheck = ($value['action'] == 'Delete (to trash)') ? "td[4][contains(text(), '{$value['altered_contact']}')]" : "td[4]/a[contains(text(), '{$value['altered_contact']}')]/..";
331a5c18 281
6a488035 282 $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']})");
331a5c18 283
6a488035
TO
284 if ($value['action'] == 'Update') {
285 $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)");
286 }
287 }
288 }
331a5c18 289
4cbe18b8
EM
290 /**
291 * @param $dataForReportDetail
292 * @param array $filters
293 */
6a488035
TO
294 function detailReportCheck($dataForReportDetail, $filters = array()) {
295 foreach ($dataForReportDetail as $value) {
296 $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]");
297 $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]");
298 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 299
6a488035
TO
300 foreach ($value['data'] as $key => $data) {
301 $rowCount = $this->getXpathCount("//table[@class='report-layout display']/tbody/tr");
302 for ($i = 1; $i <= $rowCount; $i++) {
303 $field = $data['field'];
304 if ($this->isElementPresent("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field'][text()='$field']")) {
305 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field']", preg_quote($data['field']));
306 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-from']", preg_quote($data['changed_from']));
307 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-to']", preg_quote($data['changed_to']));
308 }
309 }
310 }
311
312 //visit the logging contact summary report
8fe1f83e 313 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
314 foreach ($filters as $type => $filter) {
315 if ($type == 'text' ) {
316 foreach ($filter as $filterName => $filterValue) {
317 $this->type($filterName, $filterValue);
318 }
319 }
320 }
321 $this->click("_qf_LoggingSummary_submit");
331a5c18 322 $this->waitForPageToLoad($this->getTimeoutMsec());
6a488035
TO
323 }
324 }
b9715b8a 325}