Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 /**
29 * Test APIv3 civicrm_entity_tag_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Core
33 */
34
35 /**
36 * Class api_v3_EntityTagTest.
37 * @group headless
38 */
39 class api_v3_EntityTagTest extends CiviUnitTestCase {
40
41 /**
42 * @var int
43 */
44 protected $_individualID;
45 protected $_householdID;
46 protected $_organizationID;
47 protected $_tagID;
48 protected $_apiversion = 3;
49 protected $_tag;
50 protected $_entity = 'entity_tag';
51
52 /**
53 * Basic parameters for create.
54 *
55 * @var array
56 */
57 protected $_params = [];
58
59 /**
60 * Set up for test.
61 */
62 public function setUp() {
63 parent::setUp();
64 $this->useTransaction(TRUE);
65
66 $this->_individualID = $this->individualCreate();
67 $this->_tag = $this->tagCreate(['name' => 'EntityTagTest']);
68 $this->_tagID = $this->_tag['id'];
69 $this->_householdID = $this->houseHoldCreate();
70 $this->_organizationID = $this->organizationCreate();
71 $this->_params = [
72 'entity_id' => $this->_individualID,
73 'tag_id' => $this->_tagID,
74 ];
75 }
76
77 /**
78 * Test required parameters.
79 *
80 * These failure tests are low value and may not be worth putting in v4.
81 */
82 public function testFailureTests() {
83 $this->callAPIFailure('entity_tag', 'create', ['contact_id' => $this->_individualID],
84 'tag_id is a required field'
85 );
86 $this->callAPIFailure('entity_tag', 'create', ['tag_id' => $this->_tagID],
87 'contact_id is a required field'
88 );
89 }
90
91 /**
92 * Test basic create.
93 * @param int $version
94 * @dataProvider versionThreeAndFour
95 */
96 public function testContactEntityTagCreate($version) {
97 $this->_apiversion = $version;
98 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
99 }
100
101 /**
102 * Test multiple add functionality.
103 *
104 * This needs review for api v4 as it makes for a very non standard api.
105 */
106 public function testAddDouble() {
107
108 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
109 $this->assertEquals($result['added'], 1);
110
111 $params = [
112 'contact_id_i' => $this->_individualID,
113 'contact_id_o' => $this->_organizationID,
114 'tag_id' => $this->_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 * Test that get works without an entity.
124 */
125 public function testGetNoEntityID() {
126 $this->callAPISuccess('entity_tag', 'create', $this->_params);
127 $result = $this->callAPISuccess($this->_entity, 'get', ['sequential' => 1, 'tag_id' => $this->_tagID]);
128 $this->assertEquals($this->_individualID, $result['values'][0]['entity_id']);
129 }
130
131 /**
132 * Basic get functionality test.
133 * @param int $version
134 * @dataProvider versionThreeAndFour
135 */
136 public function testIndividualEntityTagGet($version) {
137 $this->_apiversion = $version;
138 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params);
139
140 $paramsEntity = [
141 'contact_id' => $this->_individualID,
142 ];
143 $result = $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
144 $this->assertEquals(1, $result['count']);
145 $this->assertEquals($this->_tagID, $result['values'][$result['id']]['tag_id']);
146 }
147
148 /**
149 * Test memory usage does not escalate crazily.
150 */
151 public function testMemoryLeak() {
152 $start = memory_get_usage();
153 for ($i = 0; $i < 100; $i++) {
154 $this->callAPISuccess('EntityTag', 'get', []);
155 $memUsage = memory_get_usage();
156 }
157 $max = $start + 2000000;
158 $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) ");
159 }
160
161 /**
162 * Test tag can be added to a household.
163 */
164 public function testHouseholdEntityCreate() {
165 $params = [
166 'contact_id' => $this->_householdID,
167 'tag_id' => $this->_tagID,
168 ];
169
170 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
171 $this->assertEquals($householdEntity['added'], 1);
172 }
173
174 /**
175 * Test tag can be added to an organization.
176 * @param int $version
177 * @dataProvider versionThreeAndFour
178 */
179 public function testOrganizationEntityGet($version) {
180 $this->_apiversion = $version;
181
182 $params = [
183 'entity_id' => $this->_organizationID,
184 'tag_id' => $this->_tagID,
185 ];
186
187 $this->callAPISuccess('entity_tag', 'create', $params);
188
189 $tag = $this->callAPISuccess('entity_tag', 'getsingle', ['contact_id' => $this->_organizationID]);
190 $this->assertEquals($this->_organizationID, $tag['entity_id']);
191 $this->assertEquals($this->_tagID, $tag['tag_id']);
192 }
193
194 /**
195 * Civicrm_entity_tag_Delete methods.
196 */
197 public function testEntityTagDeleteNoTagId() {
198 $entityTagParams = [
199 'contact_id_i' => $this->_individualID,
200 'contact_id_h' => $this->_householdID,
201 'tag_id' => $this->_tagID,
202 ];
203 $this->entityTagAdd($entityTagParams);
204
205 $params = [
206 'contact_id_i' => $this->_individualID,
207 'contact_id_h' => $this->_householdID,
208 ];
209
210 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
211
212 $this->assertEquals($result['not_removed'], 0);
213 $this->assertEquals($result['removed'], 2);
214 $this->assertEquals($result['total_count'], 2);
215 }
216
217 public function testEntityTagDeleteINDHH() {
218 $entityTagParams = [
219 'contact_id_i' => $this->_individualID,
220 'contact_id_h' => $this->_householdID,
221 'tag_id' => $this->_tagID,
222 ];
223 $this->entityTagAdd($entityTagParams);
224
225 $params = [
226 'contact_id_i' => $this->_individualID,
227 'contact_id_h' => $this->_householdID,
228 'tag_id' => $this->_tagID,
229 ];
230
231 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
232
233 $this->assertEquals($result['removed'], 2);
234 }
235
236 public function testEntityTagDeleteHH() {
237 $entityTagParams = [
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 = [
245 'contact_id_h' => $this->_householdID,
246 'tag_id' => $this->_tagID,
247 ];
248
249 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
250 $this->assertEquals($result['removed'], 1);
251 }
252
253 public function testEntityTagDeleteHHORG() {
254 $entityTagParams = [
255 'contact_id_i' => $this->_individualID,
256 'contact_id_h' => $this->_householdID,
257 'tag_id' => $this->_tagID,
258 ];
259 $this->entityTagAdd($entityTagParams);
260
261 $params = [
262 'contact_id_h' => $this->_householdID,
263 'contact_id_o' => $this->_organizationID,
264 'tag_id' => $this->_tagID,
265 ];
266
267 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
268 $this->assertEquals($result['removed'], 1);
269 $this->assertEquals($result['not_removed'], 1);
270 }
271
272 public function testEntityTagCommonDeleteINDHH() {
273 $entityTagParams = [
274 'contact_id_i' => $this->_individualID,
275 'contact_id_h' => $this->_householdID,
276 'tag_id' => $this->_tagID,
277 ];
278 $this->entityTagAdd($entityTagParams);
279
280 $params = [
281 'contact_id_i' => $this->_individualID,
282 'contact_id_h' => $this->_householdID,
283 'tag_id' => $this->_tagID,
284 ];
285
286 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
287 $this->assertEquals($result['removed'], 2);
288 }
289
290 public function testEntityTagCommonDeleteHH() {
291 $entityTagParams = [
292 'contact_id_i' => $this->_individualID,
293 'contact_id_h' => $this->_householdID,
294 'tag_id' => $this->_tagID,
295 ];
296 $this->entityTagAdd($entityTagParams);
297
298 $params = [
299 'contact_id_h' => $this->_householdID,
300 'tag_id' => $this->_tagID,
301 ];
302
303 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
304 $this->assertEquals($result['removed'], 1);
305 }
306
307 public function testEntityTagCommonDeleteHHORG() {
308 $entityTagParams = [
309 'contact_id_i' => $this->_individualID,
310 'contact_id_h' => $this->_householdID,
311 'tag_id' => $this->_tagID,
312 ];
313 $this->entityTagAdd($entityTagParams);
314
315 $params = [
316 'contact_id_h' => $this->_householdID,
317 'contact_id_o' => $this->_organizationID,
318 'tag_id' => $this->_tagID,
319 ];
320
321 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
322 $this->assertEquals($result['removed'], 1);
323 $this->assertEquals($result['not_removed'], 1);
324 }
325
326 public function testEntityTagJoin() {
327 $org = $this->callAPISuccess('Contact', 'create', [
328 'contact_type' => 'Organization',
329 'organization_name' => 'Org123',
330 'api.EntityTag.create' => [
331 'tag_id' => $this->_tagID,
332 ],
333 ]);
334 // Fetch contact info via join
335 $result = $this->callAPISuccessGetSingle('EntityTag', [
336 'return' => ["entity_id.organization_name", "tag_id.name"],
337 'entity_id' => $org['id'],
338 'entity_table' => "civicrm_contact",
339 ]);
340 $this->assertEquals('Org123', $result['entity_id.organization_name']);
341 $this->assertEquals('EntityTagTest', $result['tag_id.name']);
342 // This should return no results by restricting contact_type
343 $result = $this->callAPISuccess('EntityTag', 'get', [
344 'return' => ["entity_id.organization_name"],
345 'entity_id' => $org['id'],
346 'entity_table' => "civicrm_contact",
347 'entity_id.contact_type' => "Individual",
348 ]);
349 $this->assertEquals(0, $result['count']);
350 }
351
352 }