3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_entity_tag_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Core
20 * Class api_v3_EntityTagTest.
23 class api_v3_EntityTagTest
extends CiviUnitTestCase
{
28 protected $_individualID;
29 protected $_householdID;
30 protected $_organizationID;
32 protected $_apiversion = 3;
34 protected $_entity = 'entity_tag';
37 * Basic parameters for create.
41 protected $_params = [];
46 public function setUp() {
48 $this->useTransaction(TRUE);
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();
56 'entity_id' => $this->_individualID
,
57 'tag_id' => $this->_tagID
,
62 * Test required parameters.
64 * These failure tests are low value and may not be worth putting in v4.
66 public function testFailureTests() {
67 $this->callAPIFailure('entity_tag', 'create', ['contact_id' => $this->_individualID
],
68 'tag_id is a required field'
70 $this->callAPIFailure('entity_tag', 'create', ['tag_id' => $this->_tagID
],
71 'contact_id is a required field'
78 * @dataProvider versionThreeAndFour
80 public function testContactEntityTagCreate($version) {
81 $this->_apiversion
= $version;
82 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
86 * Test multiple add functionality.
88 * This needs review for api v4 as it makes for a very non standard api.
90 public function testAddDouble() {
92 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
93 $this->assertEquals($result['added'], 1);
96 'contact_id_i' => $this->_individualID
,
97 'contact_id_o' => $this->_organizationID
,
98 'tag_id' => $this->_tagID
,
101 $result = $this->callAPISuccess('entity_tag', 'create', $params);
102 $this->assertEquals($result['added'], 1);
103 $this->assertEquals($result['not_added'], 1);
107 * Test that get works without an entity.
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']);
116 * Basic get functionality test.
117 * @param int $version
118 * @dataProvider versionThreeAndFour
120 public function testIndividualEntityTagGet($version) {
121 $this->_apiversion
= $version;
122 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
125 'contact_id' => $this->_individualID
,
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']);
133 * Test memory usage does not escalate crazily.
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();
141 $max = $start +
2000000;
142 $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) ");
146 * Test tag can be added to a household.
148 public function testHouseholdEntityCreate() {
150 'contact_id' => $this->_householdID
,
151 'tag_id' => $this->_tagID
,
154 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
155 $this->assertEquals($householdEntity['added'], 1);
159 * Test tag can be added to an organization.
160 * @param int $version
161 * @dataProvider versionThreeAndFour
163 public function testOrganizationEntityGet($version) {
164 $this->_apiversion
= $version;
167 'entity_id' => $this->_organizationID
,
168 'tag_id' => $this->_tagID
,
171 $this->callAPISuccess('entity_tag', 'create', $params);
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']);
179 * Civicrm_entity_tag_Delete methods.
181 public function testEntityTagDeleteNoTagId() {
183 'contact_id_i' => $this->_individualID
,
184 'contact_id_h' => $this->_householdID
,
185 'tag_id' => $this->_tagID
,
187 $this->entityTagAdd($entityTagParams);
190 'contact_id_i' => $this->_individualID
,
191 'contact_id_h' => $this->_householdID
,
194 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
196 $this->assertEquals($result['not_removed'], 0);
197 $this->assertEquals($result['removed'], 2);
198 $this->assertEquals($result['total_count'], 2);
201 public function testEntityTagDeleteINDHH() {
203 'contact_id_i' => $this->_individualID
,
204 'contact_id_h' => $this->_householdID
,
205 'tag_id' => $this->_tagID
,
207 $this->entityTagAdd($entityTagParams);
210 'contact_id_i' => $this->_individualID
,
211 'contact_id_h' => $this->_householdID
,
212 'tag_id' => $this->_tagID
,
215 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
217 $this->assertEquals($result['removed'], 2);
220 public function testEntityTagDeleteHH() {
222 'contact_id_i' => $this->_individualID
,
223 'contact_id_h' => $this->_householdID
,
224 'tag_id' => $this->_tagID
,
226 $this->entityTagAdd($entityTagParams);
229 'contact_id_h' => $this->_householdID
,
230 'tag_id' => $this->_tagID
,
233 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__
, __FILE__
);
234 $this->assertEquals($result['removed'], 1);
237 public function testEntityTagDeleteHHORG() {
239 'contact_id_i' => $this->_individualID
,
240 'contact_id_h' => $this->_householdID
,
241 'tag_id' => $this->_tagID
,
243 $this->entityTagAdd($entityTagParams);
246 'contact_id_h' => $this->_householdID
,
247 'contact_id_o' => $this->_organizationID
,
248 'tag_id' => $this->_tagID
,
251 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
252 $this->assertEquals($result['removed'], 1);
253 $this->assertEquals($result['not_removed'], 1);
256 public function testEntityTagCommonDeleteINDHH() {
258 'contact_id_i' => $this->_individualID
,
259 'contact_id_h' => $this->_householdID
,
260 'tag_id' => $this->_tagID
,
262 $this->entityTagAdd($entityTagParams);
265 'contact_id_i' => $this->_individualID
,
266 'contact_id_h' => $this->_householdID
,
267 'tag_id' => $this->_tagID
,
270 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
271 $this->assertEquals($result['removed'], 2);
274 public function testEntityTagCommonDeleteHH() {
276 'contact_id_i' => $this->_individualID
,
277 'contact_id_h' => $this->_householdID
,
278 'tag_id' => $this->_tagID
,
280 $this->entityTagAdd($entityTagParams);
283 'contact_id_h' => $this->_householdID
,
284 'tag_id' => $this->_tagID
,
287 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
288 $this->assertEquals($result['removed'], 1);
291 public function testEntityTagCommonDeleteHHORG() {
293 'contact_id_i' => $this->_individualID
,
294 'contact_id_h' => $this->_householdID
,
295 'tag_id' => $this->_tagID
,
297 $this->entityTagAdd($entityTagParams);
300 'contact_id_h' => $this->_householdID
,
301 'contact_id_o' => $this->_organizationID
,
302 'tag_id' => $this->_tagID
,
305 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
306 $this->assertEquals($result['removed'], 1);
307 $this->assertEquals($result['not_removed'], 1);
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
,
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",
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",
333 $this->assertEquals(0, $result['count']);