3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 = array();
62 public function setUp() {
64 $this->useTransaction(TRUE);
66 $this->_individualID
= $this->individualCreate();
67 $this->_tag
= $this->tagCreate(array('name' => 'EntityTagTest'));
68 $this->_tagID
= $this->_tag
['id'];
69 $this->_householdID
= $this->houseHoldCreate();
70 $this->_organizationID
= $this->organizationCreate();
71 $this->_params
= array(
72 'contact_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', array('contact_id' => $this->_individualID
),
84 'tag_id is a required field'
86 $this->callAPIFailure('entity_tag', 'create', array('tag_id' => $this->_tagID
),
87 'contact_id is a required field'
94 public function testContactEntityTagCreate() {
95 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
96 $this->assertEquals($result['added'], 1);
100 * Test multiple add functionality.
102 * This needs review for api v4 as it makes for a very non standard api.
104 public function testAddDouble() {
106 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
107 $this->assertEquals($result['added'], 1);
110 'contact_id_i' => $this->_individualID
,
111 'contact_id_o' => $this->_organizationID
,
112 'tag_id' => $this->_tagID
,
115 $result = $this->callAPISuccess('entity_tag', 'create', $params);
116 $this->assertEquals($result['added'], 1);
117 $this->assertEquals($result['not_added'], 1);
121 * Test that get works without an entity.
123 public function testGetNoEntityID() {
124 $this->callAPISuccess('entity_tag', 'create', $this->_params
);
125 $result = $this->callAPISuccess($this->_entity
, 'get', array('sequential' => 1, 'tag_id' => $this->_tagID
));
126 $this->assertEquals($this->_individualID
, $result['values'][0]['entity_id']);
130 * Basic get functionality test.
132 public function testIndividualEntityTagGet() {
133 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params
);
134 $this->assertEquals($individualEntity['added'], 1);
136 $paramsEntity = array(
137 'contact_id' => $this->_individualID
,
139 $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__
, __FILE__
);
143 * Test memory usage does not escalate crazily.
145 public function testMemoryLeak() {
146 $start = memory_get_usage();
147 for ($i = 0; $i < 100; $i++
) {
148 $this->callAPISuccess('EntityTag', 'get', []);
149 $memUsage = memory_get_usage();
151 $max = $start +
2000000;
152 $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) ");
156 * Test tag can be added to a household.
158 public function testHouseholdEntityCreate() {
160 'contact_id' => $this->_householdID
,
161 'tag_id' => $this->_tagID
,
164 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
165 $this->assertEquals($householdEntity['added'], 1);
169 * Test tag can be added to an organization.
171 public function testOrganizationEntityGet() {
174 'contact_id' => $this->_organizationID
,
175 'tag_id' => $this->_tagID
,
178 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
179 $this->assertEquals($organizationEntity['added'], 1);
181 $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID
));
185 * Civicrm_entity_tag_Delete methods.
187 public function testEntityTagDeleteNoTagId() {
188 $entityTagParams = array(
189 'contact_id_i' => $this->_individualID
,
190 'contact_id_h' => $this->_householdID
,
191 'tag_id' => $this->_tagID
,
193 $this->entityTagAdd($entityTagParams);
196 'contact_id_i' => $this->_individualID
,
197 'contact_id_h' => $this->_householdID
,
200 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
202 $this->assertEquals($result['not_removed'], 0);
203 $this->assertEquals($result['removed'], 2);
204 $this->assertEquals($result['total_count'], 2);
207 public function testEntityTagDeleteINDHH() {
208 $entityTagParams = array(
209 'contact_id_i' => $this->_individualID
,
210 'contact_id_h' => $this->_householdID
,
211 'tag_id' => $this->_tagID
,
213 $this->entityTagAdd($entityTagParams);
216 'contact_id_i' => $this->_individualID
,
217 'contact_id_h' => $this->_householdID
,
218 'tag_id' => $this->_tagID
,
221 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
223 $this->assertEquals($result['removed'], 2);
226 public function testEntityTagDeleteHH() {
227 $entityTagParams = array(
228 'contact_id_i' => $this->_individualID
,
229 'contact_id_h' => $this->_householdID
,
230 'tag_id' => $this->_tagID
,
232 $this->entityTagAdd($entityTagParams);
235 'contact_id_h' => $this->_householdID
,
236 'tag_id' => $this->_tagID
,
239 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__
, __FILE__
);
240 $this->assertEquals($result['removed'], 1);
243 public function testEntityTagDeleteHHORG() {
244 $entityTagParams = array(
245 'contact_id_i' => $this->_individualID
,
246 'contact_id_h' => $this->_householdID
,
247 'tag_id' => $this->_tagID
,
249 $this->entityTagAdd($entityTagParams);
252 'contact_id_h' => $this->_householdID
,
253 'contact_id_o' => $this->_organizationID
,
254 'tag_id' => $this->_tagID
,
257 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
258 $this->assertEquals($result['removed'], 1);
259 $this->assertEquals($result['not_removed'], 1);
262 public function testEntityTagCommonDeleteINDHH() {
263 $entityTagParams = array(
264 'contact_id_i' => $this->_individualID
,
265 'contact_id_h' => $this->_householdID
,
266 'tag_id' => $this->_tagID
,
268 $this->entityTagAdd($entityTagParams);
271 'contact_id_i' => $this->_individualID
,
272 'contact_id_h' => $this->_householdID
,
273 'tag_id' => $this->_tagID
,
276 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
277 $this->assertEquals($result['removed'], 2);
280 public function testEntityTagCommonDeleteHH() {
281 $entityTagParams = array(
282 'contact_id_i' => $this->_individualID
,
283 'contact_id_h' => $this->_householdID
,
284 'tag_id' => $this->_tagID
,
286 $this->entityTagAdd($entityTagParams);
289 'contact_id_h' => $this->_householdID
,
290 'tag_id' => $this->_tagID
,
293 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
294 $this->assertEquals($result['removed'], 1);
297 public function testEntityTagCommonDeleteHHORG() {
298 $entityTagParams = array(
299 'contact_id_i' => $this->_individualID
,
300 'contact_id_h' => $this->_householdID
,
301 'tag_id' => $this->_tagID
,
303 $this->entityTagAdd($entityTagParams);
306 'contact_id_h' => $this->_householdID
,
307 'contact_id_o' => $this->_organizationID
,
308 'tag_id' => $this->_tagID
,
311 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
312 $this->assertEquals($result['removed'], 1);
313 $this->assertEquals($result['not_removed'], 1);
316 public function testEntityTagJoin() {
317 $org = $this->callAPISuccess('Contact', 'create', array(
318 'contact_type' => 'Organization',
319 'organization_name' => 'Org123',
320 'api.EntityTag.create' => array(
321 'tag_id' => $this->_tagID
,
324 // Fetch contact info via join
325 $result = $this->callAPISuccessGetSingle('EntityTag', array(
326 'return' => array("entity_id.organization_name", "tag_id.name"),
327 'entity_id' => $org['id'],
328 'entity_table' => "civicrm_contact",
330 $this->assertEquals('Org123', $result['entity_id.organization_name']);
331 $this->assertEquals('EntityTagTest', $result['tag_id.name']);
332 // This should return no results by restricting contact_type
333 $result = $this->callAPISuccess('EntityTag', 'get', array(
334 'return' => array("entity_id.organization_name"),
335 'entity_id' => $org['id'],
336 'entity_table' => "civicrm_contact",
337 'entity_id.contact_type' => "Individual",
339 $this->assertEquals(0, $result['count']);