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