| 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_Report_RolePermissionReportTest |
| 31 | */ |
| 32 | class WebTest_Report_RolePermissionReportTest extends CiviSeleniumTestCase { |
| 33 | |
| 34 | protected function setUp() { |
| 35 | parent::setUp(); |
| 36 | } |
| 37 | |
| 38 | public function testRolePermissionReport() { |
| 39 | $this->webtestLogin('admin'); |
| 40 | |
| 41 | //create new roles |
| 42 | $role1 = 'role1' . substr(sha1(rand()), 0, 7); |
| 43 | $role2 = 'role2' . substr(sha1(rand()), 0, 7); |
| 44 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 45 | $this->waitForElementPresent("edit-add"); |
| 46 | $this->type("edit-name", $role1); |
| 47 | $this->click("edit-add"); |
| 48 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 49 | |
| 50 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 51 | $this->waitForElementPresent("edit-add"); |
| 52 | $this->type("edit-name", $role2); |
| 53 | $this->click("edit-add"); |
| 54 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 55 | |
| 56 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 57 | |
| 58 | $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role1}']"); |
| 59 | $roleid = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role1}']/../td[4]/a[text()='edit permissions']/@href")); |
| 60 | $roleId1 = end($roleid); |
| 61 | $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role2}']"); |
| 62 | $roleid = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role2}']/../td[4]/a[text()='edit permissions']/@href")); |
| 63 | $roleId2 = end($roleid); |
| 64 | |
| 65 | $user1 = $this->_testCreateUser($roleId1); |
| 66 | $user2 = $this->_testCreateUser($roleId2); |
| 67 | |
| 68 | // let's give full CiviReport permissions. |
| 69 | $permissions = array( |
| 70 | "edit-2-access-civireport", |
| 71 | "edit-2-view-all-contacts", |
| 72 | "edit-2-administer-civicrm", |
| 73 | "edit-2-access-civicrm", |
| 74 | ); |
| 75 | $this->changePermissions($permissions); |
| 76 | |
| 77 | // change report setting to for a particular role |
| 78 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 79 | $this->click("xpath=//div[@id='mainTabContainer']/ul/li[6]/a"); |
| 80 | $this->waitForElementPresent("_qf_Summary_submit_save"); |
| 81 | $this->select("permission", "value=access CiviCRM"); |
| 82 | $this->select("grouprole-f", "value=$role1"); |
| 83 | $this->click("add"); |
| 84 | $this->click("_qf_Summary_submit_save"); |
| 85 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 86 | $this->webtestLogin($user2, 'Test12345'); |
| 87 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 88 | $this->assertElementContainsText('crm-container', 'You do not have permission to access this report.'); |
| 89 | $this->openCiviPage('report/list', 'reset=1'); |
| 90 | |
| 91 | //delete roles |
| 92 | $this->webtestLogin('admin'); |
| 93 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 94 | $this->_roleDelete($role1); |
| 95 | $this->_roleDelete($role2); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | *check for CRM-10148 |
| 100 | */ |
| 101 | public function testReservedReportPermission() { |
| 102 | $this->webtestLogin('admin'); |
| 103 | |
| 104 | //create new role |
| 105 | $role = 'role' . substr(sha1(rand()), 0, 7); |
| 106 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 107 | |
| 108 | $this->waitForElementPresent("edit-add"); |
| 109 | $this->type("edit-name", $role); |
| 110 | $this->click("edit-add"); |
| 111 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 112 | |
| 113 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 114 | $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']"); |
| 115 | $roleId = explode('/', $this->getAttribute("xpath=//table[@id='user-roles']/tbody//tr/td[1][text()='{$role}']/../td[4]/a[text()='edit permissions']/@href")); |
| 116 | $roleId = end($roleId); |
| 117 | $user = $this->_testCreateUser($roleId); |
| 118 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 119 | if ($this->isChecked("is_reserved")) { |
| 120 | $this->click("is_reserved"); |
| 121 | $this->click("_qf_Summary_submit_save"); |
| 122 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 123 | } |
| 124 | $permissions = array( |
| 125 | "edit-{$roleId}-access-civireport", |
| 126 | "edit-{$roleId}-view-all-contacts", |
| 127 | "edit-{$roleId}-administer-reports", |
| 128 | "edit-{$roleId}-access-civicrm", |
| 129 | ); |
| 130 | $this->changePermissions($permissions); |
| 131 | |
| 132 | $this->webtestLogin($user, 'Test12345'); |
| 133 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 134 | |
| 135 | //check if the reserved report field is frozen |
| 136 | $this->assertTrue($this->isElementPresent("xpath=//div[@id='report-tab-access']/table/tbody//tr/td[2]/span/tt[text()='[ ]']")); |
| 137 | |
| 138 | // let's give full CiviReport permissions. |
| 139 | $permissions = array( |
| 140 | "edit-{$roleId}-access-civireport", |
| 141 | "edit-{$roleId}-view-all-contacts", |
| 142 | "edit-{$roleId}-administer-reports", |
| 143 | "edit-{$roleId}-access-civicrm", |
| 144 | "edit-{$roleId}-administer-reserved-reports", |
| 145 | ); |
| 146 | $this->changePermissions($permissions); |
| 147 | |
| 148 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 149 | |
| 150 | //make the report reserved |
| 151 | $this->click("is_reserved"); |
| 152 | $this->click("_qf_Summary_submit_save"); |
| 153 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 154 | |
| 155 | $this->webtestLogin($user, 'Test12345'); |
| 156 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 157 | |
| 158 | //check if the report criteria and settings is accessible |
| 159 | $this->assertTrue($this->isElementPresent("xpath=//div[@id='report-tab-col-groups']/table/tbody//tr/td[3]/input[@id='fields_email']")); |
| 160 | $this->assertTrue($this->isElementPresent("xpath=//div[@id='report-tab-format']/table/tbody//tr/td[2]/input[@id='title']")); |
| 161 | |
| 162 | //login as admin and remove reserved permission |
| 163 | $this->webtestLogin('admin'); |
| 164 | $this->open($this->sboxPath . "admin/people/permissions"); |
| 165 | $this->waitForElementPresent("edit-submit"); |
| 166 | |
| 167 | if ($this->isChecked("edit-2-administer-reserved-reports")) { |
| 168 | $this->click("edit-2-administer-reserved-reports"); |
| 169 | } |
| 170 | else { |
| 171 | $this->click("edit-{$roleId}-administer-reserved-reports"); |
| 172 | } |
| 173 | $this->click("edit-submit"); |
| 174 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 175 | |
| 176 | //login as user and check for absence of report criteria and settings |
| 177 | $this->webtestLogin($user, 'Test12345'); |
| 178 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 179 | |
| 180 | if ($this->isElementPresent("xpath=//div[@id='mainTabContainer']/ul/li")) { |
| 181 | $this->verifyNotText("xpath=//div[@id='mainTabContainer']/ul/li/a", "Columns"); |
| 182 | } |
| 183 | if ($this->isElementPresent("xpath=//li[@id='tab_settings']")) { |
| 184 | $this->verifyNotText("xpath=//li[@id='tab_settings']/a", "Title and Format"); |
| 185 | } |
| 186 | |
| 187 | $this->assertFalse($this->isElementPresent("xpath=//div[@id='report-tab-format']/table/tbody//tr/td[2]/input[@id='title']")); |
| 188 | |
| 189 | //login as admin and turn the is_reserved flag off for the instance |
| 190 | $this->webtestLogin('admin'); |
| 191 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 192 | $this->click("is_reserved"); |
| 193 | $this->click("_qf_Summary_submit_save"); |
| 194 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 195 | |
| 196 | $this->webtestLogin($user, 'Test12345'); |
| 197 | $this->openCiviPage('report/instance/1', 'reset=1'); |
| 198 | |
| 199 | $this->assertTrue($this->isElementPresent("xpath=//div[@id='report-tab-col-groups']/table/tbody//tr/td[3]/input[@id='fields_email']")); |
| 200 | $this->assertTrue($this->isElementPresent("xpath=//div[@id='report-tab-format']/table/tbody//tr/td[2]/input[@id='title']")); |
| 201 | |
| 202 | //login as admin and delete the role |
| 203 | $this->webtestLogin('admin'); |
| 204 | $this->open($this->sboxPath . "admin/people/permissions/roles"); |
| 205 | $this->_roleDelete($role); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param $role |
| 210 | */ |
| 211 | public function _roleDelete($role) { |
| 212 | $this->waitForElementPresent("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']"); |
| 213 | $this->click("xpath=//table[@id='user-roles']/tbody//tr/td[text()='{$role}']/..//td/a[text()='edit role']"); |
| 214 | $this->waitForElementPresent('edit-delete'); |
| 215 | $this->click('edit-delete'); |
| 216 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 217 | $this->click("edit-submit"); |
| 218 | $this->waitForTextPresent("The role has been deleted."); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param int $roleid |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function _testCreateUser($roleid) { |
| 227 | |
| 228 | $this->open($this->sboxPath . "admin/people/create"); |
| 229 | |
| 230 | $this->waitForElementPresent("edit-submit"); |
| 231 | |
| 232 | $name = "TestUser" . substr(sha1(rand()), 0, 4); |
| 233 | $this->type("edit-name", $name); |
| 234 | |
| 235 | $emailId = substr(sha1(rand()), 0, 7) . '@web.com'; |
| 236 | $this->type("edit-mail", $emailId); |
| 237 | $this->type("edit-pass-pass1", "Test12345"); |
| 238 | $this->type("edit-pass-pass2", "Test12345"); |
| 239 | $role = "edit-roles-" . $roleid; |
| 240 | $this->check("name=roles[$roleid] value={$roleid}"); |
| 241 | |
| 242 | //Add profile Details |
| 243 | $firstName = 'Ma' . substr(sha1(rand()), 0, 4); |
| 244 | $lastName = 'An' . substr(sha1(rand()), 0, 7); |
| 245 | |
| 246 | $this->type("first_name", $firstName); |
| 247 | $this->type("last_name", $lastName); |
| 248 | |
| 249 | //Address Details |
| 250 | $this->type("street_address-1", "902C El Camino Way SW"); |
| 251 | $this->type("city-1", "Dumfries"); |
| 252 | $this->type("postal_code-1", "1234"); |
| 253 | $this->select("state_province-1", "value=1019"); |
| 254 | |
| 255 | $this->click("edit-submit"); |
| 256 | $this->waitForPageToLoad($this->getTimeoutMsec()); |
| 257 | return $name; |
| 258 | } |
| 259 | } |