a505ff02d05373c9493345617657df26a83444d8
[civicrm-core.git] / tests / phpunit / WebTest / Report / LoggingReportTest.php
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
27
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 class WebTest_Report_LoggingReportTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testLoggingReport() {
36 // This is the path where our testing install resides.
37 // The rest of URL is defined in CiviSeleniumTestCase base class, in
38 // class attributes.
39 $this->open($this->sboxPath);
40
41 // Logging in. Remember to wait for page to load. In most cases,
42 // you can rely on 30000 as the value that allows your test to pass, however,
43 // sometimes your test might fail because of this. In such cases, it's better to pick one element
44 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
45 // page contents loaded and you can continue your test execution.
46 $this->webtestLogin();
47
48 //enable the logging
49 $this->openCiviPage('admin/setting/misc', 'reset=1');
50 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='Yes']");
51 $this->click("_qf_Miscellaneous_next-top");
52 $this->waitForTextPresent("Changes Saved");
53
54 //enable CiviCase component
55 $this->openCiviPage('admin/setting/component', 'reset=1', '_qf_Component_next-bottom');
56 $enabledComponents = $this->getSelectOptions("enableComponents-t");
57 if (!in_array("CiviCase", $enabledComponents)) {
58 $this->addSelection("enableComponents-f", "label=CiviCase");
59 $this->click("//option[@value='CiviCase']");
60 $this->click("add");
61 $this->click("_qf_Component_next-bottom");
62 $this->waitForPageToLoad($this->getTimeoutMsec());
63 $this->assertElementContainsText('crm-notification-container', "Changes Saved");
64 }
65 //add new contact
66 $orginalFirstName = $firstName = 'Anthony' . substr(sha1(rand()), 0, 7);
67 $lastName = 'Anderson' . substr(sha1(rand()), 0, 7);
68
69 $this->webtestAddContact($firstName, $lastName);
70 $cid = explode('&cid=', $this->getLocation());
71
72 //add contact to group
73 $this->waitForElementPresent("xpath=//li[@id='tab_group']/a");
74 $this->click("xpath=//li[@id='tab_group']/a");
75 sleep(3);
76 $this->select("group_id", "label=Case Resources");
77 $this->click("_qf_GroupContact_next");
78
79 $this->waitForPageToLoad($this->getTimeoutMsec());
80 $this->waitForElementPresent("xpath=//div[@id='groupContact']/div/div[4]/table/tbody/tr/td[4]/a");
81 $this->click("xpath=//div[@id='groupContact']/div/div[4]/table/tbody/tr/td[4]/a");
82
83 // Check confirmation alert.
84 $this->assertTrue((bool)preg_match("/^Are you sure you want to remove/",
85 $this->getConfirmation()
86 ));
87 $this->chooseOkOnNextConfirmation();
88 $this->waitForPageToLoad($this->getTimeoutMsec());
89
90 //tag addition
91 $this->waitForElementPresent("xpath=//li[@id='tab_tag']/a");
92 $this->click("xpath=//li[@id='tab_tag']/a");
93 sleep(3);
94 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Company']/../input");
95 $this->waitForTextPresent("Saved");
96 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Government Entity']/../input");
97 $this->waitForTextPresent("Saved");
98 $this->click("xpath=//div[@id='tagtree']/ul//li/label[text()='Company']/../input");
99 $this->waitForTextPresent("Saved");
100
101 //add new note
102 $this->waitForElementPresent("xpath=//li[@id='tab_note']/a");
103 $this->click("xpath=//li[@id='tab_note']/a");
104 sleep(3);
105 $this->click("xpath=//div[@id='Notes']//div[@class='action-link']/a");
106
107 $this->waitForElementPresent("_qf_Note_upload-top");
108 $noteSubject = "test note" . substr(sha1(rand()), 0, 7);
109 $noteText = "test note text" . substr(sha1(rand()), 0, 7);
110 $this->type('subject', $noteSubject);
111 $this->type('note', $noteText);
112 $this->click("_qf_Note_upload-top");
113 $this->waitForElementPresent("xpath=//div[@id='notes']//a[text()='Edit']");
114 $this->click("xpath=//div[@id='notes']//a[text()='Edit']");
115
116 $this->waitForElementPresent("_qf_Note_upload-top");
117 $this->type('subject', $noteSubject . "_edited");
118 $this->type('note', $noteText . "_edited");
119 $this->click("_qf_Note_upload-top");
120
121 $this->waitForElementPresent("xpath=//div[@class='crm-results-block']/div[@id='notes']/div/table/tbody/tr//td/span[2]/ul/li[2]/a[text()='Delete']");
122 $this->click("xpath=//div[@class='crm-results-block']/div[@id='notes']/div/table/tbody/tr//td/span[2]/ul/li[2]/a[text()='Delete']");
123 // Check confirmation alert.
124 $this->assertTrue((bool)preg_match("/^Are you sure you want to delete this note/",
125 $this->getConfirmation()
126 ));
127 $this->chooseOkOnNextConfirmation();
128 $this->waitForPageToLoad($this->getTimeoutMsec());
129
130 //add new relationship , disable it , delete it
131 $this->waitForElementPresent("xpath=//li[@id='tab_rel']/a");
132 $this->click("xpath=//li[@id='tab_rel']/a");
133 sleep(3);
134 $this->click("xpath=//div[@id='Relationships']//div[@class='action-link']/a");
135 $this->waitForElementPresent("_qf_Relationship_refresh");
136 $this->select("relationship_type_id", "label=Employee of");
137 $this->webtestFillAutocomplete("Default Organization");
138 $this->waitForElementPresent("quick-save");
139 $this->click("quick-save");
140 $this->waitForPageToLoad($this->getTimeoutMsec());
141 $this->waitForElementPresent("xpath=//div[@id='current-relationships']//a[text()='Disable']");
142 $this->click("xpath=//div[@id='current-relationships']//a[text()='Disable']");
143 $this->assertTrue((bool)preg_match("/^Are you sure you want to disable this relationship/",
144 $this->getConfirmation()
145 ));
146 $this->chooseOkOnNextConfirmation();
147 $this->waitForPageToLoad($this->getTimeoutMsec());
148 $this->waitForElementPresent(" xpath=//div[@id='inactive-relationships']//a[text()='Delete']");
149 $this->click("xpath=//div[@id='inactive-relationships']//a[text()='Delete']");
150 $this->assertTrue((bool)preg_match("/^Are you sure you want to delete this relationship/",
151 $this->getConfirmation()
152 ));
153 $this->chooseOkOnNextConfirmation();
154 $this->waitForPageToLoad($this->getTimeoutMsec());
155
156 //update existing contact
157 $this->click("xpath=//ul[@id='actions']/li[2]/a");
158 $this->waitForElementPresent("_qf_Contact_upload_view-top");
159 $firstName = "{$firstName}_edited";
160 $this->type("first_name", $firstName);
161 $this->click("_qf_Contact_upload_view-top");
162 $this->waitForPageToLoad($this->getTimeoutMsec());
163
164 //add an activity
165 $this->click("xpath=//li[@id='tab_activity']/a");
166 $this->waitForElementPresent("other_activity");
167 $this->select("other_activity", "label=Interview");
168 $this->waitForPageToLoad($this->getTimeoutMsec());
169 $this->click('_qf_Activity_upload-bottom');
170 $this->waitForElementPresent("xpath=//table[@id='contact-activity-selector-activity']/tbody/tr/td[9]/span/a[2]");
171 $this->click("xpath=//table[@id='contact-activity-selector-activity']/tbody/tr/td[9]/span/a[2]");
172 $this->waitForPageToLoad($this->getTimeoutMsec());
173 $this->select("status_id","value=2");
174 $this->click('_qf_Activity_upload-bottom');
175 $this->waitForPageToLoad($this->getTimeoutMsec());
176
177 //add a case
178 $this->click("xpath=//li[@id='tab_case']/a");
179 $this->waitForElementPresent("xpath=//div[@id='Cases']//div[@class='action-link']/a");
180 $this->click("xpath=//div[@id='Cases']//div[@class='action-link']/a");
181 $this->waitForPageToLoad($this->getTimeoutMsec());
182 $this->type('activity_subject',"subject".rand());
183 $this->select('case_type_id','value=1');
184 $this->click('_qf_Case_upload-bottom');
185 $this->waitForPageToLoad($this->getTimeoutMsec());
186 $this->click("xpath=//form[@id='CaseView']/div[2]/table/tbody/tr/td[4]/a");
187 $this->waitForElementPresent("_qf_Activity_cancel-bottom");
188 $this->select("case_status_id","value=2");
189 $this->click("_qf_Activity_upload-top");
190 $this->waitForPageToLoad($this->getTimeoutMsec());
191
192 //visit the logging contact summary report
193 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
194 $this->type('altered_contact_value', $firstName);
195 $this->click("_qf_LoggingSummary_submit");
196 $this->waitForPageToLoad($this->getTimeoutMsec());
197
198 $data = array(
199 //contact data check
200 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
201 array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
202 //relationship data check
203 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Update"),
204 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Insert"),
205 array("log_type" => "Relationship", "altered_contact" => "{$firstName} {$lastName} [Employee of]", "action" => "Delete"),
206 //group data check
207 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Added"),
208 array("log_type" => "Group", "altered_contact" => "{$firstName} {$lastName} [Case Resources]", "action" => "Removed"),
209 //note data check
210 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Update"),
211 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Insert"),
212 array("log_type" => "Note", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete"),
213 //tags data check
214 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Insert"),
215 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Government Entity]", "action" => "Insert"),
216 array("log_type" => "Tag", "altered_contact" => "{$firstName} {$lastName} [Company]", "action" => "Delete"),
217 //case data check
218 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Update"),
219 array("log_type" => "Case", "altered_contact" => "{$firstName} {$lastName} [Housing Support]", "action" => "Insert"),
220 //case activity check
221 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Update"),
222 array("log_type" => "Activity", "altered_contact" => "{$firstName} {$lastName} [Interview]", "action" => "Insert"),
223 );
224 $this->verifyReportData($data);
225
226 //update link (logging details report check)
227 $contactInfo = array();
228 $contactInfo['data'] = array(
229 array('field' => 'Sort Name', 'changed_from' => "{$lastName}, {$orginalFirstName}", 'changed_to' => "{$lastName}, {$firstName}"),
230 array('field' => 'Display Name', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
231 array('field' => 'First Name', 'changed_from' => $orginalFirstName, 'changed_to' => $firstName),
232 // array('field' => 'Email Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
233 // array('field' => 'Postal Greeting', 'changed_from' => "Dear {$orginalFirstName}", 'changed_to' => "Dear {$firstName}"),
234 // array('field' => 'Addressee', 'changed_from' => "{$orginalFirstName} {$lastName}", 'changed_to' => "{$firstName} {$lastName}"),
235 );
236 $contactInfo = array_merge($contactInfo, $data[0]);
237
238 $relationshipInfo = array();
239 $relationshipInfo['data'] = array(
240 array('field' => 'Relationship Is Active', 'changed_from' => 'true', 'changed_to' => 'false')
241 );
242 $relationshipInfo = array_merge($relationshipInfo, $data[2]);
243
244 $noteInfo = array();
245 $noteInfo['data'] = array(
246 array('field' => 'Note', 'changed_from' => $noteText, 'changed_to' => "{$noteText}_edited"),
247 array('field' => 'Subject', 'changed_from' => $noteSubject, 'changed_to' => "{$noteSubject}_edited"),
248 );
249 $noteInfo = array_merge($noteInfo, $data[7]);
250
251 $caseInfo = array();
252 $caseInfo['data'] = array(
253 array('field' => 'Case Status Id', 'changed_from' => 'Ongoing', 'changed_to' => "Resolved"),
254 );
255 $caseInfo = array_merge($caseInfo, $data[13]);
256
257 $activityInfo = array();
258 $activityInfo['data'] = array(
259 array('field' => 'Activity Status Id', 'changed_from' => 'Scheduled', 'changed_to' => 'Completed'),
260 );
261 $activityInfo = array_merge($activityInfo, $data[15]);
262
263 $dataForReportDetail = array($contactInfo, $relationshipInfo, $noteInfo, $caseInfo, $activityInfo);
264 $filters = array(
265 'text' => array('altered_contact_value' => "{$firstName} {$lastName}"),
266 );
267 $this->detailReportCheck($dataForReportDetail, $filters);
268
269 //delete contact check
270 $this->openCiviPage('contact/view/delete', "reset=1&delete=1&cid={$cid[1]}");
271 $this->click("_qf_Delete_done");
272 $this->waitForPageToLoad($this->getTimeoutMsec());
273
274 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
275 $this->click("_qf_LoggingSummary_submit");
276 $this->waitForPageToLoad($this->getTimeoutMsec());
277
278 $contactDataDelete = array(array("log_type" => "Contact", "altered_contact" => "{$firstName} {$lastName}", "action" => "Delete (to trash)"));
279 $this->verifyReportData($contactDataDelete);
280
281 //disable the logging
282 $this->openCiviPage('admin/setting/misc', 'reset=1');
283 $this->click("xpath=//tr[@class='crm-miscellaneous-form-block-logging']/td[2]/label[text()='No']");
284 $this->click("_qf_Miscellaneous_next-top");
285 $this->waitForTextPresent("Changes Saved");
286 }
287
288 function verifyReportData($data) {
289 foreach ($data as $value) {
290 // check for the row contains proper data
291 $actionPath = ($value['action'] == 'Update') ? "td[1]/a[2]" : "td[1][contains(text(), '{$value['action']}')]";
292 $contactCheck = ($value['action'] == 'Delete (to trash)') ? "td[4][contains(text(), '{$value['altered_contact']}')]" : "td[4]/a[contains(text(), '{$value['altered_contact']}')]/..";
293
294 $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']})");
295
296 if ($value['action'] == 'Update') {
297 $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)");
298 }
299 }
300 }
301
302 function detailReportCheck($dataForReportDetail, $filters = array()) {
303 foreach ($dataForReportDetail as $value) {
304 $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]");
305 $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]");
306 $this->waitForPageToLoad($this->getTimeoutMsec());
307
308 foreach ($value['data'] as $key => $data) {
309 $rowCount = $this->getXpathCount("//table[@class='report-layout display']/tbody/tr");
310 for ($i = 1; $i <= $rowCount; $i++) {
311 $field = $data['field'];
312 if ($this->isElementPresent("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field'][text()='$field']")) {
313 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-field']", preg_quote($data['field']));
314 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-from']", preg_quote($data['changed_from']));
315 $this->verifyText("xpath=//form[@id='LoggingDetail']//table/tbody/tr[{$i}]/td[@class='crm-report-to']", preg_quote($data['changed_to']));
316 }
317 }
318 }
319
320 //visit the logging contact summary report
321 $this->openCiviPage('report/logging/contact/summary', 'reset=1');
322 foreach ($filters as $type => $filter) {
323 if ($type == 'text' ) {
324 foreach ($filter as $filterName => $filterValue) {
325 $this->type($filterName, $filterValue);
326 }
327 }
328 }
329 $this->click("_qf_LoggingSummary_submit");
330 $this->waitForPageToLoad($this->getTimeoutMsec());
331 }
332 }
333 }