CRM-12274
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
28require_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
38require_once 'CiviTest/CiviUnitTestCase.php';
39class api_v3_EntityTagTest extends CiviUnitTestCase {
40
41 protected $_individualID;
42 protected $_householdID;
43 protected $_organizationID;
44 protected $_tagID;
45 protected $_apiversion;
46 protected $_tag;
47 public $_eNoticeCompliant = TRUE;
48
49 function setUp() {
50 parent::setUp();
51 $this->_apiversion = 3;
52
53 $this->quickCleanup(array('civicrm_tag', 'civicrm_entity_tag'));
54
55 $this->_individualID = $this->individualCreate(NULL);
56 $this->_tag = $this->tagCreate(NULL);
57 $this->_tagID = $this->_tag['id'];
58 $this->_householdID = $this->houseHoldCreate(NULL);
59 $this->_organizationID = $this->organizationCreate(NULL);
60 }
61
62 function tearDown() {}
63
64 function testAddEmptyParams() {
65 $params = array('version' => $this->_apiversion);
66 $individualEntity = civicrm_api('entity_tag', 'create', $params);
67 $this->assertEquals($individualEntity['is_error'], 1);
68 $this->assertEquals($individualEntity['error_message'], 'contact_id is a required field');
69 }
70
71 function testAddWithoutTagID() {
72 $params = array(
73 'contact_id' => $this->_individualID,
74 'version' => $this->_apiversion,
75 );
76 $individualEntity = civicrm_api('entity_tag', 'create', $params);
77 $this->assertEquals($individualEntity['is_error'], 1);
78 $this->assertEquals($individualEntity['error_message'], 'tag_id is a required field');
79 }
80
81 function testAddWithoutContactID() {
82 $params = array(
83 'tag_id' => $this->_tagID,
84 'version' => $this->_apiversion,
85 );
86 $individualEntity = civicrm_api('entity_tag', 'create', $params);
87 $this->assertEquals($individualEntity['is_error'], 1);
88 $this->assertEquals($individualEntity['error_message'], 'contact_id is a required field');
89 }
90
91 function testContactEntityTagCreate() {
92 $params = array(
93 'contact_id' => $this->_individualID,
94 'tag_id' => $this->_tagID,
95 'version' => $this->_apiversion,
96 );
97
98 $result = civicrm_api('entity_tag', 'create', $params);
99 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
100
101 $this->assertEquals($result['is_error'], 0);
102 $this->assertEquals($result['added'], 1);
103 }
104
105 function testAddDouble() {
106 $individualId = $this->_individualID;
107 $organizationId = $this->_organizationID;
108 $tagID = $this->_tagID;
109 $params = array(
110 'contact_id' => $individualId,
111 'tag_id' => $tagID,
112 'version' => $this->_apiversion,
113 );
114
115 $result = civicrm_api('entity_tag', 'create', $params);
116
117 $this->assertEquals($result['is_error'], 0);
118 $this->assertEquals($result['added'], 1);
119
120 $params = array(
121 'contact_id_i' => $individualId,
122 'contact_id_o' => $organizationId,
123 'tag_id' => $tagID,
124 'version' => $this->_apiversion,
125 );
126
127 $result = civicrm_api('entity_tag', 'create', $params);
128 $this->assertEquals($result['is_error'], 0);
129 $this->assertEquals($result['added'], 1);
130 $this->assertEquals($result['not_added'], 1);
131 }
132
133 ///////////////// civicrm_entity_tag_get methods
134 function testGetWrongParamsType() {
135 $ContactId = $this->_individualID;
136 $tagID = $this->_tagID;
137 $params = array(
138 'contact_id' => $ContactId,
139 'tag_id' => $tagID,
140 'version' => $this->_apiversion,
141 );
142
143 $individualEntity = civicrm_api('entity_tag', 'create', $params);
144 $this->assertEquals($individualEntity['is_error'], 0);
145 $this->assertEquals($individualEntity['added'], 1);
146
147 $paramsEntity = "wrong params";
148 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
149
150 $this->assertEquals($entity['is_error'], 1,
151 "In line " . __LINE__
152 );
153 $this->assertEquals($entity['error_message'], 'Input variable `params` is not an array');
154 }
155
156 function testIndividualEntityTagGetWithoutContactID() {
157 $paramsEntity = array('version' => $this->_apiversion);
158 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
159 $this->assertEquals($entity['is_error'], 1);
160 $this->assertNotNull($entity['error_message']);
161 $this->assertEquals($entity['error_message'], 'Mandatory key(s) missing from params array: entity_id');
162 }
163
164 function testIndividualEntityTagGet() {
165 $contactId = $this->_individualID;
166 $tagID = $this->_tagID;
167 $params = array(
168 'contact_id' => $contactId,
169 'tag_id' => $tagID,
170 'version' => $this->_apiversion,
171 );
172
173 $individualEntity = civicrm_api('entity_tag', 'create', $params);
174 $this->assertEquals($individualEntity['is_error'], 0);
175 $this->assertEquals($individualEntity['added'], 1);
176
177 $paramsEntity = array(
178 'contact_id' => $contactId,
179 'version' => $this->_apiversion,
180 );
181 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
182 }
183
184 function testHouseholdEntityGetWithoutContactID() {
185 $paramsEntity = array('version' => $this->_apiversion);
186 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
187 $this->assertEquals($entity['is_error'], 1);
188 $this->assertNotNull($entity['error_message']);
189 }
190
191 function testHouseholdEntityGet() {
192 $ContactId = $this->_householdID;
193 $tagID = $this->_tagID;
194 $params = array(
195 'contact_id' => $ContactId,
196 'tag_id' => $tagID,
197 'version' => $this->_apiversion,
198 );
199
200 $householdEntity = civicrm_api('entity_tag', 'create', $params);
201 $this->assertEquals($householdEntity['is_error'], 0);
202 $this->assertEquals($householdEntity['added'], 1);
203
204 $paramsEntity = array('contact_id' => $ContactId);
205 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
206 }
207
208 function testOrganizationEntityGetWithoutContactID() {
209 $paramsEntity = array('version' => $this->_apiversion);
210 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
211 $this->assertEquals($entity['is_error'], 1);
212 $this->assertNotNull($entity['error_message']);
213 }
214
215 function testOrganizationEntityGet() {
216 $ContactId = $this->_organizationID;
217 $tagID = $this->_tagID;
218 $params = array(
219 'contact_id' => $ContactId,
220 'tag_id' => $tagID,
221 'version' => $this->_apiversion,
222 );
223
224 $organizationEntity = civicrm_api('entity_tag', 'create', $params);
225 $this->assertEquals($organizationEntity['is_error'], 0);
226 $this->assertEquals($organizationEntity['added'], 1);
227
228 $paramsEntity = array('contact_id' => $ContactId);
229 $entity = civicrm_api('entity_tag', 'get', $paramsEntity);
230 }
231
232 ///////////////// civicrm_entity_tag_remove methods
233 function testEntityTagRemoveNoTagId() {
234 $entityTagParams = array(
235 'contact_id_i' => $this->_individualID,
236 'contact_id_h' => $this->_householdID,
237 'tag_id' => $this->_tagID,
238 'version' => $this->_apiversion,
239 );
240 $this->entityTagAdd($entityTagParams);
241
242 $params = array(
243 'contact_id_i' => $this->_individualID,
244 'contact_id_h' => $this->_householdID,
245 'version' => $this->_apiversion,
246 );
247
248 $result = civicrm_api('entity_tag', 'delete', $params);
249 $this->assertEquals($result['is_error'], 1);
250 $this->assertEquals($result['error_message'], 'tag_id is a required field');
251 }
252
253 function testEntityTagRemoveINDHH() {
254 $entityTagParams = array(
255 'contact_id_i' => $this->_individualID,
256 'contact_id_h' => $this->_householdID,
257 'tag_id' => $this->_tagID,
258 'version' => $this->_apiversion,
259 );
260 $this->entityTagAdd($entityTagParams);
261
262 $params = array(
263 'contact_id_i' => $this->_individualID,
264 'contact_id_h' => $this->_householdID,
265 'tag_id' => $this->_tagID,
266 'version' => $this->_apiversion,
267 );
268
269 $result = civicrm_api('entity_tag', 'delete', $params);
270
271 $this->assertEquals($result['is_error'], 0);
272 $this->assertEquals($result['removed'], 2);
273 }
274
275 function testEntityTagDeleteHH() {
276 $entityTagParams = array(
277 'contact_id_i' => $this->_individualID,
278 'contact_id_h' => $this->_householdID,
279 'tag_id' => $this->_tagID,
280 'version' => $this->_apiversion,
281 );
282 $this->entityTagAdd($entityTagParams);
283
284 $params = array(
285 'contact_id_h' => $this->_householdID,
286 'tag_id' => $this->_tagID,
287 'version' => $this->_apiversion,
288 );
289
290 $result = civicrm_api('entity_tag', 'delete', $params);
291 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
292 $this->assertEquals($result['removed'], 1);
293 }
294
295 function testEntityTagRemoveHHORG() {
296 $entityTagParams = array(
297 'contact_id_i' => $this->_individualID,
298 'contact_id_h' => $this->_householdID,
299 'tag_id' => $this->_tagID,
300 'version' => $this->_apiversion,
301 );
302 $this->entityTagAdd($entityTagParams);
303
304 $params = array(
305 'contact_id_h' => $this->_householdID,
306 'contact_id_o' => $this->_organizationID,
307 'tag_id' => $this->_tagID,
308 'version' => $this->_apiversion,
309 );
310
311 $result = civicrm_api('entity_tag', 'delete', $params);
312 $this->assertEquals($result['removed'], 1);
313 $this->assertEquals($result['not_removed'], 1);
314 }
315
316 ///////////////// civicrm_entity_tag_display methods
317 function testEntityTagDisplayWithContactId() {
318 $entityTagParams = array(
319 'contact_id' => $this->_individualID,
320 'tag_id' => $this->_tagID,
321 'version' => $this->_apiversion,
322 );
323 $this->entityTagAdd($entityTagParams);
324
325 $params = array(
326 'contact_id' => $this->_individualID,
327 'version' => $this->_apiversion,
328 );
329
330 $result = civicrm_api3_entity_tag_display($params);
331 $this->assertEquals($this->_tag['values'][$this->_tag['id']]['name'], $result);
332 }
333
334 ///////////////// civicrm_tag_entities_get methods
335
336
337
338 ///////////////// civicrm_entity_tag_common methods
339 function testCommonAddEmptyParams() {
340 $params = array(
341 'version' => $this->_apiversion,
342 );
343 $individualEntity = _civicrm_api3_entity_tag_common($params, 'add');
344 $this->assertEquals($individualEntity['is_error'], 1);
345 $this->assertEquals($individualEntity['error_message'], 'contact_id is a required field');
346 }
347
348 function testCommonAddWithoutTagID() {
349 $params = array(
350 'contact_id' => $this->_individualID,
351 'version' => $this->_apiversion,
352 );
353 $individualEntity = _civicrm_api3_entity_tag_common($params, 'add');
354 $this->assertEquals($individualEntity['is_error'], 1);
355 $this->assertEquals($individualEntity['error_message'], 'tag_id is a required field');
356 }
357
358 function testCommonAddWithoutContactID() {
359 $params = array(
360 'tag_id' => $this->_tagID,
361 'version' => $this->_apiversion,
362 );
363 $individualEntity = _civicrm_api3_entity_tag_common($params, 'add');
364 $this->assertEquals($individualEntity['is_error'], 1);
365 $this->assertEquals($individualEntity['error_message'], 'contact_id is a required field');
366 }
367
368 function testCommonContactEntityTagAdd() {
369 $params = array(
370 'contact_id' => $this->_individualID,
371 'tag_id' => $this->_tagID,
372 'version' => $this->_apiversion,
373 );
374
375 $individualEntity = _civicrm_api3_entity_tag_common($params, 'add');
376 $this->assertEquals($individualEntity['is_error'], 0);
377 $this->assertEquals($individualEntity['added'], 1);
378 }
379
380 function testEntityTagCommonRemoveNoContactId() {
381 $entityTagParams = array(
382 'contact_id_i' => $this->_individualID,
383 'contact_id_h' => $this->_householdID,
384 'tag_id' => $this->_tagID,
385 'version' => $this->_apiversion,
386 );
387 $this->entityTagAdd($entityTagParams);
388
389 $params = array(
390 'tag_id' => $this->_tagID,
391 'version' => $this->_apiversion,
392 );
393
394 $result = _civicrm_api3_entity_tag_common($params, 'remove');
395 $this->assertEquals($result['is_error'], 1);
396 $this->assertEquals($result['error_message'], 'contact_id is a required field');
397 }
398
399 function testEntityTagCommonRemoveNoTagId() {
400 $entityTagParams = array(
401 'contact_id_i' => $this->_individualID,
402 'contact_id_h' => $this->_householdID,
403 'tag_id' => $this->_tagID,
404 'version' => $this->_apiversion,
405 );
406 $this->entityTagAdd($entityTagParams);
407
408 $params = array(
409 'contact_id_i' => $this->_individualID,
410 'contact_id_h' => $this->_householdID,
411 'version' => $this->_apiversion,
412 );
413
414 $result = _civicrm_api3_entity_tag_common($params, 'remove');
415 $this->assertEquals($result['is_error'], 1);
416 $this->assertEquals($result['error_message'], 'tag_id is a required field');
417 }
418
419 function testEntityTagCommonRemoveINDHH() {
420 $entityTagParams = array(
421 'contact_id_i' => $this->_individualID,
422 'contact_id_h' => $this->_householdID,
423 'tag_id' => $this->_tagID,
424 'version' => $this->_apiversion,
425 );
426 $this->entityTagAdd($entityTagParams);
427
428 $params = array(
429 'contact_id_i' => $this->_individualID,
430 'contact_id_h' => $this->_householdID,
431 'tag_id' => $this->_tagID,
432 'version' => $this->_apiversion,
433 );
434
435 $result = _civicrm_api3_entity_tag_common($params, 'remove');
436
437 $this->assertEquals($result['is_error'], 0);
438 $this->assertEquals($result['removed'], 2);
439 }
440
441 function testEntityTagCommonRemoveHH() {
442 $entityTagParams = array(
443 'contact_id_i' => $this->_individualID,
444 'contact_id_h' => $this->_householdID,
445 'tag_id' => $this->_tagID,
446 'version' => $this->_apiversion,
447 );
448 $this->entityTagAdd($entityTagParams);
449
450 $params = array(
451 'contact_id_h' => $this->_householdID,
452 'tag_id' => $this->_tagID,
453 'version' => $this->_apiversion,
454 );
455
456 $result = _civicrm_api3_entity_tag_common($params, 'remove');
457 $this->assertEquals($result['removed'], 1);
458 }
459
460 function testEntityTagCommonRemoveHHORG() {
461 $entityTagParams = array(
462 'contact_id_i' => $this->_individualID,
463 'contact_id_h' => $this->_householdID,
464 'tag_id' => $this->_tagID,
465 'version' => $this->_apiversion,
466 );
467 $this->entityTagAdd($entityTagParams);
468
469 $params = array(
470 'contact_id_h' => $this->_householdID,
471 'contact_id_o' => $this->_organizationID,
472 'tag_id' => $this->_tagID,
473 'version' => $this->_apiversion,
474 );
475
476 $result = _civicrm_api3_entity_tag_common($params, 'remove');
477 $this->assertEquals($result['removed'], 1);
478 $this->assertEquals($result['not_removed'], 1);
479 }
480}
481