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