Merge pull request #475 from ravishnair/webtest-fix
[civicrm-core.git] / tests / phpunit / WebTest / Report / LoggingReportTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
TO
27require_once 'CiviTest/CiviSeleniumTestCase.php';
28class WebTest_Report_LoggingReportTest extends CiviSeleniumTestCase {
29
30 protected function setUp() {
31 parent::setUp();
32 }
331a5c18 33
6a488035 34 function testLoggingReport() {
6a488035
TO
35 $this->webtestLogin();
36
37 //enable the logging
8fe1f83e 38 $this->openCiviPage('admin/setting/misc', 'reset=1');
6a488035
TO
39 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='Yes']");
40 $this->click("_qf_Miscellaneous_next-top");
57828c6e
DS
41 $this->waitForPageToLoad(2 * $this->getTimeoutMsec());
42 // 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
43
44 //enable CiviCase component
b9715b8a
CW
45 $this->enableComponents("CiviCase");
46
6a488035
TO
47 //add new contact
48 $orginalFirstName = $firstName = 'Anthony' . substr(sha1(rand()), 0, 7);
49 $lastName = 'Anderson' . substr(sha1(rand()), 0, 7);
331a5c18 50
6a488035 51 $this->webtestAddContact($firstName, $lastName);
a471a3b6 52 $cid = $this->urlArg('cid');
6a488035
TO
53
54 //add contact to group
55 $this->waitForElementPresent("xpath=//li[@id='tab_group']/a");
56 $this->click("xpath=//li[@id='tab_group']/a");
efb29358
CW
57 // Because it tends to cause problems, all uses of sleep() must be justified in comments
58 // Sleep should never be used for wait for anything to load from the server
59 // Justification for this instance: FIXME
6a488035
TO
60 sleep(3);
61 $this->select("group_id", "label=Case Resources");
225a8648 62 $this->clickLink("_qf_GroupContact_next", "xpath=//form[@id='GroupContact']//div[@class='view-content']//div[@class='dataTables_wrapper']/table/tbody/tr/td[4]/a");
b481fc6f 63 $this->click("xpath=//form[@id='GroupContact']//div[@class='view-content']//div[@class='dataTables_wrapper']/table/tbody/tr/td[4]/a");
331a5c18
PJ
64
65 // Check confirmation alert.
6a488035
TO
66 $this->assertTrue((bool)preg_match("/^Are you sure you want to remove/",
67 $this->getConfirmation()
68 ));
69 $this->chooseOkOnNextConfirmation();
70 $this->waitForPageToLoad($this->getTimeoutMsec());
71
331a5c18 72 //tag addition
6a488035
TO
73 $this->waitForElementPresent("xpath=//li[@id='tab_tag']/a");
74 $this->click("xpath=//li[@id='tab_tag']/a");
efb29358
CW
75 // Because it tends to cause problems, all uses of sleep() must be justified in comments
76 // Sleep should never be used for wait for anything to load from the server
77 // Justification for this instance: FIXME
6a488035
TO
78 sleep(3);
79 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Company']/../input");
80 $this->waitForTextPresent("Saved");
81 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Government Entity']/../input");
82 $this->waitForTextPresent("Saved");
83 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Company']/../input");
84 $this->waitForTextPresent("Saved");
85
86 //add new note
87 $this->waitForElementPresent("xpath=//li[@id='tab_note']/a");
88 $this->click("xpath=//li[@id='tab_note']/a");
efb29358
CW
89 // Because it tends to cause problems, all uses of sleep() must be justified in comments
90 // Sleep should never be used for wait for anything to load from the server
91 // Justification for this instance: FIXME
6a488035
TO
92 sleep(3);
93 $this->click("xpath=//div[@id='Notes']//div[@class='action-link']/a");
94
95 $this->waitForElementPresent("_qf_Note_upload-top");
96 $noteSubject = "test note" . substr(sha1(rand()), 0, 7);
97 $noteText = "test note text" . substr(sha1(rand()), 0, 7);
98 $this->type('subject', $noteSubject);
99 $this->type('note', $noteText);
100 $this->click("_qf_Note_upload-top");
101 $this->waitForElementPresent("xpath=//div[@id='notes']//a[text()='Edit']");
102 $this->click("xpath=//div[@id='notes']//a[text()='Edit']");
331a5c18 103
6a488035
TO
104 $this->waitForElementPresent("_qf_Note_upload-top");
105 $this->type('subject', $noteSubject . "_edited");
106 $this->type('note', $noteText . "_edited");
f4c17a3c 107 $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']");
331a5c18 108
6a488035 109 $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 110 // Check confirmation alert.
6a488035
TO
111 $this->assertTrue((bool)preg_match("/^Are you sure you want to delete this note/",
112 $this->getConfirmation()
113 ));
114 $this->chooseOkOnNextConfirmation();
115 $this->waitForPageToLoad($this->getTimeoutMsec());
d7149bca 116 $this->waitForText('crm-notification-container', "deleted successfully");
331a5c18 117
6a488035
TO
118 //add new relationship , disable it , delete it
119 $this->waitForElementPresent("xpath=//li[@id='tab_rel']/a");
57828c6e
DS
120 $this->click("css=li#tab_rel a");
121 $this->waitForElementPresent("link=Add Relationship");
122 $this->click("link=Add Relationship");
123 $this->waitForElementPresent("_qf_Relationship_cancel");
6a488035 124 $this->select("relationship_type_id", "label=Employee of");
57828c6e 125 $this->webtestFillAutocomplete("Default");
6a488035 126 $this->waitForElementPresent("quick-save");
225a8648 127 $this->clickLink("quick-save", "xpath=//div[@id='current-relationships']//a[text()='Disable']");
6a488035
TO
128 $this->click("xpath=//div[@id='current-relationships']//a[text()='Disable']");
129 $this->assertTrue((bool)preg_match("/^Are you sure you want to disable this relationship/",
130 $this->getConfirmation()
131 ));
132 $this->chooseOkOnNextConfirmation();
133 $this->waitForPageToLoad($this->getTimeoutMsec());
134 $this->waitForElementPresent(" xpath=//div[@id='inactive-relationships']//a[text()='Delete']");
135 $this->click("xpath=//div[@id='inactive-relationships']//a[text()='Delete']");
136 $this->assertTrue((bool)preg_match("/^Are you sure you want to delete this relationship/",
137 $this->getConfirmation()
138 ));
139 $this->chooseOkOnNextConfirmation();
140 $this->waitForPageToLoad($this->getTimeoutMsec());
141
142 //update existing contact
143 $this->click("xpath=//ul[@id='actions']/li[2]/a");
144 $this->waitForElementPresent("_qf_Contact_upload_view-top");
145 $firstName = "{$firstName}_edited";
146 $this->type("first_name", $firstName);
147 $this->click("_qf_Contact_upload_view-top");
148 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 149
6a488035
TO
150 //add an activity
151 $this->click("xpath=//li[@id='tab_activity']/a");
152 $this->waitForElementPresent("other_activity");
153 $this->select("other_activity", "label=Interview");
154 $this->waitForPageToLoad($this->getTimeoutMsec());
155 $this->click('_qf_Activity_upload-bottom');
156 $this->waitForElementPresent("xpath=//table[@id='contact-activity-selector-activity']/tbody/tr/td[9]/span/a[2]");
157 $this->click("xpath=//table[@id='contact-activity-selector-activity']/tbody/tr/td[9]/span/a[2]");
158 $this->waitForPageToLoad($this->getTimeoutMsec());
159 $this->select("status_id","value=2");
160 $this->click('_qf_Activity_upload-bottom');
161 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 162
6a488035
TO
163 //add a case
164 $this->click("xpath=//li[@id='tab_case']/a");
165 $this->waitForElementPresent("xpath=//div[@id='Cases']//div[@class='action-link']/a");
166 $this->click("xpath=//div[@id='Cases']//div[@class='action-link']/a");
167 $this->waitForPageToLoad($this->getTimeoutMsec());
168 $this->type('activity_subject',"subject".rand());
169 $this->select('case_type_id','value=1');
170 $this->click('_qf_Case_upload-bottom');
171 $this->waitForPageToLoad($this->getTimeoutMsec());
172 $this->click("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
173 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
174 $this->select("case_status_id","value=2");
175 $this->click("_qf_Activity_upload-top");
176 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 177
6a488035 178 //visit the logging contact summary report
8fe1f83e 179 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
180 $this->type('altered_contact_value', $firstName);
181 $this->click("_qf_LoggingSummary_submit");
182 $this->waitForPageToLoad($this->getTimeoutMsec());
183
184 $data = array(
331a5c18
PJ
185 //contact data check
186 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
187 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
188 //relationship data check
189 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Update"),
190 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Insert"),
191 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Delete"),
192 //group data check
193 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Added"),
194 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Removed"),
195 //note data check
196 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
197 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
198 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete"),
199 //tags data check
200 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Insert"),
201 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Government Entity]", "action" => "Insert"),
202 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Delete"),
203 //case data check
204 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Update"),
205 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Insert"),
206 //case activity check
207 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Update"),
208 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Insert"),
209 );
6a488035 210 $this->verifyReportData($data);
331a5c18 211
6a488035
TO
212 //update link (logging details report check)
213 $contactInfo = array();
214 $contactInfo['data'] = array(
215 array('field' => 'Sort Name', 'changed_from' => "{$lastName}, {$orginalFirstName}", 'changed_to' => "{$lastName}, {$firstName}"),
216 array('field' => 'Display Name', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
217 array('field' => 'First Name', 'changed_from' => $orginalFirstName, 'changed_to' => $firstName),
218 // array('field' => 'Email Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
219 // array('field' => 'Postal Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
220 // array('field' => 'Addressee', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
221 );
222 $contactInfo = array_merge($contactInfo, $data[0]);
331a5c18 223
6a488035
TO
224 $relationshipInfo = array();
225 $relationshipInfo['data'] = array(
331a5c18 226 array('field' => 'Relationship Is Active', 'changed_from' => 'true', 'changed_to' => 'false')
6a488035
TO
227 );
228 $relationshipInfo = array_merge($relationshipInfo, $data[2]);
229
230 $noteInfo = array();
231 $noteInfo['data'] = array(
232 array('field' => 'Note', 'changed_from' => $noteText, 'changed_to' => "{$noteText}_edited"),
233 array('field' => 'Subject', 'changed_from' => $noteSubject, 'changed_to' => "{$noteSubject}_edited"),
234 );
235 $noteInfo = array_merge($noteInfo, $data[7]);
236
237 $caseInfo = array();
238 $caseInfo['data'] = array(
239 array('field' => 'Case Status Id', 'changed_from' => 'Ongoing', 'changed_to' => "Resolved"),
240 );
241 $caseInfo = array_merge($caseInfo, $data[13]);
242
331a5c18
PJ
243 $activityInfo = array();
244 $activityInfo['data'] = array(
245 array('field' => 'Activity Status Id', 'changed_from' => 'Scheduled', 'changed_to' => 'Completed'),
246 );
247 $activityInfo = array_merge($activityInfo, $data[15]);
6a488035
TO
248
249 $dataForReportDetail = array($contactInfo, $relationshipInfo, $noteInfo, $caseInfo, $activityInfo);
250 $filters = array(
251 'text' => array('altered_contact_value' => "{$firstName} {$lastName}"),
252 );
253 $this->detailReportCheck($dataForReportDetail, $filters);
331a5c18 254
6a488035 255 //delete contact check
57828c6e 256 $this->openCiviPage('contact/view/delete', "reset=1&delete=1&cid=$cid");
6a488035
TO
257 $this->click("_qf_Delete_done");
258 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 259
8fe1f83e 260 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
261 $this->click("_qf_LoggingSummary_submit");
262 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 263
6a488035
TO
264 $contactDataDelete = array(array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete (to trash)"));
265 $this->verifyReportData($contactDataDelete);
331a5c18 266
6a488035 267 //disable the logging
8fe1f83e 268 $this->openCiviPage('admin/setting/misc', 'reset=1');
6a488035
TO
269 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='No']");
270 $this->click("_qf_Miscellaneous_next-top");
271 $this->waitForTextPresent("Changes Saved");
272 }
331a5c18 273
6a488035
TO
274 function verifyReportData($data) {
275 foreach ($data as $value) {
276 // check for the row contains proper data
277 $actionPath = ($value['action'] == 'Update') ? "td[1]/a[2]" : "td[1][contains(text(), '{$value['action']}')]";
278 $contactCheck = ($value['action'] == 'Delete (to trash)') ? "td[4][contains(text(), '{$value['altered_contact']}')]" : "td[4]/a[contains(text(), '{$value['altered_contact']}')]/..";
331a5c18 279
6a488035 280 $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 281
6a488035
TO
282 if ($value['action'] == 'Update') {
283 $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)");
284 }
285 }
286 }
331a5c18 287
6a488035
TO
288 function detailReportCheck($dataForReportDetail, $filters = array()) {
289 foreach ($dataForReportDetail as $value) {
290 $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]");
291 $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]");
292 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 293
6a488035
TO
294 foreach ($value['data'] as $key => $data) {
295 $rowCount = $this->getXpathCount("//table[@class='report-layout display']/tbody/tr");
296 for ($i = 1; $i <= $rowCount; $i++) {
297 $field = $data['field'];
298 if ($this->isElementPresent("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field'][text()='$field']")) {
299 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field']", preg_quote($data['field']));
300 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-from']", preg_quote($data['changed_from']));
301 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-to']", preg_quote($data['changed_to']));
302 }
303 }
304 }
305
306 //visit the logging contact summary report
8fe1f83e 307 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
6a488035
TO
308 foreach ($filters as $type => $filter) {
309 if ($type == 'text' ) {
310 foreach ($filter as $filterName => $filterValue) {
311 $this->type($filterName, $filterValue);
312 }
313 }
314 }
315 $this->click("_qf_LoggingSummary_submit");
331a5c18 316 $this->waitForPageToLoad($this->getTimeoutMsec());
6a488035
TO
317 }
318 }
b9715b8a 319}