Merge pull request #53 from totten/releaser
[civicrm-core.git] / tests / phpunit / WebTest / Report / RolePermissionReportTest.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
27
28require_once 'CiviTest/CiviSeleniumTestCase.php';
29class WebTest_Report_RolePermissionReportTest extends CiviSeleniumTestCase {
30
31 protected function setUp() {
32 parent::setUp();
33 }
34
35 function testRolePermissionReport() {
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(TRUE);
47
48 //create new roles
49 $role1 = 'role1' . substr(sha1(rand()), 0, 7);
50 $role2 = 'role2' . substr(sha1(rand()), 0, 7);
51 $this->open($this->sboxPath . "admin/people/permissions/roles");
52 $this->waitForElementPresent("edit-add");
53 $this->type("edit-name", $role1);
54 $this->click("edit-add");
55 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 56
6a488035
TO
57 $this->open($this->sboxPath . "admin/people/permissions/roles");
58 $this->waitForElementPresent("edit-add");
59 $this->type("edit-name", $role2);
60 $this->click("edit-add");
61 $this->waitForPageToLoad($this->getTimeoutMsec());
62
63 $this->open($this->sboxPath . "admin/people/permissions/roles");
64
65 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role1}']");
66 $roleid = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role1}']/../td[4]/a[text()='edit permissions']/@href"));
67 $roleId1 = end($roleid);
68 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role2}']");
69 $roleid = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role2}']/../td[4]/a[text()='edit permissions']/@href"));
70 $roleId2 = end($roleid);
71
72 $user1 = $this->_testCreateUser($roleId1);
73 $user2 = $this->_testCreateUser($roleId2);
74 $this->open($this->sboxPath . "user/logout");
75 $this->waitForPageToLoad($this->getTimeoutMsec());
76
77 // let's give full CiviReport permissions.
78 $permissions = array(
79 "edit-2-access-civireport",
80 "edit-2-view-all-contacts",
81 "edit-2-administer-civicrm",
82 "edit-2-access-civicrm",
83 );
84 $this->changePermissions($permissions);
85
86 // change report setting to for a particular role
87 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
88 $this->waitForPageToLoad($this->getTimeoutMsec());
89 $this->click("css=div.crm-report_setting-accordion div.crm-accordion-header");
90 $this->waitForElementPresent("_qf_Summary_submit_save");
91 $this->select("permission", "value=access CiviCRM");
92 $this->select("grouprole-f", "value=$role1");
93 $this->click("add");
94 $this->click("_qf_Summary_submit_save");
95 $this->waitForPageToLoad($this->getTimeoutMsec());
96 $this->open($this->sboxPath . "civicrm/logout?reset=1");
97 $this->open($this->sboxPath);
98 $this->waitForElementPresent('edit-submit');
99 $this->type('edit-name', $user2);
100 $this->type('edit-pass', 'Test12345');
101 $this->click('edit-submit');
102 $this->waitForPageToLoad($this->getTimeoutMsec());
103 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
104 $this->waitForPageToLoad($this->getTimeoutMsec());
105 $this->assertTrue($this->isTextPresent("You do not have permission to access this report."));
106 $this->open($this->sboxPath . "civicrm/report/list?reset=1");
107 $this->waitForPageToLoad($this->getTimeoutMsec());
108 $this->open($this->sboxPath . "civicrm/logout?reset=1");
109 $this->waitForPageToLoad($this->getTimeoutMsec());
110
111 //delete roles
112 $this->webtestLogin(TRUE);
113 $this->open($this->sboxPath . "admin/people/permissions/roles");
114 $this->_roleDelete($role1);
115 $this->_roleDelete($role2);
116 }
331a5c18 117
6a488035
TO
118 /*
119 *check for CRM-10148
120 */
121 function testReservedReportPermission() {
122 // This is the path where our testing install resides.
123 // The rest of URL is defined in CiviSeleniumTestCase base class, in
124 // class attributes.
125 $this->open($this->sboxPath);
126
127 // Logging in. Remember to wait for page to load. In most cases,
128 // you can rely on 30000 as the value that allows your test to pass, however,
129 // sometimes your test might fail because of this. In such cases, it's better to pick one element
130 // somewhere at the end of page and use waitForElementPresent on it - this assures you, that whole
131 // page contents loaded and you can continue your test execution.
132 $this->webtestLogin(TRUE);
133
134 //create new role
135 $role = 'role' . substr(sha1(rand()), 0, 7);
136 $this->open($this->sboxPath . "admin/people/permissions/roles");
137
138 $this->waitForElementPresent("edit-add");
139 $this->type("edit-name", $role);
140 $this->click("edit-add");
141 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 142
6a488035
TO
143 $this->open($this->sboxPath . "admin/people/permissions/roles");
144 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
145 $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
146 $roleId = end($roleId);
147 $user = $this->_testCreateUser($roleId);
148 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
149 $this->waitForPageToLoad($this->getTimeoutMsec());
150 if ($this->isChecked("is_reserved")) {
151 $this->click("is_reserved");
152 $this->click("_qf_Summary_submit_save");
153 $this->waitForPageToLoad($this->getTimeoutMsec());
154 }
155 $permissions = array(
156 "edit-{$roleId}-access-civireport",
157 "edit-{$roleId}-view-all-contacts",
158 "edit-{$roleId}-administer-reports",
159 "edit-{$roleId}-access-civicrm"
331a5c18 160 );
6a488035 161 $this->changePermissions($permissions);
331a5c18 162
6a488035
TO
163 $this->open($this->sboxPath . "civicrm/logout?reset=1");
164 $this->open($this->sboxPath);
165 $this->waitForElementPresent('edit-submit');
166 $this->type('edit-name', $user);
167 $this->type('edit-pass', 'Test12345');
168 $this->click('edit-submit');
169 $this->waitForPageToLoad($this->getTimeoutMsec());
170 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
171 $this->waitForPageToLoad($this->getTimeoutMsec());
172
173 //check if the reserved report field is frozen
174 $this->assertTrue($this->isElementPresent("xpath=//div[@id='instanceForm']//table[3]/tbody//tr/td[2]/tt[text()='[ ]']"));
175
176 $this->open($this->sboxPath . "civicrm/logout?reset=1");
177 $this->open($this->sboxPath);
178 $this->webtestLogin(TRUE);
179 // let's give full CiviReport permissions.
180 $permissions = array(
181 "edit-{$roleId}-access-civireport",
182 "edit-{$roleId}-view-all-contacts",
183 "edit-{$roleId}-administer-reports",
184 "edit-{$roleId}-access-civicrm",
185 "edit-{$roleId}-administer-reserved-reports"
186 );
187 $this->changePermissions($permissions);
331a5c18 188
6a488035
TO
189 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
190 $this->waitForPageToLoad($this->getTimeoutMsec());
191
192 //make the report reserved
193 $this->click("is_reserved");
194 $this->click("_qf_Summary_submit_save");
195 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 196
6a488035
TO
197 $this->open($this->sboxPath . "civicrm/logout?reset=1");
198 $this->open($this->sboxPath);
199 $this->waitForElementPresent('edit-submit');
200 $this->type('edit-name', $user);
201 $this->type('edit-pass', 'Test12345');
202 $this->click('edit-submit');
203 $this->waitForPageToLoad($this->getTimeoutMsec());
204 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
205 $this->waitForPageToLoad($this->getTimeoutMsec());
206
207 //check if the report criteria and settings is accessible
208 $this->assertTrue($this->isElementPresent("xpath=//form[@id='Summary']//div[@id='id_default']//input[@id='fields_email']"));
209 $this->assertTrue($this->isElementPresent("xpath=//form[@id='Summary']//div[@id='instanceForm']/table//input[@id='title']"));
331a5c18 210
6a488035
TO
211 //login as admin and remove reserved permission
212 $this->open($this->sboxPath . "civicrm/logout?reset=1");
213 $this->open($this->sboxPath);
214 $this->webtestLogin(TRUE);
215 $this->open($this->sboxPath . "admin/people/permissions");
216 $this->waitForElementPresent("edit-submit");
331a5c18 217
6a488035
TO
218 if ($this->isChecked("edit-2-administer-reserved-reports")) {
219 $this->click("edit-2-administer-reserved-reports");
220 } else {
221 $this->click("edit-{$roleId}-administer-reserved-reports");
222 }
223 $this->click("edit-submit");
224 $this->waitForPageToLoad($this->getTimeoutMsec());
225
226 //login as user and check for absence of report criteria and settings
227 $this->open($this->sboxPath . "civicrm/logout?reset=1");
228 $this->open($this->sboxPath);
229 $this->waitForElementPresent('edit-submit');
230 $this->type('edit-name', $user);
331a5c18 231 $this->type('edit-pass', 'Test12345');
6a488035
TO
232 $this->click('edit-submit');
233 $this->waitForPageToLoad($this->getTimeoutMsec());
234 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
235 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 236
6a488035 237 if ($this->isElementPresent("xpath=//form[@id='Summary']/div[2]/div/div/div")) {
331a5c18 238 $this->verifyNotText("xpath=//form[@id='Summary']/div[2]/div/div/div", "Report Criteria");
6a488035
TO
239 }
240 if ($this->isElementPresent("xpath=//form[@id='Summary']/div[2]/div[2]/div")) {
331a5c18 241 $this->verifyNotText("xpath=//form[@id='Summary']/div[2]/div[2]/div", "Report Settings");
6a488035 242 }
331a5c18 243
6a488035
TO
244 $this->assertFalse($this->isElementPresent("xpath=//form[@id='Summary']//div[@id='instanceForm']//input[@id='title']"));
245
246 //login as admin and turn the is_reserved flag off for the instance
247 $this->open($this->sboxPath . "civicrm/logout?reset=1");
248 $this->open($this->sboxPath);
249 $this->webtestLogin(TRUE);
250 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
251 $this->waitForPageToLoad($this->getTimeoutMsec());
252 $this->click("is_reserved");
253 $this->click("_qf_Summary_submit_save");
254 $this->waitForPageToLoad($this->getTimeoutMsec());
331a5c18 255
6a488035
TO
256 $this->open($this->sboxPath . "civicrm/logout?reset=1");
257 $this->open($this->sboxPath);
258 $this->waitForElementPresent('edit-submit');
259 $this->type('edit-name', $user);
331a5c18 260 $this->type('edit-pass', 'Test12345');
6a488035
TO
261 $this->click('edit-submit');
262 $this->waitForPageToLoad($this->getTimeoutMsec());
263 $this->open($this->sboxPath . "civicrm/report/instance/1?reset=1");
264 $this->waitForPageToLoad($this->getTimeoutMsec());
265
266 $this->assertTrue($this->isElementPresent("xpath=//form[@id='Summary']//div[@id='id_default']//input[@id='fields_email']"));
267 $this->assertTrue($this->isElementPresent("xpath=//form[@id='Summary']//div[@id='instanceForm']//input[@id='title']"));
331a5c18 268
6a488035
TO
269 //login as admin and delete the role
270 $this->open($this->sboxPath . "civicrm/logout?reset=1");
271 $this->open($this->sboxPath);
272 $this->webtestLogin(TRUE);
273 $this->open($this->sboxPath . "admin/people/permissions/roles");
274 $this->_roleDelete($role);
275 }
331a5c18 276
6a488035
TO
277 function _roleDelete($role) {
278 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']");
279 $this->click("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']");
280 $this->waitForElementPresent('edit-delete');
281 $this->click('edit-delete');
282 $this->waitForPageToLoad($this->getTimeoutMsec());
283 $this->click("edit-submit");
284 $this->waitForTextPresent("The role has been deleted.");
285 }
286
287 function _testCreateUser($roleid) {
288
289 // Go directly to the URL of the screen that will Create User Authentically.
290 $this->open($this->sboxPath . "admin/people/create");
291
292 $this->waitForElementPresent("edit-submit");
293
294 $name = "TestUser" . substr(sha1(rand()), 0, 4);
295 $this->type("edit-name", $name);
296
297 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
298 $this->type("edit-mail", $emailId);
299 $this->type("edit-pass-pass1", "Test12345");
300 $this->type("edit-pass-pass2", "Test12345");
301 $role = "edit-roles-" . $roleid;
302 $this->check("name=roles[$roleid] value={$roleid}");
303
304 //Add profile Details
305 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
306 $lastName = 'An' . substr(sha1(rand()), 0, 7);
307
308 $this->type("first_name", $firstName);
309 $this->type("last_name", $lastName);
310
311 //Address Details
312 $this->type("street_address-1", "902C El Camino Way SW");
313 $this->type("city-1", "Dumfries");
314 $this->type("postal_code-1", "1234");
315 $this->select("state_province-1", "value=1019");
316
317 $this->click("edit-submit");
318 $this->waitForPageToLoad($this->getTimeoutMsec());
319 return $name;
320 }
331a5c18 321}