CRM-13072 upgrade additional test classes
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
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
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 public $_eNoticeCompliant = TRUE;
48
49 function setUp() {
50 parent::setUp();
51
52 $this->_individualID = $this->individualCreate(NULL);
53 $this->_tag = $this->tagCreate(NULL);
54 $this->_tagID = $this->_tag['id'];
55 $this->_householdID = $this->houseHoldCreate(NULL);
56 $this->_organizationID = $this->organizationCreate(NULL);
57 }
58
59 function tearDown() {
60 $this->quickCleanup(array('civicrm_tag', 'civicrm_entity_tag'));
61 }
62
63 function testAddEmptyParams() {
64 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params = array(),
65 'contact_id is a required field'
66 );
67 }
68
69 function testAddWithoutTagID() {
70 $params = array(
71 'contact_id' => $this->_individualID,
72 );
73 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
74 'tag_id is a required field'
75 );
76 }
77
78 function testAddWithoutContactID() {
79 $params = array(
80 'tag_id' => $this->_tagID,
81 );
82 $individualEntity = $this->callAPIFailure('entity_tag', 'create', $params,
83 'contact_id is a required field');
84 }
85
86 function testContactEntityTagCreate() {
87 $params = array(
88 'contact_id' => $this->_individualID,
89 'tag_id' => $this->_tagID,
90 );
91
92 $result = $this->callAPIAndDocument('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
93 $this->assertEquals($result['added'], 1);
94 }
95
96 function testAddDouble() {
97 $individualId = $this->_individualID;
98 $organizationId = $this->_organizationID;
99 $tagID = $this->_tagID;
100 $params = array(
101 'contact_id' => $individualId,
102 'tag_id' => $tagID,
103 );
104
105 $result = $this->callAPISuccess('entity_tag', 'create', $params);
106
107 $this->assertEquals($result['added'], 1);
108
109 $params = array(
110 'contact_id_i' => $individualId,
111 'contact_id_o' => $organizationId,
112 'tag_id' => $tagID,
113 );
114
115 $result = $this->callAPISuccess('entity_tag', 'create', $params);
116 $this->assertEquals($result['added'], 1);
117 $this->assertEquals($result['not_added'], 1);
118 }
119
120 ///////////////// civicrm_entity_tag_get methods
121 function testGetWrongParamsType() {
122 $ContactId = $this->_individualID;
123 $tagID = $this->_tagID;
124 $params = array(
125 'contact_id' => $ContactId,
126 'tag_id' => $tagID,
127 );
128
129 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
130 $this->assertEquals($individualEntity['added'], 1);
131 }
132
133 function testIndividualEntityTagGetWithoutContactID() {
134 $paramsEntity = array();
135 $entity = $this->callAPIFailure('entity_tag', 'get', $paramsEntity,
136 'Mandatory key(s) missing from params array: entity_id'
137 );
138 }
139
140 function testIndividualEntityTagGet() {
141 $contactId = $this->_individualID;
142 $tagID = $this->_tagID;
143 $params = array(
144 'contact_id' => $contactId,
145 'tag_id' => $tagID,
146 );
147
148 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
149 $this->assertEquals($individualEntity['added'], 1);
150
151 $paramsEntity = array(
152 'contact_id' => $contactId,
153 );
154 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
155 }
156
157 function testHouseholdEntityGetWithoutContactID() {
158 $entity = $this->callAPIFailure('entity_tag', 'get', array());
159 }
160
161 function testHouseholdEntityGet() {
162 $ContactId = $this->_householdID;
163 $tagID = $this->_tagID;
164 $params = array(
165 'contact_id' => $ContactId,
166 'tag_id' => $tagID,
167 );
168
169 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
170 $this->assertEquals($householdEntity['added'], 1);
171 }
172
173 function testOrganizationEntityGetWithoutContactID() {
174 $entity = $this->callAPIFailure('entity_tag', 'get', array());
175 }
176
177 function testOrganizationEntityGet() {
178 $ContactId = $this->_organizationID;
179 $tagID = $this->_tagID;
180 $params = array(
181 'contact_id' => $ContactId,
182 'tag_id' => $tagID,
183 );
184
185 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
186 $this->assertEquals($organizationEntity['added'], 1);
187
188 $paramsEntity = array('contact_id' => $ContactId);
189 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
190 }
191
192 ///////////////// civicrm_entity_tag_remove methods
193 function testEntityTagRemoveNoTagId() {
194 $entityTagParams = array(
195 'contact_id_i' => $this->_individualID,
196 'contact_id_h' => $this->_householdID,
197 'tag_id' => $this->_tagID,
198 );
199 $this->entityTagAdd($entityTagParams);
200
201 $params = array(
202 'contact_id_i' => $this->_individualID,
203 'contact_id_h' => $this->_householdID,
204 );
205
206 $result = $this->callAPIFailure('entity_tag', 'delete', $params,
207 'tag_id is a required field'
208 );
209 }
210
211 function testEntityTagRemoveINDHH() {
212 $entityTagParams = array(
213 'contact_id_i' => $this->_individualID,
214 'contact_id_h' => $this->_householdID,
215 'tag_id' => $this->_tagID,
216 );
217 $this->entityTagAdd($entityTagParams);
218
219 $params = array(
220 'contact_id_i' => $this->_individualID,
221 'contact_id_h' => $this->_householdID,
222 'tag_id' => $this->_tagID,
223 );
224
225 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
226
227 $this->assertEquals($result['removed'], 2);
228 }
229
230 function testEntityTagDeleteHH() {
231 $entityTagParams = array(
232 'contact_id_i' => $this->_individualID,
233 'contact_id_h' => $this->_householdID,
234 'tag_id' => $this->_tagID,
235 );
236 $this->entityTagAdd($entityTagParams);
237
238 $params = array(
239 'contact_id_h' => $this->_householdID,
240 'tag_id' => $this->_tagID,
241 );
242
243 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
244 $this->assertEquals($result['removed'], 1);
245 }
246
247 function testEntityTagRemoveHHORG() {
248 $entityTagParams = array(
249 'contact_id_i' => $this->_individualID,
250 'contact_id_h' => $this->_householdID,
251 'tag_id' => $this->_tagID,
252 );
253 $this->entityTagAdd($entityTagParams);
254
255 $params = array(
256 'contact_id_h' => $this->_householdID,
257 'contact_id_o' => $this->_organizationID,
258 'tag_id' => $this->_tagID,
259 );
260
261 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
262 $this->assertEquals($result['removed'], 1);
263 $this->assertEquals($result['not_removed'], 1);
264 }
265
266 ///////////////// civicrm_tag_entities_get methods
267
268 function testCommonContactEntityTagAdd() {
269 $params = array(
270 'contact_id' => $this->_individualID,
271 'tag_id' => $this->_tagID,
272 );
273
274 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
275 $this->assertEquals($individualEntity['added'], 1);
276 }
277
278
279 function testEntityTagCommonRemoveINDHH() {
280 $entityTagParams = array(
281 'contact_id_i' => $this->_individualID,
282 'contact_id_h' => $this->_householdID,
283 'tag_id' => $this->_tagID,
284 );
285 $this->entityTagAdd($entityTagParams);
286
287 $params = array(
288 'contact_id_i' => $this->_individualID,
289 'contact_id_h' => $this->_householdID,
290 'tag_id' => $this->_tagID,
291 );
292
293 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
294 $this->assertEquals($result['removed'], 2);
295 }
296
297 function testEntityTagCommonRemoveHH() {
298 $entityTagParams = array(
299 'contact_id_i' => $this->_individualID,
300 'contact_id_h' => $this->_householdID,
301 'tag_id' => $this->_tagID,
302 );
303 $this->entityTagAdd($entityTagParams);
304
305 $params = array(
306 'contact_id_h' => $this->_householdID,
307 'tag_id' => $this->_tagID,
308 );
309
310 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
311 $this->assertEquals($result['removed'], 1);
312 }
313
314 function testEntityTagCommonRemoveHHORG() {
315 $entityTagParams = array(
316 'contact_id_i' => $this->_individualID,
317 'contact_id_h' => $this->_householdID,
318 'tag_id' => $this->_tagID,
319 );
320 $this->entityTagAdd($entityTagParams);
321
322 $params = array(
323 'contact_id_h' => $this->_householdID,
324 'contact_id_o' => $this->_organizationID,
325 'tag_id' => $this->_tagID,
326 );
327
328 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
329 $this->assertEquals($result['removed'], 1);
330 $this->assertEquals($result['not_removed'], 1);
331 }
332 }
333