Merge pull request #7550 from jitendrapurohit/CRM-17778
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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->callAPISuccess('entity_tag', 'delete', $params);
200
201 $this->assertEquals($result['not_removed'], 0);
202 $this->assertEquals($result['removed'], 2);
203 $this->assertEquals($result['total_count'], 2);
204 }
205
206 public function testEntityTagDeleteINDHH() {
207 $entityTagParams = array(
208 'contact_id_i' => $this->_individualID,
209 'contact_id_h' => $this->_householdID,
210 'tag_id' => $this->_tagID,
211 );
212 $this->entityTagAdd($entityTagParams);
213
214 $params = array(
215 'contact_id_i' => $this->_individualID,
216 'contact_id_h' => $this->_householdID,
217 'tag_id' => $this->_tagID,
218 );
219
220 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
221
222 $this->assertEquals($result['removed'], 2);
223 }
224
225 public function testEntityTagDeleteHH() {
226 $entityTagParams = array(
227 'contact_id_i' => $this->_individualID,
228 'contact_id_h' => $this->_householdID,
229 'tag_id' => $this->_tagID,
230 );
231 $this->entityTagAdd($entityTagParams);
232
233 $params = array(
234 'contact_id_h' => $this->_householdID,
235 'tag_id' => $this->_tagID,
236 );
237
238 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
239 $this->assertEquals($result['removed'], 1);
240 }
241
242 public function testEntityTagDeleteHHORG() {
243 $entityTagParams = array(
244 'contact_id_i' => $this->_individualID,
245 'contact_id_h' => $this->_householdID,
246 'tag_id' => $this->_tagID,
247 );
248 $this->entityTagAdd($entityTagParams);
249
250 $params = array(
251 'contact_id_h' => $this->_householdID,
252 'contact_id_o' => $this->_organizationID,
253 'tag_id' => $this->_tagID,
254 );
255
256 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
257 $this->assertEquals($result['removed'], 1);
258 $this->assertEquals($result['not_removed'], 1);
259 }
260
261 /**
262 * civicrm_tag_entities_get methods.
263 */
264 public function testCommonContactEntityTagAdd() {
265 $params = array(
266 'contact_id' => $this->_individualID,
267 'tag_id' => $this->_tagID,
268 );
269
270 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
271 $this->assertEquals($individualEntity['added'], 1);
272 }
273
274
275 public function testEntityTagCommonDeleteINDHH() {
276 $entityTagParams = array(
277 'contact_id_i' => $this->_individualID,
278 'contact_id_h' => $this->_householdID,
279 'tag_id' => $this->_tagID,
280 );
281 $this->entityTagAdd($entityTagParams);
282
283 $params = array(
284 'contact_id_i' => $this->_individualID,
285 'contact_id_h' => $this->_householdID,
286 'tag_id' => $this->_tagID,
287 );
288
289 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
290 $this->assertEquals($result['removed'], 2);
291 }
292
293 public function testEntityTagCommonDeleteHH() {
294 $entityTagParams = array(
295 'contact_id_i' => $this->_individualID,
296 'contact_id_h' => $this->_householdID,
297 'tag_id' => $this->_tagID,
298 );
299 $this->entityTagAdd($entityTagParams);
300
301 $params = array(
302 'contact_id_h' => $this->_householdID,
303 'tag_id' => $this->_tagID,
304 );
305
306 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
307 $this->assertEquals($result['removed'], 1);
308 }
309
310 public function testEntityTagCommonDeleteHHORG() {
311 $entityTagParams = array(
312 'contact_id_i' => $this->_individualID,
313 'contact_id_h' => $this->_householdID,
314 'tag_id' => $this->_tagID,
315 );
316 $this->entityTagAdd($entityTagParams);
317
318 $params = array(
319 'contact_id_h' => $this->_householdID,
320 'contact_id_o' => $this->_organizationID,
321 'tag_id' => $this->_tagID,
322 );
323
324 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
325 $this->assertEquals($result['removed'], 1);
326 $this->assertEquals($result['not_removed'], 1);
327 }
328
329 }