3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_entity_tag_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Core
36 * Class api_v3_EntityTagTest.
38 * This test class was introduced to ensure that the fix for CRM-17350 (reducing the required permission
39 * from edit all contacts to has right to edit this contact) would not result in inappropriate permission opening on
40 * other entities. Other entities are still too restricted but that is a larger job.
43 class api_v3_EntityTagACLTest
extends CiviUnitTestCase
{
50 protected $_apiversion = 3;
53 * Entity being tested.
57 protected $_entity = 'entity_tag';
60 * Set up permissions for test.
62 public function setUp() {
63 $this->useTransaction(TRUE);
65 $individualID = $this->individualCreate();
66 $daoObj = new CRM_Core_DAO();
67 $this->callAPISuccess('Attachment', 'create', array(
68 'entity_table' => 'civicrm_contact',
69 'entity_id' => $individualID,
74 $daoObj->createTestObject('CRM_Activity_BAO_Activity', array(), 1, 0);
75 $daoObj->createTestObject('CRM_Case_BAO_Case', array(), 1, 0);
76 $entities = $this->getTagOptions();
77 foreach ($entities as $key => $entity) {
78 $this->callAPISuccess('Tag', 'create', array(
81 'description' => $entity,
85 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM');
89 * Get the options for the used_for fields.
93 public function getTagOptions() {
94 $options = $this->callAPISuccess('Tag', 'getoptions', array('field' => 'used_for'));
95 return $options['values'];
99 * Get the entity table for a tag label.
101 * @param string $entity
105 protected function getTableForTag($entity) {
106 $options = $this->getTagOptions();
107 return array_search($entity, $options);
110 * Get entities which can be tagged in data provider format.
112 public function taggableEntities() {
114 foreach ($this->getTagOptions() as $entity) {
115 $return[] = array($entity);
121 * This test checks that users with edit all contacts can edit all tags.
123 * @dataProvider taggableEntities
125 * We are looking to see that a contact with edit all contacts can still add all tags (for all
126 * tag entities since that was how it was historically and we are not fixing non-contact entities).
128 * @param string $entity
131 public function testThatForEntitiesEditAllContactsCanAddTags($entity) {
133 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('edit all contacts', 'access CiviCRM');
134 $this->callAPISuccess('EntityTag', 'create', array(
137 'check_permissions' => TRUE,
138 'entity_table' => $this->getTableForTag($entity),
140 $this->callAPISuccessGetCount('EntityTag', array(
142 'entity_table' => $this->getTableForTag($entity),
147 * This test checks that an ACL or edit all contacts is required to be able to create a contact.
149 * @dataProvider taggableEntities
151 public function testThatForEntityWithoutACLOrEditAllThereIsNoAccess($entity) {
153 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'view all contacts');
154 $this->callAPISuccess('EntityTag', 'create', array(
157 'check_permissions' => TRUE,
158 'entity_table' => $this->getTableForTag($entity),
160 $this->callAPISuccessGetCount('EntityTag', array(
162 'entity_table' => $this->getTableForTag($entity),
167 * This test checks that permissions are not applied when check_permissions is off.
169 * @dataProvider taggableEntities
171 * @param string $entity
174 public function testCheckPermissionsOffWorks($entity) {
176 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'view all contacts');
177 $result = $this->callAPISuccess('EntityTag', 'create', array(
180 'check_permissions' => 0,
181 'entity_table' => $this->getTableForTag($entity),
183 $this->assertEquals(1, $result['added']);
184 $this->callAPISuccessGetCount('EntityTag', array(
186 'entity_table' => $this->getTableForTag($entity),
187 'check_permissions' => 0,
192 * This test checks ACLs can be used to control who can edit a contact.
194 * Note that for other entities this hook will not allow them to edit the entity_tag and they still need
195 * edit all contacts (pending a more extensive fix).
197 * @dataProvider taggableEntities
199 * @param string $entity
202 public function testThatForEntitiesACLApplies($entity) {
204 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'view all contacts');
205 $this->hookClass
->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
206 $this->callAPISuccess('EntityTag', 'create', array(
209 'entity_table' => $this->getTableForTag($entity),
210 'check_permissions' => TRUE,
212 $this->callAPISuccessGetCount('EntityTag', array(
214 'entity_table' => $this->getTableForTag($entity),
215 ), ($entity == 'Contacts' ?
1 : 0));
219 * All results returned.
221 * @implements CRM_Utils_Hook::aclWhereClause
223 * @param string $type
224 * @param array $tables
225 * @param array $whereTables
226 * @param int $contactID
227 * @param string $where
229 public function aclWhereHookAllResults($type, &$tables, &$whereTables, &$contactID, &$where) {