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