Merge pull request #4820 from kurund/CRM-15705
[civicrm-core.git] / tests / phpunit / WebTest / ACL / AssignUsersToRolesTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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->click("_qf_Options_next-bottom");
56
57 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
58
59 $this->openCiviPage("acl/entityrole", "action=add&reset=1");
60
61 $this->select("acl_role_id", "label=" . $label);
62 $this->select("entity_id", "label={$groupTitle}");
63
64 $this->clickLink("_qf_EntityRole_next-botttom");
65
66 $this->openCiviPage("acl", "action=add&reset=1");
67 $this->click("group_id");
68 $this->select("group_id", "label={$groupTitle}");
69 $this->select("operation", "label=View");
70 $this->select("entity_id", "label={$label}");
71 $this->type("name", "describe {$label}");
72 $this->clickLink("_qf_ACL_next-bottom");
73 }
74
75 public function testACLforSmartGroups() {
76 $this->webtestLogin();
77
78 //Create role
79 $role = 'role' . substr(sha1(rand()), 0, 7);
80 $this->open($this->sboxPath . "admin/people/permissions/roles");
81 $this->waitForElementPresent("edit-submit");
82 $this->type("edit-name", $role);
83 $this->click("edit-add");
84 $this->waitForPageToLoad($this->getTimeoutMsec());
85 $this->open($this->sboxPath . "admin/people/permissions/roles");
86 $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']");
87 $roleURL = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href"));
88 $roleId = end($roleURL);
89
90 //create user with roleId
91 $this->open($this->sboxPath . "admin/people/create");
92 $this->waitForElementPresent("edit-submit");
93 $user = "TestUser" . substr(sha1(rand()), 0, 4);
94 $this->type("edit-name", $user);
95 $emailId = substr(sha1(rand()), 0, 7) . '@web.com';
96 $this->type("edit-mail", $emailId);
97 $this->type("edit-pass-pass1", "Test12345");
98 $this->type("edit-pass-pass2", "Test12345");
99 $role = "edit-roles-" . $roleId;
100 $this->check("name=roles[$roleId] value={$roleId}");
101 $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
102 $lastName = 'An' . substr(sha1(rand()), 0, 7);
103 $this->type("first_name", $firstName);
104 $this->type("last_name", $lastName);
105 $this->click("edit-submit");
106 $this->waitForPageToLoad($this->getTimeoutMsec());
107 $permissions = array("edit-{$roleId}-access-civicrm");
108 $this->changePermissions($permissions);
109
110 //Create group and add your user's contact to that group
111 $this->openCiviPage("group/add", "reset=1");
112 $groupTitle = "testGroup" . substr(sha1(rand()), 0, 4);
113 $this->type("title", $groupTitle);
114 $this->click("group_type[1]");
115 $this->click("_qf_Edit_upload-bottom");
116 $this->waitForPageToLoad($this->getTimeoutMsec());
117 $this->waitForText('crm-notification-container', "The Group '{$groupTitle}' has been saved.");
118 $this->waitForElementPresent("_qf_Basic_refresh");
119 $this->type('sort_name', $firstName);
120 $this->click('_qf_Basic_refresh');
121 $this->waitForPageToLoad($this->getTimeoutMsec());
122 $this->waitForElementPresent("_qf_Basic_next_action");
123 $this->assertTrue($this->isElementPresent("xpath=//table/tbody//tr/td[3]/a[text()='{$lastName}, {$firstName}']"));
124 $this->click("xpath=//table/tbody//tr/td[1]/input[@type='checkbox']");
125 $this->click('_qf_Basic_next_action');
126 $this->waitForElementPresent("_qf_AddToGroup_back-bottom");
127 $this->click('_qf_AddToGroup_next-bottom');
128 $this->waitForText('crm-notification-container', "1 contact added to group");
129
130 //create Smart Group
131 $this->openCiviPage('contact/search/advanced', 'reset=1');
132 $this->click("location");
133 $this->waitForElementPresent("country");
134 $this->select("country", "United States");
135 $this->clickLink("_qf_Advanced_refresh");
136 $this->waitForElementPresent("task");
137 $this->click('radio_ts', 'ts_all');
138 $this->click('task');
139 $this->select('task', 'label=New Smart Group');
140 $this->waitForPageToLoad($this->getTimeoutMsec());
141 $smartGroupTitle = "SmartGroup" . substr(sha1(rand()), 0, 4);
142 $this->type("title", $smartGroupTitle);
143 $this->clickLink("_qf_SaveSearch_next-bottom");
144 $this->waitForText('crm-notification-container', "Your smart group has been saved as \'$smartGroupTitle\'");
145
146 //Create ACL role
147 $this->openCiviPage("admin/options/acl_role", "action=add&reset=1", "_qf_Options_cancel-bottom");
148 $label = "TestAclRole" . substr(sha1(rand()), 0, 4);
149 $this->type("label", $label);
150 $this->click("_qf_Options_next-bottom");
151 $this->waitForText('crm-notification-container', "The ACL Role '{$label}' has been saved.");
152
153 // Assign group to ACL role created
154 $this->openCiviPage("acl/entityrole", "action=add&reset=1");
155 $this->select("acl_role_id", "label=" . $label);
156 $this->select("entity_id", "label={$groupTitle}");
157 $this->clickLink("_qf_EntityRole_next-botttom");
158
159 //Create ACL granting 'Edit' access on smart group to the role
160 $this->openCiviPage("acl", "action=add&reset=1");
161 $this->click("group_id");
162 $this->select("group_id", "label={$smartGroupTitle}");
163 $this->select("operation", "label=Edit");
164 $this->select("entity_id", "label={$label}");
165 $this->type("name", "describe {$label}");
166 $this->clickLink("_qf_ACL_next-bottom");
167
168 //Login as your role user and do Find Contacts
169 $this->webtestLogin($user, 'Test12345');
170 $this->openCiviPage('contact/search/advanced', 'reset=1');
171 $this->click("location");
172 $this->waitForElementPresent("country");
173 $this->select("country", "United States");
174 $this->clickLink("_qf_Advanced_refresh");
175 $this->waitForElementPresent("xpath=//div[@class='crm-search-results']");
176 $this->assertElementNotContainsText("xpath=//form[@id='Advanced']/div[3]/div/div", "No matches found for");
177 $this->assertElementContainsText("xpath=//div[@class='crm-search-results']//table/tbody/tr[1]/td[8]", 'United States');
178 }
179 }