3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * This class is intended to test ACL permission using the multisite module
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
36 class api_v3_ACLPermissionTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 public $DBResetRequired = FALSE;
40 protected $allowedContactId = 0;
42 public function setUp() {
44 $baoObj = new CRM_Core_DAO();
45 $baoObj->createTestObject('CRM_Pledge_BAO_Pledge', array(), 1, 0);
46 $baoObj->createTestObject('CRM_Core_BAO_Phone', array(), 1, 0);
47 $config = CRM_Core_Config
::singleton();
48 $config->userPermissionClass
->permissions
= array();
53 * @see CiviUnitTestCase::tearDown()
55 public function tearDown() {
56 CRM_Utils_Hook
::singleton()->reset();
57 $tablesToTruncate = array(
59 'civicrm_group_contact',
63 'civicrm_acl_entity_role',
64 'civicrm_acl_contact_cache',
65 'civicrm_contribution',
66 'civicrm_participant',
69 $this->quickCleanup($tablesToTruncate);
70 $config = CRM_Core_Config
::singleton();
71 unset($config->userPermissionClass
->permissions
);
75 * Function tests that an empty where hook returns no results.
77 public function testContactGetNoResultsHook() {
78 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
79 $result = $this->callAPISuccess('contact', 'get', array(
80 'check_permissions' => 1,
81 'return' => 'display_name',
83 $this->assertEquals(0, $result['count']);
87 * Function tests that an empty where hook returns exactly 1 result with "view my contact".
89 * CRM-16512 caused contacts with Edit my contact to be able to view all records.
91 public function testContactGetOneResultHookWithViewMyContact() {
92 $this->createLoggedInUser();
93 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
94 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'view my contact');
95 $result = $this->callAPISuccess('contact', 'get', array(
96 'check_permissions' => 1,
97 'return' => 'display_name',
99 $this->assertEquals(1, $result['count']);
103 * Function tests that a user with "edit my contact" can edit themselves.
105 public function testContactEditHookWithEditMyContact() {
106 $cid = $this->createLoggedInUser();
107 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
108 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'edit my contact');
109 $this->callAPISuccess('contact', 'create', array(
110 'check_permissions' => 1,
116 * Ensure contact permissions extend to related entities like email
118 public function testRelatedEntityPermissions() {
119 $this->createLoggedInUser();
120 $disallowedContact = $this->individualCreate(array(), 0);
121 $this->allowedContactId
= $this->individualCreate(array(), 1);
122 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlyOne'));
123 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
124 $testEntities = array(
125 'Email' => array('email' => 'null@nothing', 'location_type_id' => 1),
126 'Phone' => array('phone' => '123456', 'location_type_id' => 1),
127 'IM' => array('name' => 'hello', 'location_type_id' => 1),
128 'Website' => array('url' => 'http://test'),
129 'Address' => array('street_address' => '123 Sesame St.', 'location_type_id' => 1),
131 foreach ($testEntities as $entity => $params) {
133 'contact_id' => $disallowedContact,
134 'check_permissions' => 1,
136 // We should be prevented from getting or creating entities for a contact we don't have permission for
137 $this->callAPIFailure($entity, 'create', $params);
138 $results = $this->callAPISuccess($entity, 'get', array('contact_id' => $disallowedContact, 'check_permissions' => 1));
139 $this->assertEquals(0, $results['count']);
141 // We should be allowed to create and get for contacts we do have permission on
142 $params['contact_id'] = $this->allowedContactId
;
143 $this->callAPISuccess($entity, 'create', $params);
144 $results = $this->callAPISuccess($entity, 'get', array('contact_id' => $this->allowedContactId
, 'check_permissions' => 1));
145 $this->assertGreaterThan(0, $results['count']);
150 * Function tests all results are returned.
152 public function testContactGetAllResultsHook() {
153 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
154 $result = $this->callAPISuccess('contact', 'get', array(
155 'check_permissions' => 1,
156 'return' => 'display_name',
159 $this->assertEquals(2, $result['count']);
163 * Function tests that deleted contacts are not returned.
165 public function testContactGetPermissionHookNoDeleted() {
166 $this->callAPISuccess('contact', 'create', array('id' => 2, 'is_deleted' => 1));
167 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
168 $result = $this->callAPISuccess('contact', 'get', array(
169 'check_permissions' => 1,
170 'return' => 'display_name',
172 $this->assertEquals(1, $result['count']);
176 * Test permissions limited by hook.
178 public function testContactGetHookLimitingHook() {
179 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond'));
181 $result = $this->callAPISuccess('contact', 'get', array(
182 'check_permissions' => 1,
183 'return' => 'display_name',
185 $this->assertEquals(1, $result['count']);
189 * Confirm that without check permissions we still get 2 contacts returned.
191 public function testContactGetHookLimitingHookDontCheck() {
192 $result = $this->callAPISuccess('contact', 'get', array(
193 'check_permissions' => 0,
194 'return' => 'display_name',
196 $this->assertEquals(2, $result['count']);
200 * Check that id works as a filter.
202 public function testContactGetIDFilter() {
203 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
204 $result = $this->callAPISuccess('contact', 'get', array(
207 'check_permissions' => 1,
210 $this->assertEquals(1, $result['count']);
211 $this->assertEquals(2, $result['id']);
215 * Check that address IS returned.
217 public function testContactGetAddressReturned() {
218 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond'));
219 $fullresult = $this->callAPISuccess('contact', 'get', array(
222 //return doesn't work for all keys - can't fix that here so let's skip ...
223 //prefix & suffix are inconsistent due to CRM-7929
224 // unsure about others but return doesn't work on them
225 $elementsReturnDoesntSupport = array(
236 $expectedReturnElements = array_diff(array_keys($fullresult['values'][0]), $elementsReturnDoesntSupport);
237 $result = $this->callAPISuccess('contact', 'get', array(
238 'check_permissions' => 1,
239 'return' => $expectedReturnElements,
242 $this->assertEquals(1, $result['count']);
243 foreach ($expectedReturnElements as $element) {
244 $this->assertArrayHasKey($element, $result['values'][0]);
249 * Check that pledge IS not returned.
251 public function testContactGetPledgeIDNotReturned() {
252 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
253 $this->callAPISuccess('contact', 'get', array(
256 $result = $this->callAPISuccess('contact', 'get', array(
257 'check_permissions' => 1,
258 'return' => 'pledge_id',
261 $this->assertArrayNotHasKey('pledge_id', $result['values'][0]);
265 * Check that pledge IS not an allowable filter.
267 public function testContactGetPledgeIDNotFiltered() {
268 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
269 $this->callAPISuccess('contact', 'get', array(
272 $result = $this->callAPISuccess('contact', 'get', array(
273 'check_permissions' => 1,
277 $this->assertEquals(2, $result['count']);
281 * Check that chaining doesn't bypass permissions
283 public function testContactGetPledgeNotChainable() {
284 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereOnlySecond'));
285 $this->callAPISuccess('contact', 'get', array(
288 $this->callAPIFailure('contact', 'get', array(
289 'check_permissions' => 1,
290 'api.pledge.get' => 1,
293 'Error in call to pledge_get : API permission check failed for pledge/get call; missing permission: access CiviCRM.'
297 public function setupCoreACL() {
298 $this->createLoggedInUser();
299 $this->_permissionedDisabledGroup
= $this->groupCreate(array(
300 'title' => 'pick-me-disabled',
302 'name' => 'pick-me-disabled',
304 $this->_permissionedGroup
= $this->groupCreate(array(
305 'title' => 'pick-me-active',
307 'name' => 'pick-me-active',
313 * @dataProvider entities
314 * confirm that without check permissions we still get 2 contacts returned
317 public function testEntitiesGetHookLimitingHookNoCheck($entity) {
318 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array();
319 $this->setUpEntities($entity);
320 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
321 $result = $this->callAPISuccess($entity, 'get', array(
322 'check_permissions' => 0,
323 'return' => 'contact_id',
325 $this->assertEquals(2, $result['count']);
329 * @dataProvider entities
330 * confirm that without check permissions we still get 2 entities returned
333 public function testEntitiesGetCoreACLLimitingHookNoCheck($entity) {
334 $this->setupCoreACL();
335 //CRM_Core_Config::singleton()->userPermissionClass->permissions = array();
336 $this->setUpEntities($entity);
337 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
338 $result = $this->callAPISuccess($entity, 'get', array(
339 'check_permissions' => 0,
340 'return' => 'contact_id',
342 $this->assertEquals(2, $result['count']);
346 * @dataProvider entities
347 * confirm that with check permissions we don't get entities
349 * @throws \PHPUnit_Framework_IncompleteTestError
351 public function testEntitiesGetCoreACLLimitingCheck($entity) {
352 $this->markTestIncomplete('this does not work in 4.4 but can be enabled in 4.5 or a security release of 4.4 including the important security fix CRM-14877');
353 $this->setupCoreACL();
354 $this->setUpEntities($entity);
355 $result = $this->callAPISuccess($entity, 'get', array(
356 'check_permissions' => 1,
357 'return' => 'contact_id',
359 $this->assertEquals(0, $result['count']);
363 * @dataProvider entities
364 * Function tests that an empty where hook returns no results
365 * @param string $entity
366 * @throws \PHPUnit_Framework_IncompleteTestError
368 public function testEntityGetNoResultsHook($entity) {
369 $this->markTestIncomplete('hook acls only work with contacts so far');
370 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array();
371 $this->setUpEntities($entity);
372 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookNoResults'));
373 $result = $this->callAPISuccess($entity, 'get', array(
374 'check_permission' => 1,
376 $this->assertEquals(0, $result['count']);
382 public static function entities() {
383 return array(array('contribution'), array('participant'));// @todo array('pledge' => 'pledge')
390 public function setUpEntities($entity) {
391 $baoObj = new CRM_Core_DAO();
392 $baoObj->createTestObject(_civicrm_api3_get_BAO($entity), array(), 2, 0);
393 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array(
395 'access CiviContribute',
397 'view event participants',
402 * No results returned.
404 * @implements CRM_Utils_Hook::aclWhereClause
406 * @param string $type
407 * @param array $tables
408 * @param array $whereTables
409 * @param int $contactID
410 * @param string $where
412 public function aclWhereHookNoResults($type, &$tables, &$whereTables, &$contactID, &$where) {
416 * All results returned.
418 * @implements CRM_Utils_Hook::aclWhereClause
420 * @param string $type
421 * @param array $tables
422 * @param array $whereTables
423 * @param int $contactID
424 * @param string $where
426 public function aclWhereHookAllResults($type, &$tables, &$whereTables, &$contactID, &$where) {
431 * All but first results returned.
432 * @implements CRM_Utils_Hook::aclWhereClause
435 * @param $whereTables
439 public function aclWhereOnlySecond($type, &$tables, &$whereTables, &$contactID, &$where) {
440 $where = " contact_a.id > 1";
444 * Only specified contact returned.
445 * @implements CRM_Utils_Hook::aclWhereClause
448 * @param $whereTables
452 public function aclWhereOnlyOne($type, &$tables, &$whereTables, &$contactID, &$where) {
453 $where = " contact_a.id = " . $this->allowedContactId
;