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