Coding standards cleanup sprint.
[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-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 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->callAPIAndDocument('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 ///////////////// civicrm_entity_tag_get methods
123 public function testGetNoEntityID() {
124 $ContactId = $this->_individualID;
125 $tagID = $this->_tagID;
126 $params = array(
127 'contact_id' => $ContactId,
128 'tag_id' => $tagID,
129 );
130
131 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
132 $this->assertEquals($individualEntity['added'], 1);
133 $result = $this->callAPISuccess($this->_entity, 'get', array('sequential' => 1, 'tag_id' => $tagID));
134 $this->assertEquals($ContactId, $result['values'][0]['entity_id']);
135 }
136
137 public function testIndividualEntityTagGet() {
138 $contactId = $this->_individualID;
139 $tagID = $this->_tagID;
140 $params = array(
141 'contact_id' => $contactId,
142 'tag_id' => $tagID,
143 );
144
145 $individualEntity = $this->callAPIAndDocument('entity_tag', 'create', $params, __FUNCTION__, __FILE__);
146 $this->assertEquals($individualEntity['added'], 1);
147
148 $paramsEntity = array(
149 'contact_id' => $contactId,
150 );
151 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
152 }
153
154 public function testHouseholdEntityGet() {
155 $ContactId = $this->_householdID;
156 $tagID = $this->_tagID;
157 $params = array(
158 'contact_id' => $ContactId,
159 'tag_id' => $tagID,
160 );
161
162 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
163 $this->assertEquals($householdEntity['added'], 1);
164 }
165
166 public function testOrganizationEntityGet() {
167 $ContactId = $this->_organizationID;
168 $tagID = $this->_tagID;
169 $params = array(
170 'contact_id' => $ContactId,
171 'tag_id' => $tagID,
172 );
173
174 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
175 $this->assertEquals($organizationEntity['added'], 1);
176
177 $paramsEntity = array('contact_id' => $ContactId);
178 $entity = $this->callAPISuccess('entity_tag', 'get', $paramsEntity);
179 }
180
181 ///////////////// civicrm_entity_tag_Delete methods
182 public function testEntityTagDeleteNoTagId() {
183 $entityTagParams = array(
184 'contact_id_i' => $this->_individualID,
185 'contact_id_h' => $this->_householdID,
186 'tag_id' => $this->_tagID,
187 );
188 $this->entityTagAdd($entityTagParams);
189
190 $params = array(
191 'contact_id_i' => $this->_individualID,
192 'contact_id_h' => $this->_householdID,
193 );
194
195 $result = $this->callAPIFailure('entity_tag', 'delete', $params,
196 'tag_id is a required field'
197 );
198 }
199
200 public function testEntityTagDeleteINDHH() {
201 $entityTagParams = array(
202 'contact_id_i' => $this->_individualID,
203 'contact_id_h' => $this->_householdID,
204 'tag_id' => $this->_tagID,
205 );
206 $this->entityTagAdd($entityTagParams);
207
208 $params = array(
209 'contact_id_i' => $this->_individualID,
210 'contact_id_h' => $this->_householdID,
211 'tag_id' => $this->_tagID,
212 );
213
214 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
215
216 $this->assertEquals($result['removed'], 2);
217 }
218
219 public function testEntityTagDeleteHH() {
220 $entityTagParams = array(
221 'contact_id_i' => $this->_individualID,
222 'contact_id_h' => $this->_householdID,
223 'tag_id' => $this->_tagID,
224 );
225 $this->entityTagAdd($entityTagParams);
226
227 $params = array(
228 'contact_id_h' => $this->_householdID,
229 'tag_id' => $this->_tagID,
230 );
231
232 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
233 $this->assertEquals($result['removed'], 1);
234 }
235
236 public function testEntityTagDeleteHHORG() {
237 $entityTagParams = array(
238 'contact_id_i' => $this->_individualID,
239 'contact_id_h' => $this->_householdID,
240 'tag_id' => $this->_tagID,
241 );
242 $this->entityTagAdd($entityTagParams);
243
244 $params = array(
245 'contact_id_h' => $this->_householdID,
246 'contact_id_o' => $this->_organizationID,
247 'tag_id' => $this->_tagID,
248 );
249
250 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
251 $this->assertEquals($result['removed'], 1);
252 $this->assertEquals($result['not_removed'], 1);
253 }
254
255 ///////////////// civicrm_tag_entities_get methods
256
257 public function testCommonContactEntityTagAdd() {
258 $params = array(
259 'contact_id' => $this->_individualID,
260 'tag_id' => $this->_tagID,
261 );
262
263 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $params);
264 $this->assertEquals($individualEntity['added'], 1);
265 }
266
267
268 public function testEntityTagCommonDeleteINDHH() {
269 $entityTagParams = array(
270 'contact_id_i' => $this->_individualID,
271 'contact_id_h' => $this->_householdID,
272 'tag_id' => $this->_tagID,
273 );
274 $this->entityTagAdd($entityTagParams);
275
276 $params = array(
277 'contact_id_i' => $this->_individualID,
278 'contact_id_h' => $this->_householdID,
279 'tag_id' => $this->_tagID,
280 );
281
282 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
283 $this->assertEquals($result['removed'], 2);
284 }
285
286 public function testEntityTagCommonDeleteHH() {
287 $entityTagParams = array(
288 'contact_id_i' => $this->_individualID,
289 'contact_id_h' => $this->_householdID,
290 'tag_id' => $this->_tagID,
291 );
292 $this->entityTagAdd($entityTagParams);
293
294 $params = array(
295 'contact_id_h' => $this->_householdID,
296 'tag_id' => $this->_tagID,
297 );
298
299 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
300 $this->assertEquals($result['removed'], 1);
301 }
302
303 public function testEntityTagCommonDeleteHHORG() {
304 $entityTagParams = array(
305 'contact_id_i' => $this->_individualID,
306 'contact_id_h' => $this->_householdID,
307 'tag_id' => $this->_tagID,
308 );
309 $this->entityTagAdd($entityTagParams);
310
311 $params = array(
312 'contact_id_h' => $this->_householdID,
313 'contact_id_o' => $this->_organizationID,
314 'tag_id' => $this->_tagID,
315 );
316
317 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
318 $this->assertEquals($result['removed'], 1);
319 $this->assertEquals($result['not_removed'], 1);
320 }
321 }