CRM-13744 permissions allow 'or' permissions - prevents regression on event registrat...
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_entity_tag_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
36 */
37
38 require_once 'CiviTest/CiviUnitTestCase.php';
39 class api_v3_EntityTagTest extends CiviUnitTestCase {
40
41 protected $_individualID;
42 protected $_householdID;
43 protected $_organizationID;
44 protected $_tagID;
45 protected $_apiversion = 3;
46 protected $_tag;
47 protected $_entity = 'entity_tag';
48 public $_eNoticeCompliant = TRUE;
49
50 function setUp() {
51 parent::setUp();
52
53 $this->_individualID = $this->individualCreate();
54 $this->_tag = $this->tagCreate();
55 $this->_tagID = $this->_tag['id'];
56 $this->_householdID = $this->houseHoldCreate();
57 $this->_organizationID = $this->organizationCreate();
58 }
59
60 function tearDown() {
61 $this->quickCleanup(array('civicrm_tag', 'civicrm_entity_tag'));
62 }
63
64 function testAddEmptyParams() {
65 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params = array(),
66 'contact_id is a required field'
67 );
68 }
69
70 function testAddWithoutTagID() {
71 $params = array(
72 'contact_id' => $this->_individualID,
73 );
74 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
75 'tag_id is a required field'
76 );
77 }
78
79 function testAddWithoutContactID() {
80 $params = array(
81 'tag_id' => $this->_tagID,
82 );
83 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
84 'contact_id is a required field');
85 }
86
87 function testContactEntityTagCreate() {
88 $params = array(
89 'contact_id' => $this->_individualID,
90 'tag_id' => $this->_tagID,
91 );
92
93 $result = $this->callAPIAndDocument('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
94 $this->assertEquals($result['added'], 1);
95 }
96
97 function testAddDouble() {
98 $individualId = $this->_individualID;
99 $organizationId = $this->_organizationID;
100 $tagID = $this->_tagID;
101 $params = array(
102 'contact_id' => $individualId,
103 'tag_id' => $tagID,
104 );
105
106 $result = $this->callAPISuccess('entity_tag', 'create', $params);
107
108 $this->assertEquals($result['added'], 1);
109
110 $params = array(
111 'contact_id_i' => $individualId,
112 'contact_id_o' => $organizationId,
113 'tag_id' => $tagID,
114 );
115
116 $result = $this->callAPISuccess('entity_tag', 'create', $params);
117 $this->assertEquals($result['added'], 1);
118 $this->assertEquals($result['not_added'], 1);
119 }
120
121 ///////////////// civicrm_entity_tag_get methods
122 function testGetNoEntityID() {
123 $ContactId = $this->_individualID;
124 $tagID = $this->_tagID;
125 $params = array(
126 'contact_id' => $ContactId,
127 'tag_id' => $tagID,
128 );
129
130 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
131 $this->assertEquals($individualEntity['added'], 1);
132 $result = $this->callAPISuccess($this->_entity, 'get', array('sequential' => 1, 'tag_id' => $tagID));
133 $this->assertEquals($ContactId, $result['values'][0]['entity_id']);
134 }
135
136 function testIndividualEntityTagGet() {
137 $contactId = $this->_individualID;
138 $tagID = $this->_tagID;
139 $params = array(
140 'contact_id' => $contactId,
141 'tag_id' => $tagID,
142 );
143
144 $individualEntity = $this->callAPIAndDocument('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
145 $this->assertEquals($individualEntity['added'], 1);
146
147 $paramsEntity = array(
148 'contact_id' => $contactId,
149 );
150 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
151 }
152
153 function testHouseholdEntityGet() {
154 $ContactId = $this->_householdID;
155 $tagID = $this->_tagID;
156 $params = array(
157 'contact_id' => $ContactId,
158 'tag_id' => $tagID,
159 );
160
161 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
162 $this->assertEquals($householdEntity['added'], 1);
163 }
164
165 function testOrganizationEntityGet() {
166 $ContactId = $this->_organizationID;
167 $tagID = $this->_tagID;
168 $params = array(
169 'contact_id' => $ContactId,
170 'tag_id' => $tagID,
171 );
172
173 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
174 $this->assertEquals($organizationEntity['added'], 1);
175
176 $paramsEntity = array('contact_id' => $ContactId);
177 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
178 }
179
180 ///////////////// civicrm_entity_tag_Delete methods
181 function testEntityTagDeleteNoTagId() {
182 $entityTagParams = array(
183 'contact_id_i' => $this->_individualID,
184 'contact_id_h' => $this->_householdID,
185 'tag_id' => $this->_tagID,
186 );
187 $this->entityTagAdd($entityTagParams);
188
189 $params = array(
190 'contact_id_i' => $this->_individualID,
191 'contact_id_h' => $this->_householdID,
192 );
193
194 $result = $this->callAPIFailure('entity_tag', 'delete', $params,
195 'tag_id is a required field'
196 );
197 }
198
199 function testEntityTagDeleteINDHH() {
200 $entityTagParams = array(
201 'contact_id_i' => $this->_individualID,
202 'contact_id_h' => $this->_householdID,
203 'tag_id' => $this->_tagID,
204 );
205 $this->entityTagAdd($entityTagParams);
206
207 $params = array(
208 'contact_id_i' => $this->_individualID,
209 'contact_id_h' => $this->_householdID,
210 'tag_id' => $this->_tagID,
211 );
212
213 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
214
215 $this->assertEquals($result['removed'], 2);
216 }
217
218 function testEntityTagDeleteHH() {
219 $entityTagParams = array(
220 'contact_id_i' => $this->_individualID,
221 'contact_id_h' => $this->_householdID,
222 'tag_id' => $this->_tagID,
223 );
224 $this->entityTagAdd($entityTagParams);
225
226 $params = array(
227 'contact_id_h' => $this->_householdID,
228 'tag_id' => $this->_tagID,
229 );
230
231 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
232 $this->assertEquals($result['removed'], 1);
233 }
234
235 function testEntityTagDeleteHHORG() {
236 $entityTagParams = array(
237 'contact_id_i' => $this->_individualID,
238 'contact_id_h' => $this->_householdID,
239 'tag_id' => $this->_tagID,
240 );
241 $this->entityTagAdd($entityTagParams);
242
243 $params = array(
244 'contact_id_h' => $this->_householdID,
245 'contact_id_o' => $this->_organizationID,
246 'tag_id' => $this->_tagID,
247 );
248
249 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
250 $this->assertEquals($result['removed'], 1);
251 $this->assertEquals($result['not_removed'], 1);
252 }
253
254 ///////////////// civicrm_tag_entities_get methods
255
256 function testCommonContactEntityTagAdd() {
257 $params = array(
258 'contact_id' => $this->_individualID,
259 'tag_id' => $this->_tagID,
260 );
261
262 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
263 $this->assertEquals($individualEntity['added'], 1);
264 }
265
266
267 function testEntityTagCommonDeleteINDHH() {
268 $entityTagParams = array(
269 'contact_id_i' => $this->_individualID,
270 'contact_id_h' => $this->_householdID,
271 'tag_id' => $this->_tagID,
272 );
273 $this->entityTagAdd($entityTagParams);
274
275 $params = array(
276 'contact_id_i' => $this->_individualID,
277 'contact_id_h' => $this->_householdID,
278 'tag_id' => $this->_tagID,
279 );
280
281 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
282 $this->assertEquals($result['removed'], 2);
283 }
284
285 function testEntityTagCommonDeleteHH() {
286 $entityTagParams = array(
287 'contact_id_i' => $this->_individualID,
288 'contact_id_h' => $this->_householdID,
289 'tag_id' => $this->_tagID,
290 );
291 $this->entityTagAdd($entityTagParams);
292
293 $params = array(
294 'contact_id_h' => $this->_householdID,
295 'tag_id' => $this->_tagID,
296 );
297
298 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
299 $this->assertEquals($result['removed'], 1);
300 }
301
302 function testEntityTagCommonDeleteHHORG() {
303 $entityTagParams = array(
304 'contact_id_i' => $this->_individualID,
305 'contact_id_h' => $this->_householdID,
306 'tag_id' => $this->_tagID,
307 );
308 $this->entityTagAdd($entityTagParams);
309
310 $params = array(
311 'contact_id_h' => $this->_householdID,
312 'contact_id_o' => $this->_organizationID,
313 'tag_id' => $this->_tagID,
314 );
315
316 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
317 $this->assertEquals($result['removed'], 1);
318 $this->assertEquals($result['not_removed'], 1);
319 }
320 }
321