Merge remote-tracking branch 'origin/4.7.7-rc' into 4.7.7-rc-master-2016-05-04-14...
[civicrm-core.git] / tests / phpunit / WebTest / ACL / AssignUsersToRolesTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 require_once 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_ACL_AssignUsersToRolesTest
31 */
32 class WebTest_ACL_AssignUsersToRolesTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testAssignUsersToRoles() {
39
40 $this->webtestLogin();
41
42 $this->openCiviPage("group/add", "reset=1");
43 $groupTitle = "testGroup" . substr(sha1(rand()), 0, 4);
44 $this->type("title", $groupTitle);
45 $this->click("group_type[1]");
46 $this->click("_qf_Edit_upload-bottom");
47 $this->waitForPageToLoad($this->getTimeoutMsec());
48
49 $this->waitForText('crm-notification-container', "The Group '{$groupTitle}' has been saved.");
50
51 $this->openCiviPage("admin/options/acl_role", "action=add&reset=1", "_qf_Options_cancel-bottom");
52
53 $label = "TestAclRole" . substr(sha1(rand()), 0, 4);
54 $this->type("label", $label);
55 $this->type("value", "Acl value" . $label);
56 $this->click("_qf_Options_next-bottom");
57
58 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
59
60 $this->openCiviPage("acl/entityrole", "action=add&reset=1");
61
62 $this->select("acl_role_id", "label=" . $label);
63 $this->select("entity_id", "label={$groupTitle}");
64
65 $this->clickLink("_qf_EntityRole_next-botttom");
66
67 $this->openCiviPage("acl", "action=add&reset=1");
68 $this->click("group_id");
69 $this->select("group_id", "label={$groupTitle}");
70 $this->select("operation", "label=View");
71 $this->select("entity_id", "label={$label}");
72 $this->type("name", "describe {$label}");
73 $this->clickLink("_qf_ACL_next-bottom");
74 }
75
76 /**
77 * Check ACL for Smart Groups and Profiles.
78 */
79 public function testACLforSmartGroupsAndProfiles() {
80 $this->webtestLogin();
81
82 //Create role
83 $role = 'role' . substr(sha1(rand()), 0, 7);
84 $this->open($this->sboxPath . "admin/people/permissions/roles");
85 $this->waitForAjaxContent();
86 $this->type("edit-name", $role);
87 $this->click("edit-add");
88 $this->waitForPageToLoad($this->getTimeoutMsec());
89 $this->open($this->sboxPath . "admin/people/permissions/roles");
90 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
91 $roleURL = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
92 $roleId = end($roleURL);
93
94 //create user with roleId
95 $this->open($this->sboxPath . "admin/people/create");
96 $this->waitForElementPresent("edit-submit");
97 $user = "TestUser" . substr(sha1(rand()), 0, 4);
98 $this->type("edit-name", $user);
99 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
100 $this->type("edit-mail", $emailId);
101 $this->type("edit-pass-pass1", "Test12345");
102 $this->type("edit-pass-pass2", "Test12345");
103 $role = "edit-roles-" . $roleId;
104 $this->check("name=roles[$roleId] value={$roleId}");
105 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
106 $lastName = 'An' . substr(sha1(rand()), 0, 7);
107 $this->type("first_name", $firstName);
108 $this->type("last_name", $lastName);
109 $this->click("edit-submit");
110 $this->waitForPageToLoad($this->getTimeoutMsec());
111 $permissions = array("edit-{$roleId}-access-civicrm");
112 $this->changePermissions($permissions);
113
114 //Create group and add your user's contact to that group
115 $this->openCiviPage("group/add", "reset=1");
116 $groupTitle = "testGroup" . substr(sha1(rand()), 0, 4);
117 $this->type("title", $groupTitle);
118 $this->click("group_type[1]");
119 $this->click("_qf_Edit_upload-bottom");
120 $this->waitForPageToLoad($this->getTimeoutMsec());
121 $this->waitForText('crm-notification-container', "The Group '{$groupTitle}' has been saved.");
122 $this->waitForElementPresent("_qf_Basic_refresh");
123 $this->type('sort_name', $firstName);
124 $this->click('_qf_Basic_refresh');
125 $this->waitForPageToLoad($this->getTimeoutMsec());
126 $this->waitForElementPresent("_qf_Basic_next_action");
127 $this->assertTrue($this->isElementPresent("xpath=//table[@class='selector row-highlight']/tbody/tr/td[3]/a[text()='{$lastName}, {$firstName}']"));
128 $this->click("xpath=//table[@class='selector row-highlight']/tbody//tr/td[1]/input[@type='checkbox']");
129 $this->click('_qf_Basic_next_action');
130 $this->waitForElementPresent("_qf_AddToGroup_back-bottom");
131 $this->click('_qf_AddToGroup_next-bottom');
132 $this->waitForText('crm-notification-container', "1 contact added to group");
133
134 //create Smart Group
135 $this->openCiviPage('contact/search/advanced', 'reset=1');
136 $this->click("location");
137 $this->waitForElementPresent("country");
138 $this->select("country", "UNITED STATES");
139 $this->clickLink("_qf_Advanced_refresh");
140 $this->waitForElementPresent("task");
141 $this->click('radio_ts', 'ts_all');
142 $this->click('task');
143 $this->select('task', 'label=Group - create smart group');
144 $this->waitForPageToLoad($this->getTimeoutMsec());
145 $smartGroupTitle = "SmartGroup" . substr(sha1(rand()), 0, 4);
146 $this->type("title", $smartGroupTitle);
147 $this->clickLink("_qf_SaveSearch_next-bottom");
148 $this->waitForText('crm-notification-container', "Your smart group has been saved as '$smartGroupTitle'");
149
150 //Create ACL role
151 $this->openCiviPage("admin/options/acl_role", "reset=1", "xpath=//a[@class='button new-option']");
152 $this->click("xpath=//a[@class='button new-option']");
153 $label = "TestAclRole" . substr(sha1(rand()), 0, 4);
154 $this->waitForElementPresent("label");
155 $this->type("label", $label);
156 $this->click("_qf_Options_next-bottom");
157 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
158
159 // Assign group to ACL role created
160 $this->openCiviPage("acl/entityrole", "reset=1", 'newACL');
161 $this->click('newACL');
162 $this->waitForElementPresent("acl_role_id");
163 $this->select("acl_role_id", "label=" . $label);
164 $this->waitForAjaxContent();
165 $this->select("entity_id", "label={$groupTitle}");
166 $this->clickLink("_qf_EntityRole_next-botttom", 'newACL', FALSE);
167
168 //Create ACL granting 'Edit' access on smart group to the role
169 $this->waitForAjaxContent();
170 $this->openCiviPage("acl", "reset=1");
171 $this->click('newACL');
172 $this->waitForElementPresent("group_id");
173 $this->select("group_id", "label={$smartGroupTitle}");
174 $this->select("operation", "label=Edit");
175 $this->waitForAjaxContent();
176 $this->select("entity_id", "label={$label}");
177 $this->type("name", "describe {$label}");
178 $this->clickLink("_qf_ACL_next-bottom", 'newACL', FALSE);
179
180 //ACL granting edit permission on events.
181 $this->waitForAjaxContent();
182 $this->click('newACL');
183 $this->waitForElementPresent('name');
184 $this->type("name", "Edit All Events $label");
185 $this->select("entity_id", "label={$label}");
186 $this->waitForAjaxContent();
187 $this->select("operation", "label=Edit");
188 $this->click("xpath=//label[contains(text(), 'Events')]");
189 $this->select("event_id", "value=0");
190 $this->clickLink("_qf_ACL_next-bottom", 'newACL', FALSE);
191
192 $this->webtestLogin($user, 'Test12345');
193 $this->openCiviPage('event/manage/registration', 'reset=1&action=update&id=3');
194 //ensure all the three buttons are not displayed
195 $this->waitForElementPresent('registration_screen');
196 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
197 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Copy')]");
198 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Create')]");
199 $this->webtestLogout();
200
201 $this->webtestLogin();
202
203 //Create ACL granting Edit permission on Profiles
204 $this->openCiviPage("acl", "reset=1", 'newACL');
205 $this->click('newACL');
206 $this->waitForElementPresent('name');
207 $this->type("name", "Edit All Profiles $label");
208 $this->select("entity_id", "label={$label}");
209 $this->select("operation", "label=Edit");
210 $this->click("xpath=//label[contains(text(), 'A profile')]");
211 $this->select("uf_group_id", "value=0");
212 $this->clickLink("_qf_ACL_next-bottom", 'newACL', FALSE);
213
214 //Login as your role user and do Find Contacts
215 $this->webtestLogin($user, 'Test12345');
216 $this->openCiviPage('contact/search/advanced', 'reset=1');
217 $this->click("location");
218 $this->waitForElementPresent("country");
219 $this->select("country", "UNITED STATES");
220 $this->clickLink("_qf_Advanced_refresh");
221 $this->waitForElementPresent("xpath=//div[@class='crm-search-results']");
222 $this->assertElementNotContainsText("xpath=//form[@id='Advanced']/div[3]/div/div", "No matches found for");
223 $this->verifyText("xpath=//div[@class='crm-search-results']//table/tbody/tr[1]/td[8]", 'UNITED STATES');
224
225 $this->checkEditOnEventProfile();
226 }
227
228 /**
229 * CRM-16776 - Check Profile Edit on Events with 'manage event profile' permission.
230 */
231 public function testEventProfilePermission() {
232 $this->webtestLogin();
233
234 //create new role
235 $role = 'role' . substr(sha1(rand()), 0, 7);
236 $this->open($this->sboxPath . "admin/people/permissions/roles");
237
238 $this->waitForAjaxContent();
239 $this->type("edit-name", $role);
240 $this->click("edit-add");
241 $this->waitForPageToLoad($this->getTimeoutMsec());
242
243 $this->open($this->sboxPath . "admin/people/permissions/roles");
244 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
245 $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
246 $roleId = end($roleId);
247
248 $this->open($this->sboxPath . "admin/people/create");
249 $this->waitForElementPresent("edit-submit");
250 $name = "TestUser" . substr(sha1(rand()), 0, 4);
251 $this->type("edit-name", $name);
252 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
253 $this->type("edit-mail", $emailId);
254 $this->type("edit-pass-pass1", "Test12345");
255 $this->type("edit-pass-pass2", "Test12345");
256 $role = "edit-roles-" . $roleId;
257 $this->check("name=roles[$roleId] value={$roleId}");
258
259 //Add profile Details
260 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
261 $lastName = 'An' . substr(sha1(rand()), 0, 7);
262 $this->type("first_name", $firstName);
263 $this->type("last_name", $lastName);
264
265 $this->click("edit-submit");
266 $this->waitForPageToLoad($this->getTimeoutMsec());
267
268 $permissions = array("edit-{$roleId}-access-civicrm", "edit-{$roleId}-edit-all-events", "edit-{$roleId}-manage-event-profiles");
269 $this->changePermissions($permissions);
270 $this->webtestLogout();
271 $this->webtestLogin($name, 'Test12345');
272 $this->checkEditOnEventProfile();
273 }
274
275 /**
276 * Check Profile Edit on OnlineRegistration Tab
277 */
278 public function checkEditOnEventProfile() {
279 $this->openCiviPage('event/manage/registration', 'reset=1&action=update&id=3');
280 //ensure all the three buttons are displayed
281 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
282 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Copy')]");
283 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Create')]");
284
285 $this->click("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
286 $this->waitForAjaxContent();
287 $this->waitForElementPresent("//div[@class='crm-designer-fields-region']");
288 if ($this->isElementPresent("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]")) {
289 $this->click("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]/../../span//a[@title='Remove']");
290 $this->waitForElementNotPresent("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]");
291 }
292 else {
293 $this->click("xpath=//li[@class='crm-designer-palette-section jstree-closed']/a[contains(text(), 'Individual')]");
294 $this->waitForAjaxContent();
295 $this->doubleClick("xpath=//a[contains(text(), 'Individual')]/../ul//li/a[contains(text(), 'City')]");
296 $this->waitForAjaxContent();
297 }
298 $this->click("xpath=//button/span[contains(text(), 'Save')]");
299 $this->waitForElementPresent("crm-notification-container");
300 $this->assertElementNotContainsText("crm-notification-container", 'API permission check failed for UFGroup/create call; insufficient permission: require administer CiviCRM');
301 $this->click("_qf_Registration_upload-top");
302 $this->waitForTextPresent("'Online Registration' information has been saved.");
303 }
304
305 /**
306 * CRM-16777: Allow to add schedule reminder for event through ACLs 'edit' permission
307 */
308 public function testACLforReminders() {
309 $this->webtestLogin('admin');
310
311 //Details for ACLUser1
312 $ACLrole1 = 'ACLrole1' . substr(sha1(rand()), 0, 7);
313 $ACLUser1 = "ACLUser1" . substr(sha1(rand()), 0, 4);
314 $emailId1 = substr(sha1(rand()), 0, 7) . '@web.com';
315
316 //create ACLrole1 (with 'Access CiviCRM' and 'Access CiviEvent' permissions only).
317 $this->open($this->sboxPath . "admin/people/permissions/roles");
318 $this->type("edit-name", $ACLrole1);
319 $this->waitForElementPresent("edit-add");
320 $this->click("edit-add");
321 $this->waitForPageToLoad($this->getTimeoutMsec());
322 $this->open($this->sboxPath . "admin/people/permissions/roles");
323 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$ACLrole1}']");
324 $roleId = explode("people/permissions/", $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$ACLrole1}']/../td[4]/a[text()='edit permissions']/@href"));
325 $permissions = array(
326 "edit-{$roleId[1]}-access-civicrm",
327 "edit-{$roleId[1]}-access-civievent",
328 );
329 $this->changePermissions($permissions);
330
331 //Create ACLUser1
332 $this->open($this->sboxPath . "admin/people/create");
333 $this->waitForElementPresent("edit-submit");
334 $this->type("edit-name", $ACLUser1);
335 $this->type("edit-mail", $emailId1);
336 $this->type("edit-pass-pass1", "Test12345");
337 $this->type("edit-pass-pass2", "Test12345");
338 $this->click("xpath=//div[@class='form-item form-type-checkboxes form-item-roles']/div//div/label[contains(text(), '{$ACLrole1}')]");
339 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
340 $lastName = 'An' . substr(sha1(rand()), 0, 7);
341 $this->type("first_name", $firstName);
342 $this->type("last_name", $lastName);
343 $this->type("street_address-1", "902C El Camino Way SW");
344 $this->type("city-1", "Dumfries");
345 $this->type("postal_code-1", "1234");
346 $this->select("state_province-1", "value=1019");
347 $this->click("edit-submit");
348 $this->waitForPageToLoad($this->getTimeoutMsec());
349
350 //Create group and add contact.
351 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload-bottom');
352 $groupTitle = 'ACLGroup' . substr(sha1(rand()), 0, 7);
353 $this->type("title", $groupTitle);
354 $this->click("group_type_1");
355 $this->click("_qf_Edit_upload-bottom");
356 $this->waitForElementPresent('_qf_Basic_refresh');
357 $this->type("sort_name", $firstName);
358 $this->click('_qf_Basic_refresh');
359 $this->waitForElementPresent('toggleSelect');
360 $this->click('_qf_Basic_next_action');
361 $this->waitForElementPresent('_qf_AddToGroup_back-bottom');
362 $this->click('_qf_AddToGroup_next-bottom');
363 $this->waitForTextPresent("1 contact added to group");
364
365 //Add the ACLs
366 $this->openCiviPage("admin/options/acl_role", "action=add&reset=1", "_qf_Options_cancel-bottom");
367 $label = "TestAclRole" . substr(sha1(rand()), 0, 4);
368 $this->type("label", $label);
369 $this->type("value", "Acl value" . $label);
370 $this->click("_qf_Options_next-bottom");
371 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
372 $this->waitForAjaxContent();
373 $this->openCiviPage("acl/entityrole", "action=add&reset=1");
374 $this->waitForAjaxContent();
375 $this->select("acl_role_id", "label=" . $label);
376 $this->waitForAjaxContent();
377 $this->select("entity_id", "label={$groupTitle}");
378 $this->clickLink("_qf_EntityRole_next-botttom");
379 $this->openCiviPage("acl", "action=add&reset=1");
380 $this->type("name", "Edit Events{$label}");
381 $this->select("operation", "label=Edit");
382 $this->select("entity_id", "label={$label}");
383 $this->waitForElementPresent("xpath=//tr[@class='crm-acl-form-block-object_type']/td[2]/label[contains(text(), 'Events')]");
384 $this->click("xpath=//tr[@class='crm-acl-form-block-object_type']/td[2]/label[contains(text(), 'Events')]");
385 $this->select("event_id", "label=All Events");
386 $this->clickLink("_qf_ACL_next-bottom");
387 $this->webtestLogout();
388 $this->webtestLogin($ACLUser1, 'Test12345');
389
390 //Add scheduled reminder
391 $this->openCiviPage("event/manage/reminder", "reset=1&action=browse&setTab=1&id=1");
392 $reminderTitle = "Fall Fundraiser Dinner" . substr(sha1(rand()), 0, 4);
393 $this->waitForElementPresent('newScheduleReminder');
394 $this->click("newScheduleReminder");
395 $this->waitForElementPresent("_qf_ScheduleReminders_next-bottom");
396 $this->type("title", $reminderTitle);
397 $this->select('entity', 'label=Registered');
398 $this->select('start_action_offset', 'label=1');
399 $this->select('start_action_condition', 'label=after');
400 $this->click('is_repeat');
401 $this->select('repetition_frequency_interval', 'label=2');
402 $this->select('end_date', 'label=Event End Date');
403 $this->click('recipient');
404 $this->select('recipient', 'label=Participant Role');
405 $subject = 'subject' . substr(sha1(rand()), 0, 4);
406 $this->type('subject', $subject);
407 $this->fillRichTextField("html_message", "This is the test HTML version here!!!", 'CKEditor');
408 $this->type("text_message", "This is the test text version here!!!");
409 $this->click('_qf_ScheduleReminders_next-bottom');
410 $this->webtestLogout();
411
412 //Disable the ACLs
413 $this->webtestLogin('admin');
414 $this->openCiviPage("acl", "reset=1");
415 $this->waitForAjaxContent();
416 $this->click("xpath=//div[contains(text(), 'Edit Events{$label}')]/../../td[7]/span/a[2][contains(text(), 'Disable')]");
417 $this->waitForTextPresent("Are you sure you want to disable this ACL?");
418 $this->click("xpath=//button//span[contains(text(), 'Yes')]");
419
420 //Login with same test-user created above
421 $this->webtestLogin($ACLUser1, 'Test12345');
422 $this->openCiviPage("event/manage", "reset=1");
423 $this->waitForElementPresent("xpath=//div[@id='event_status_id']/div[@class='dataTables_wrapper no-footer']");
424 $this->verifyText("xpath=//div[@id='event_status_id']/div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td", "None found.");
425 }
426
427 }