3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_entity_tag_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Core
36 * Class api_v3_EntityTagTest.
39 class api_v3_EntityTagTest
extends CiviUnitTestCase
{
44 protected $_individualID;
45 protected $_householdID;
46 protected $_organizationID;
48 protected $_apiversion = 3;
50 protected $_entity = 'entity_tag';
53 * Basic parameters for create.
57 protected $_params = [];
62 public function setUp() {
64 $this->useTransaction(TRUE);
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();
72 'entity_id' => $this->_individualID
,
73 'tag_id' => $this->_tagID
,
78 * Test required parameters.
80 * These failure tests are low value and may not be worth putting in v4.
82 public function testFailureTests() {
83 $this->callAPIFailure('entity_tag', 'create', ['contact_id' => $this->_individualID
],
84 'tag_id is a required field'
86 $this->callAPIFailure('entity_tag', 'create', ['tag_id' => $this->_tagID
],
87 'contact_id is a required field'
94 * @dataProvider versionThreeAndFour
96 public function testContactEntityTagCreate($version) {
97 $this->_apiversion
= $version;
98 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
102 * Test multiple add functionality.
104 * This needs review for api v4 as it makes for a very non standard api.
106 public function testAddDouble() {
108 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
109 $this->assertEquals($result['added'], 1);
112 'contact_id_i' => $this->_individualID
,
113 'contact_id_o' => $this->_organizationID
,
114 'tag_id' => $this->_tagID
,
117 $result = $this->callAPISuccess('entity_tag', 'create', $params);
118 $this->assertEquals($result['added'], 1);
119 $this->assertEquals($result['not_added'], 1);
123 * Test that get works without an entity.
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']);
132 * Basic get functionality test.
133 * @param int $version
134 * @dataProvider versionThreeAndFour
136 public function testIndividualEntityTagGet($version) {
137 $this->_apiversion
= $version;
138 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
141 'contact_id' => $this->_individualID
,
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']);
149 * Test memory usage does not escalate crazily.
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();
157 $max = $start +
2000000;
158 $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) ");
162 * Test tag can be added to a household.
164 public function testHouseholdEntityCreate() {
166 'contact_id' => $this->_householdID
,
167 'tag_id' => $this->_tagID
,
170 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
171 $this->assertEquals($householdEntity['added'], 1);
175 * Test tag can be added to an organization.
176 * @param int $version
177 * @dataProvider versionThreeAndFour
179 public function testOrganizationEntityGet($version) {
180 $this->_apiversion
= $version;
183 'entity_id' => $this->_organizationID
,
184 'tag_id' => $this->_tagID
,
187 $this->callAPISuccess('entity_tag', 'create', $params);
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']);
195 * Civicrm_entity_tag_Delete methods.
197 public function testEntityTagDeleteNoTagId() {
199 'contact_id_i' => $this->_individualID
,
200 'contact_id_h' => $this->_householdID
,
201 'tag_id' => $this->_tagID
,
203 $this->entityTagAdd($entityTagParams);
206 'contact_id_i' => $this->_individualID
,
207 'contact_id_h' => $this->_householdID
,
210 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
212 $this->assertEquals($result['not_removed'], 0);
213 $this->assertEquals($result['removed'], 2);
214 $this->assertEquals($result['total_count'], 2);
217 public function testEntityTagDeleteINDHH() {
219 'contact_id_i' => $this->_individualID
,
220 'contact_id_h' => $this->_householdID
,
221 'tag_id' => $this->_tagID
,
223 $this->entityTagAdd($entityTagParams);
226 'contact_id_i' => $this->_individualID
,
227 'contact_id_h' => $this->_householdID
,
228 'tag_id' => $this->_tagID
,
231 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
233 $this->assertEquals($result['removed'], 2);
236 public function testEntityTagDeleteHH() {
238 'contact_id_i' => $this->_individualID
,
239 'contact_id_h' => $this->_householdID
,
240 'tag_id' => $this->_tagID
,
242 $this->entityTagAdd($entityTagParams);
245 'contact_id_h' => $this->_householdID
,
246 'tag_id' => $this->_tagID
,
249 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__
, __FILE__
);
250 $this->assertEquals($result['removed'], 1);
253 public function testEntityTagDeleteHHORG() {
255 'contact_id_i' => $this->_individualID
,
256 'contact_id_h' => $this->_householdID
,
257 'tag_id' => $this->_tagID
,
259 $this->entityTagAdd($entityTagParams);
262 'contact_id_h' => $this->_householdID
,
263 'contact_id_o' => $this->_organizationID
,
264 'tag_id' => $this->_tagID
,
267 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
268 $this->assertEquals($result['removed'], 1);
269 $this->assertEquals($result['not_removed'], 1);
272 public function testEntityTagCommonDeleteINDHH() {
274 'contact_id_i' => $this->_individualID
,
275 'contact_id_h' => $this->_householdID
,
276 'tag_id' => $this->_tagID
,
278 $this->entityTagAdd($entityTagParams);
281 'contact_id_i' => $this->_individualID
,
282 'contact_id_h' => $this->_householdID
,
283 'tag_id' => $this->_tagID
,
286 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
287 $this->assertEquals($result['removed'], 2);
290 public function testEntityTagCommonDeleteHH() {
292 'contact_id_i' => $this->_individualID
,
293 'contact_id_h' => $this->_householdID
,
294 'tag_id' => $this->_tagID
,
296 $this->entityTagAdd($entityTagParams);
299 'contact_id_h' => $this->_householdID
,
300 'tag_id' => $this->_tagID
,
303 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
304 $this->assertEquals($result['removed'], 1);
307 public function testEntityTagCommonDeleteHHORG() {
309 'contact_id_i' => $this->_individualID
,
310 'contact_id_h' => $this->_householdID
,
311 'tag_id' => $this->_tagID
,
313 $this->entityTagAdd($entityTagParams);
316 'contact_id_h' => $this->_householdID
,
317 'contact_id_o' => $this->_organizationID
,
318 'tag_id' => $this->_tagID
,
321 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
322 $this->assertEquals($result['removed'], 1);
323 $this->assertEquals($result['not_removed'], 1);
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
,
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",
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",
349 $this->assertEquals(0, $result['count']);