Merge pull request #7075 from rohankatkar/Master_webtests
[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-2015 |
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->waitForElementPresent("edit-submit");
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->type("value", "Acl value" . $label);
157 $this->click("_qf_Options_next-bottom");
158 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
159
160 // Assign group to ACL role created
161 $this->openCiviPage("acl/entityrole", "reset=1", 'newACL');
162 $this->click('newACL');
163 $this->waitForElementPresent("acl_role_id");
164 $this->select("acl_role_id", "label=" . $label);
165 $this->waitForAjaxContent();
166 $this->select("entity_id", "label={$groupTitle}");
167 $this->clickLink("_qf_EntityRole_next-botttom", 'newACL', FALSE);
168
169 //Create ACL granting 'Edit' access on smart group to the role
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->click('newACL');
182 $this->waitForElementPresent('name');
183 $this->type("name", "Edit All Events $label");
184 $this->select("entity_id", "label={$label}");
185 $this->waitForAjaxContent();
186 $this->select("operation", "label=Edit");
187 $this->click("xpath=//label[contains(text(), 'Events')]");
188 $this->select("event_id", "value=0");
189 $this->clickLink("_qf_ACL_next-bottom", 'newACL', FALSE);
190
191 $this->webtestLogin($user, 'Test12345');
192 $this->openCiviPage('event/manage/registration', 'reset=1&action=update&id=3');
193 //ensure all the three buttons are not displayed
194 $this->waitForElementPresent('registration_screen');
195 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
196 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Copy')]");
197 $this->verifyElementNotPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Create')]");
198 $this->webtestLogout();
199
200 $this->webtestLogin();
201
202 //Create ACL granting Edit permission on Profiles
203 $this->openCiviPage("acl", "reset=1", 'newACL');
204 $this->click('newACL');
205 $this->waitForElementPresent('name');
206 $this->type("name", "Edit All Profiles $label");
207 $this->select("entity_id", "label={$label}");
208 $this->select("operation", "label=Edit");
209 $this->click("xpath=//label[contains(text(), 'A profile')]");
210 $this->select("uf_group_id", "value=0");
211 $this->clickLink("_qf_ACL_next-bottom", 'newACL', FALSE);
212
213 //Login as your role user and do Find Contacts
214 $this->webtestLogin($user, 'Test12345');
215 $this->openCiviPage('contact/search/advanced', 'reset=1');
216 $this->click("location");
217 $this->waitForElementPresent("country");
218 $this->select("country", "UNITED STATES");
219 $this->clickLink("_qf_Advanced_refresh");
220 $this->waitForElementPresent("xpath=//div[@class='crm-search-results']");
221 $this->assertElementNotContainsText("xpath=//form[@id='Advanced']/div[3]/div/div", "No matches found for");
222 $this->verifyText("xpath=//div[@class='crm-search-results']//table/tbody/tr[1]/td[8]", 'UNITED STATES');
223
224 $this->checkEditOnEventProfile();
225 }
226
227 /**
228 * CRM-16776 - Check Profile Edit on Events with 'manage event profile' permission.
229 */
230 public function testEventProfilePermission() {
231 $this->webtestLogin();
232
233 //create new role
234 $role = 'role' . substr(sha1(rand()), 0, 7);
235 $this->open($this->sboxPath . "admin/people/permissions/roles");
236
237 $this->waitForElementPresent("edit-add");
238 $this->type("edit-name", $role);
239 $this->click("edit-add");
240 $this->waitForPageToLoad($this->getTimeoutMsec());
241
242 $this->open($this->sboxPath . "admin/people/permissions/roles");
243 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
244 $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
245 $roleId = end($roleId);
246
247 $this->open($this->sboxPath . "admin/people/create");
248 $this->waitForElementPresent("edit-submit");
249 $name = "TestUser" . substr(sha1(rand()), 0, 4);
250 $this->type("edit-name", $name);
251 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
252 $this->type("edit-mail", $emailId);
253 $this->type("edit-pass-pass1", "Test12345");
254 $this->type("edit-pass-pass2", "Test12345");
255 $role = "edit-roles-" . $roleId;
256 $this->check("name=roles[$roleId] value={$roleId}");
257
258 //Add profile Details
259 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
260 $lastName = 'An' . substr(sha1(rand()), 0, 7);
261 $this->type("first_name", $firstName);
262 $this->type("last_name", $lastName);
263
264 $this->click("edit-submit");
265 $this->waitForPageToLoad($this->getTimeoutMsec());
266
267 $permissions = array("edit-{$roleId}-access-civicrm", "edit-{$roleId}-edit-all-events", "edit-{$roleId}-manage-event-profiles");
268 $this->changePermissions($permissions);
269 $this->webtestLogout();
270 $this->webtestLogin($name, 'Test12345');
271 $this->checkEditOnEventProfile();
272 }
273
274 /**
275 * Check Profile Edit on OnlineRegistration Tab
276 */
277 public function checkEditOnEventProfile() {
278 $this->openCiviPage('event/manage/registration', 'reset=1&action=update&id=3');
279 //ensure all the three buttons are displayed
280 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
281 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Copy')]");
282 $this->waitForElementPresent("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div//button[contains(text(), 'Create')]");
283
284 $this->click("xpath=//div[@id='registration_screen']/table[2]/tbody/tr/td[2]/div/div/button[contains(text(), 'Edit')]");
285 $this->waitForAjaxContent();
286 $this->waitForElementPresent("//div[@class='crm-designer-fields-region']");
287 if ($this->isElementPresent("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]")) {
288 $this->click("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]/../../span//a[@title='Remove']");
289 $this->waitForElementNotPresent("xpath=//span[@class='crm-designer-label'][contains(text(), 'City')]");
290 }
291 else {
292 $this->click("xpath=//li[@class='crm-designer-palette-section jstree-closed']/a[contains(text(), 'Individual')]");
293 $this->waitForAjaxContent();
294 $this->doubleClick("xpath=//a[contains(text(), 'Individual')]/../ul//li/a[contains(text(), 'City')]");
295 $this->waitForAjaxContent();
296 }
297 $this->click("xpath=//button/span[contains(text(), 'Save')]");
298 $this->waitForElementPresent("crm-notification-container");
299 $this->assertElementNotContainsText("crm-notification-container", 'API permission check failed for UFGroup/create call; insufficient permission: require administer CiviCRM');
300 $this->click("_qf_Registration_upload-top");
301 $this->waitForTextPresent("'Online Registration' information has been saved.");
302 }
303
304 /**
305 * CRM-16777: Allow to add schedule reminder for event through ACLs 'edit' permission
306 */
307 public function testACLforReminders() {
308 $this->webtestLogin('admin');
309
310 //Details for ACLUser1
311 $ACLrole1 = 'ACLrole1' . substr(sha1(rand()), 0, 7);
312 $ACLUser1 = "ACLUser1" . substr(sha1(rand()), 0, 4);
313 $emailId1 = substr(sha1(rand()), 0, 7) . '@web.com';
314
315 //create ACLrole1 (with 'Access CiviCRM' and 'Access CiviEvent' permissions only).
316 $this->open($this->sboxPath . "admin/people/permissions/roles");
317 $this->type("edit-name", $ACLrole1);
318 $this->waitForElementPresent("edit-add");
319 $this->click("edit-add");
320 $this->waitForPageToLoad($this->getTimeoutMsec());
321 $this->open($this->sboxPath . "admin/people/permissions/roles");
322 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$ACLrole1}']");
323 $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$ACLrole1}']/../td[4]/a[text()='edit permissions']/@href"));
324 $permissions = array(
325 "edit-{$roleId[5]}-access-civicrm",
326 "edit-{$roleId[5]}-access-civievent",
327 );
328 $this->changePermissions($permissions);
329
330 //Create ACLUser1
331 $this->open($this->sboxPath . "admin/people/create");
332 $this->waitForElementPresent("edit-submit");
333 $this->type("edit-name", $ACLUser1);
334 $this->type("edit-mail", $emailId1);
335 $this->type("edit-pass-pass1", "Test12345");
336 $this->type("edit-pass-pass2", "Test12345");
337 $this->click("xpath=//div[@class='form-item form-type-checkboxes form-item-roles']/div//div/label[contains(text(), '{$ACLrole1}')]");
338 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
339 $lastName = 'An' . substr(sha1(rand()), 0, 7);
340 $this->type("first_name", $firstName);
341 $this->type("last_name", $lastName);
342 $this->type("street_address-1", "902C El Camino Way SW");
343 $this->type("city-1", "Dumfries");
344 $this->type("postal_code-1", "1234");
345 $this->select("state_province-1", "value=1019");
346 $this->click("edit-submit");
347 $this->waitForPageToLoad($this->getTimeoutMsec());
348
349 //Create group and add contact.
350 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload-bottom');
351 $groupTitle = 'ACLGroup' . substr(sha1(rand()), 0, 7);
352 $this->type("title", $groupTitle);
353 $this->click("group_type_1");
354 $this->click("_qf_Edit_upload-bottom");
355 $this->waitForElementPresent('_qf_Basic_refresh');
356 $this->type("sort_name", $firstName);
357 $this->click('_qf_Basic_refresh');
358 $this->waitForElementPresent('toggleSelect');
359 $this->click('_qf_Basic_next_action');
360 $this->waitForElementPresent('_qf_AddToGroup_back-bottom');
361 $this->click('_qf_AddToGroup_next-bottom');
362 $this->waitForTextPresent("1 contact added to group");
363
364 //Add the ACLs
365 $this->openCiviPage("admin/options/acl_role", "action=add&reset=1", "_qf_Options_cancel-bottom");
366 $label = "TestAclRole" . substr(sha1(rand()), 0, 4);
367 $this->type("label", $label);
368 $this->type("value", "Acl value" . $label);
369 $this->click("_qf_Options_next-bottom");
370 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
371 $this->waitForAjaxContent();
372 $this->openCiviPage("acl/entityrole", "action=add&reset=1");
373 $this->waitForAjaxContent();
374 $this->select("acl_role_id", "label=" . $label);
375 $this->waitForAjaxContent();
376 $this->select("entity_id", "label={$groupTitle}");
377 $this->clickLink("_qf_EntityRole_next-botttom");
378 $this->openCiviPage("acl", "action=add&reset=1");
379 $this->type("name", "Edit Events{$label}");
380 $this->select("operation", "label=Edit");
381 $this->select("entity_id", "label={$label}");
382 $this->waitForElementPresent("xpath=//tr[@class='crm-acl-form-block-object_type']/td[2]/label[contains(text(), 'Events')]");
383 $this->click("xpath=//tr[@class='crm-acl-form-block-object_type']/td[2]/label[contains(text(), 'Events')]");
384 $this->select("event_id", "label=All Events");
385 $this->clickLink("_qf_ACL_next-bottom");
386 $this->webtestLogout();
387 $this->webtestLogin($ACLUser1, 'Test12345');
388
389 //Add scheduled reminder
390 $this->openCiviPage("event/manage/reminder", "reset=1&action=browse&setTab=1&id=1");
391 $reminderTitle = "Fall Fundraiser Dinner" . substr(sha1(rand()), 0, 4);
392 $this->waitForElementPresent('newScheduleReminder');
393 $this->click("newScheduleReminder");
394 $this->waitForElementPresent("_qf_ScheduleReminders_next-bottom");
395 $this->type("title", $reminderTitle);
396 $this->select('entity', 'label=Registered');
397 $this->select('start_action_offset', 'label=1');
398 $this->select('start_action_condition', 'label=after');
399 $this->click('is_repeat');
400 $this->select('repetition_frequency_interval', 'label=2');
401 $this->select('end_date', 'label=Event End Date');
402 $this->click('recipient');
403 $this->select('recipient', 'label=Participant Role');
404 $subject = 'subject' . substr(sha1(rand()), 0, 4);
405 $this->type('subject', $subject);
406 $this->fillRichTextField("html_message", "This is the test HTML version here!!!", 'CKEditor');
407 $this->type("text_message", "This is the test text version here!!!");
408 $this->click('_qf_ScheduleReminders_next-bottom');
409 $this->webtestLogout();
410
411 //Disable the ACLs
412 $this->webtestLogin('admin');
413 $this->openCiviPage("acl", "reset=1");
414 $this->waitForAjaxContent();
415 $this->click("xpath=//div[contains(text(), 'Edit Events{$label}')]/../../td[7]/span/a[2][contains(text(), 'Disable')]");
416 $this->waitForTextPresent("Are you sure you want to disable this ACL?");
417 $this->click("xpath=//button//span[contains(text(), 'Yes')]");
418
419 //Login with same test-user created above
420 $this->webtestLogin($ACLUser1, 'Test12345');
421 $this->openCiviPage("event/manage", "reset=1");
422 $this->waitForElementPresent("xpath=//div[@id='event_status_id']/div[@class='dataTables_wrapper no-footer']");
423 $this->verifyText("xpath=//div[@id='event_status_id']/div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td", "None found.");
424 }
425
426 }