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