Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test APIv3 civicrm_entity_tag_* functions | |
30 | * | |
92915c55 TO |
31 | * @package CiviCRM_APIv3 |
32 | * @subpackage API_Core | |
6a488035 TO |
33 | */ |
34 | ||
4cbe18b8 | 35 | /** |
24602943 | 36 | * Class api_v3_EntityTagTest. |
acb109b7 | 37 | * @group headless |
4cbe18b8 | 38 | */ |
6a488035 TO |
39 | class api_v3_EntityTagTest extends CiviUnitTestCase { |
40 | ||
24602943 | 41 | /** |
42 | * @var int | |
43 | */ | |
6a488035 TO |
44 | protected $_individualID; |
45 | protected $_householdID; | |
46 | protected $_organizationID; | |
47 | protected $_tagID; | |
fc928539 | 48 | protected $_apiversion = 3; |
6a488035 | 49 | protected $_tag; |
283c85e9 | 50 | protected $_entity = 'entity_tag'; |
b7c9bc4c | 51 | |
24602943 | 52 | /** |
53 | * Basic parameters for create. | |
54 | * | |
55 | * @var array | |
56 | */ | |
9099cab3 | 57 | protected $_params = []; |
6a488035 | 58 | |
24602943 | 59 | /** |
60 | * Set up for test. | |
61 | */ | |
00be9182 | 62 | public function setUp() { |
6a488035 | 63 | parent::setUp(); |
56b010a3 | 64 | $this->useTransaction(TRUE); |
6a488035 | 65 | |
e4d5f1e2 | 66 | $this->_individualID = $this->individualCreate(); |
9099cab3 | 67 | $this->_tag = $this->tagCreate(['name' => 'EntityTagTest']); |
6a488035 | 68 | $this->_tagID = $this->_tag['id']; |
678fbf79 | 69 | $this->_householdID = $this->houseHoldCreate(); |
70 | $this->_organizationID = $this->organizationCreate(); | |
9099cab3 | 71 | $this->_params = [ |
2d932085 | 72 | 'entity_id' => $this->_individualID, |
24602943 | 73 | 'tag_id' => $this->_tagID, |
9099cab3 | 74 | ]; |
6a488035 TO |
75 | } |
76 | ||
24602943 | 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() { | |
9099cab3 | 83 | $this->callAPIFailure('entity_tag', 'create', ['contact_id' => $this->_individualID], |
fc928539 | 84 | 'tag_id is a required field' |
85 | ); | |
9099cab3 | 86 | $this->callAPIFailure('entity_tag', 'create', ['tag_id' => $this->_tagID], |
24602943 | 87 | 'contact_id is a required field' |
6a488035 | 88 | ); |
6a488035 TO |
89 | } |
90 | ||
24602943 | 91 | /** |
92 | * Test basic create. | |
2d932085 CW |
93 | * @param int $version |
94 | * @dataProvider versionThreeAndFour | |
24602943 | 95 | */ |
2d932085 CW |
96 | public function testContactEntityTagCreate($version) { |
97 | $this->_apiversion = $version; | |
24602943 | 98 | $result = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 TO |
99 | } |
100 | ||
24602943 | 101 | /** |
102 | * Test multiple add functionality. | |
103 | * | |
104 | * This needs review for api v4 as it makes for a very non standard api. | |
105 | */ | |
00be9182 | 106 | public function testAddDouble() { |
6a488035 | 107 | |
24602943 | 108 | $result = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 TO |
109 | $this->assertEquals($result['added'], 1); |
110 | ||
9099cab3 | 111 | $params = [ |
24602943 | 112 | 'contact_id_i' => $this->_individualID, |
113 | 'contact_id_o' => $this->_organizationID, | |
114 | 'tag_id' => $this->_tagID, | |
9099cab3 | 115 | ]; |
6a488035 | 116 | |
fc928539 | 117 | $result = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 TO |
118 | $this->assertEquals($result['added'], 1); |
119 | $this->assertEquals($result['not_added'], 1); | |
120 | } | |
121 | ||
28a04ea9 | 122 | /** |
24602943 | 123 | * Test that get works without an entity. |
28a04ea9 | 124 | */ |
00be9182 | 125 | public function testGetNoEntityID() { |
24602943 | 126 | $this->callAPISuccess('entity_tag', 'create', $this->_params); |
9099cab3 | 127 | $result = $this->callAPISuccess($this->_entity, 'get', ['sequential' => 1, 'tag_id' => $this->_tagID]); |
24602943 | 128 | $this->assertEquals($this->_individualID, $result['values'][0]['entity_id']); |
6a488035 TO |
129 | } |
130 | ||
24602943 | 131 | /** |
132 | * Basic get functionality test. | |
2d932085 CW |
133 | * @param int $version |
134 | * @dataProvider versionThreeAndFour | |
24602943 | 135 | */ |
2d932085 CW |
136 | public function testIndividualEntityTagGet($version) { |
137 | $this->_apiversion = $version; | |
24602943 | 138 | $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 | 139 | |
9099cab3 | 140 | $paramsEntity = [ |
24602943 | 141 | 'contact_id' => $this->_individualID, |
9099cab3 | 142 | ]; |
2d932085 CW |
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']); | |
6a488035 TO |
146 | } |
147 | ||
ffcc1d11 | 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 | ||
24602943 | 161 | /** |
162 | * Test tag can be added to a household. | |
163 | */ | |
164 | public function testHouseholdEntityCreate() { | |
9099cab3 | 165 | $params = [ |
24602943 | 166 | 'contact_id' => $this->_householdID, |
167 | 'tag_id' => $this->_tagID, | |
9099cab3 | 168 | ]; |
6a488035 | 169 | |
fc928539 | 170 | $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 | 171 | $this->assertEquals($householdEntity['added'], 1); |
6a488035 TO |
172 | } |
173 | ||
24602943 | 174 | /** |
175 | * Test tag can be added to an organization. | |
2d932085 CW |
176 | * @param int $version |
177 | * @dataProvider versionThreeAndFour | |
24602943 | 178 | */ |
2d932085 CW |
179 | public function testOrganizationEntityGet($version) { |
180 | $this->_apiversion = $version; | |
24602943 | 181 | |
9099cab3 | 182 | $params = [ |
2d932085 | 183 | 'entity_id' => $this->_organizationID, |
24602943 | 184 | 'tag_id' => $this->_tagID, |
9099cab3 | 185 | ]; |
6a488035 | 186 | |
2d932085 | 187 | $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 | 188 | |
9099cab3 | 189 | $tag = $this->callAPISuccess('entity_tag', 'getsingle', ['contact_id' => $this->_organizationID]); |
2d932085 CW |
190 | $this->assertEquals($this->_organizationID, $tag['entity_id']); |
191 | $this->assertEquals($this->_tagID, $tag['tag_id']); | |
6a488035 TO |
192 | } |
193 | ||
28a04ea9 | 194 | /** |
24602943 | 195 | * Civicrm_entity_tag_Delete methods. |
28a04ea9 | 196 | */ |
00be9182 | 197 | public function testEntityTagDeleteNoTagId() { |
9099cab3 | 198 | $entityTagParams = [ |
6a488035 TO |
199 | 'contact_id_i' => $this->_individualID, |
200 | 'contact_id_h' => $this->_householdID, | |
201 | 'tag_id' => $this->_tagID, | |
9099cab3 | 202 | ]; |
6a488035 TO |
203 | $this->entityTagAdd($entityTagParams); |
204 | ||
9099cab3 | 205 | $params = [ |
6a488035 TO |
206 | 'contact_id_i' => $this->_individualID, |
207 | 'contact_id_h' => $this->_householdID, | |
9099cab3 | 208 | ]; |
6a488035 | 209 | |
c3a18b64 WDC |
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); | |
6a488035 TO |
215 | } |
216 | ||
00be9182 | 217 | public function testEntityTagDeleteINDHH() { |
9099cab3 | 218 | $entityTagParams = [ |
6a488035 TO |
219 | 'contact_id_i' => $this->_individualID, |
220 | 'contact_id_h' => $this->_householdID, | |
221 | 'tag_id' => $this->_tagID, | |
9099cab3 | 222 | ]; |
6a488035 TO |
223 | $this->entityTagAdd($entityTagParams); |
224 | ||
9099cab3 | 225 | $params = [ |
6a488035 TO |
226 | 'contact_id_i' => $this->_individualID, |
227 | 'contact_id_h' => $this->_householdID, | |
228 | 'tag_id' => $this->_tagID, | |
9099cab3 | 229 | ]; |
6a488035 | 230 | |
fc928539 | 231 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 | 232 | |
6a488035 TO |
233 | $this->assertEquals($result['removed'], 2); |
234 | } | |
235 | ||
00be9182 | 236 | public function testEntityTagDeleteHH() { |
9099cab3 | 237 | $entityTagParams = [ |
6a488035 TO |
238 | 'contact_id_i' => $this->_individualID, |
239 | 'contact_id_h' => $this->_householdID, | |
240 | 'tag_id' => $this->_tagID, | |
9099cab3 | 241 | ]; |
6a488035 TO |
242 | $this->entityTagAdd($entityTagParams); |
243 | ||
9099cab3 | 244 | $params = [ |
6a488035 TO |
245 | 'contact_id_h' => $this->_householdID, |
246 | 'tag_id' => $this->_tagID, | |
9099cab3 | 247 | ]; |
6a488035 | 248 | |
fc928539 | 249 | $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
250 | $this->assertEquals($result['removed'], 1); |
251 | } | |
252 | ||
00be9182 | 253 | public function testEntityTagDeleteHHORG() { |
9099cab3 | 254 | $entityTagParams = [ |
6a488035 TO |
255 | 'contact_id_i' => $this->_individualID, |
256 | 'contact_id_h' => $this->_householdID, | |
257 | 'tag_id' => $this->_tagID, | |
9099cab3 | 258 | ]; |
6a488035 TO |
259 | $this->entityTagAdd($entityTagParams); |
260 | ||
9099cab3 | 261 | $params = [ |
6a488035 TO |
262 | 'contact_id_h' => $this->_householdID, |
263 | 'contact_id_o' => $this->_organizationID, | |
264 | 'tag_id' => $this->_tagID, | |
9099cab3 | 265 | ]; |
6a488035 | 266 | |
fc928539 | 267 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
268 | $this->assertEquals($result['removed'], 1); |
269 | $this->assertEquals($result['not_removed'], 1); | |
270 | } | |
271 | ||
00be9182 | 272 | public function testEntityTagCommonDeleteINDHH() { |
9099cab3 | 273 | $entityTagParams = [ |
6a488035 TO |
274 | 'contact_id_i' => $this->_individualID, |
275 | 'contact_id_h' => $this->_householdID, | |
276 | 'tag_id' => $this->_tagID, | |
9099cab3 | 277 | ]; |
6a488035 TO |
278 | $this->entityTagAdd($entityTagParams); |
279 | ||
9099cab3 | 280 | $params = [ |
6a488035 TO |
281 | 'contact_id_i' => $this->_individualID, |
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'], 2); |
288 | } | |
289 | ||
00be9182 | 290 | public function testEntityTagCommonDeleteHH() { |
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 | 'tag_id' => $this->_tagID, | |
9099cab3 | 301 | ]; |
6a488035 | 302 | |
fc928539 | 303 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
304 | $this->assertEquals($result['removed'], 1); |
305 | } | |
306 | ||
00be9182 | 307 | public function testEntityTagCommonDeleteHHORG() { |
9099cab3 | 308 | $entityTagParams = [ |
6a488035 TO |
309 | 'contact_id_i' => $this->_individualID, |
310 | 'contact_id_h' => $this->_householdID, | |
311 | 'tag_id' => $this->_tagID, | |
9099cab3 | 312 | ]; |
6a488035 TO |
313 | $this->entityTagAdd($entityTagParams); |
314 | ||
9099cab3 | 315 | $params = [ |
6a488035 TO |
316 | 'contact_id_h' => $this->_householdID, |
317 | 'contact_id_o' => $this->_organizationID, | |
318 | 'tag_id' => $this->_tagID, | |
9099cab3 | 319 | ]; |
6a488035 | 320 | |
fc928539 | 321 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
322 | $this->assertEquals($result['removed'], 1); |
323 | $this->assertEquals($result['not_removed'], 1); | |
324 | } | |
96025800 | 325 | |
466fce54 | 326 | public function testEntityTagJoin() { |
9099cab3 | 327 | $org = $this->callAPISuccess('Contact', 'create', [ |
466fce54 CW |
328 | 'contact_type' => 'Organization', |
329 | 'organization_name' => 'Org123', | |
9099cab3 | 330 | 'api.EntityTag.create' => [ |
466fce54 | 331 | 'tag_id' => $this->_tagID, |
9099cab3 CW |
332 | ], |
333 | ]); | |
466fce54 | 334 | // Fetch contact info via join |
9099cab3 CW |
335 | $result = $this->callAPISuccessGetSingle('EntityTag', [ |
336 | 'return' => ["entity_id.organization_name", "tag_id.name"], | |
466fce54 CW |
337 | 'entity_id' => $org['id'], |
338 | 'entity_table' => "civicrm_contact", | |
9099cab3 | 339 | ]); |
466fce54 CW |
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 | |
9099cab3 CW |
343 | $result = $this->callAPISuccess('EntityTag', 'get', [ |
344 | 'return' => ["entity_id.organization_name"], | |
466fce54 CW |
345 | 'entity_id' => $org['id'], |
346 | 'entity_table' => "civicrm_contact", | |
347 | 'entity_id.contact_type' => "Individual", | |
9099cab3 | 348 | ]); |
466fce54 CW |
349 | $this->assertEquals(0, $result['count']); |
350 | } | |
351 | ||
6a488035 | 352 | } |