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