3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 tag can be added to a household.
145 public function testHouseholdEntityCreate() {
147 'contact_id' => $this->_householdID
,
148 'tag_id' => $this->_tagID
,
151 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
152 $this->assertEquals($householdEntity['added'], 1);
156 * Test tag can be added to an organization.
158 public function testOrganizationEntityGet() {
161 'contact_id' => $this->_organizationID
,
162 'tag_id' => $this->_tagID
,
165 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
166 $this->assertEquals($organizationEntity['added'], 1);
168 $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID
));
172 * Civicrm_entity_tag_Delete methods.
174 public function testEntityTagDeleteNoTagId() {
175 $entityTagParams = array(
176 'contact_id_i' => $this->_individualID
,
177 'contact_id_h' => $this->_householdID
,
178 'tag_id' => $this->_tagID
,
180 $this->entityTagAdd($entityTagParams);
183 'contact_id_i' => $this->_individualID
,
184 'contact_id_h' => $this->_householdID
,
187 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
189 $this->assertEquals($result['not_removed'], 0);
190 $this->assertEquals($result['removed'], 2);
191 $this->assertEquals($result['total_count'], 2);
194 public function testEntityTagDeleteINDHH() {
195 $entityTagParams = array(
196 'contact_id_i' => $this->_individualID
,
197 'contact_id_h' => $this->_householdID
,
198 'tag_id' => $this->_tagID
,
200 $this->entityTagAdd($entityTagParams);
203 'contact_id_i' => $this->_individualID
,
204 'contact_id_h' => $this->_householdID
,
205 'tag_id' => $this->_tagID
,
208 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
210 $this->assertEquals($result['removed'], 2);
213 public function testEntityTagDeleteHH() {
214 $entityTagParams = array(
215 'contact_id_i' => $this->_individualID
,
216 'contact_id_h' => $this->_householdID
,
217 'tag_id' => $this->_tagID
,
219 $this->entityTagAdd($entityTagParams);
222 'contact_id_h' => $this->_householdID
,
223 'tag_id' => $this->_tagID
,
226 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__
, __FILE__
);
227 $this->assertEquals($result['removed'], 1);
230 public function testEntityTagDeleteHHORG() {
231 $entityTagParams = array(
232 'contact_id_i' => $this->_individualID
,
233 'contact_id_h' => $this->_householdID
,
234 'tag_id' => $this->_tagID
,
236 $this->entityTagAdd($entityTagParams);
239 'contact_id_h' => $this->_householdID
,
240 'contact_id_o' => $this->_organizationID
,
241 'tag_id' => $this->_tagID
,
244 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
245 $this->assertEquals($result['removed'], 1);
246 $this->assertEquals($result['not_removed'], 1);
249 public function testEntityTagCommonDeleteINDHH() {
250 $entityTagParams = array(
251 'contact_id_i' => $this->_individualID
,
252 'contact_id_h' => $this->_householdID
,
253 'tag_id' => $this->_tagID
,
255 $this->entityTagAdd($entityTagParams);
258 'contact_id_i' => $this->_individualID
,
259 'contact_id_h' => $this->_householdID
,
260 'tag_id' => $this->_tagID
,
263 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
264 $this->assertEquals($result['removed'], 2);
267 public function testEntityTagCommonDeleteHH() {
268 $entityTagParams = array(
269 'contact_id_i' => $this->_individualID
,
270 'contact_id_h' => $this->_householdID
,
271 'tag_id' => $this->_tagID
,
273 $this->entityTagAdd($entityTagParams);
276 'contact_id_h' => $this->_householdID
,
277 'tag_id' => $this->_tagID
,
280 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
281 $this->assertEquals($result['removed'], 1);
284 public function testEntityTagCommonDeleteHHORG() {
285 $entityTagParams = array(
286 'contact_id_i' => $this->_individualID
,
287 'contact_id_h' => $this->_householdID
,
288 'tag_id' => $this->_tagID
,
290 $this->entityTagAdd($entityTagParams);
293 'contact_id_h' => $this->_householdID
,
294 'contact_id_o' => $this->_organizationID
,
295 'tag_id' => $this->_tagID
,
298 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
299 $this->assertEquals($result['removed'], 1);
300 $this->assertEquals($result['not_removed'], 1);
303 public function testEntityTagJoin() {
304 $org = $this->callAPISuccess('Contact', 'create', array(
305 'contact_type' => 'Organization',
306 'organization_name' => 'Org123',
307 'api.EntityTag.create' => array(
308 'tag_id' => $this->_tagID
,
311 // Fetch contact info via join
312 $result = $this->callAPISuccessGetSingle('EntityTag', array(
313 'return' => array("entity_id.organization_name", "tag_id.name"),
314 'entity_id' => $org['id'],
315 'entity_table' => "civicrm_contact",
317 $this->assertEquals('Org123', $result['entity_id.organization_name']);
318 $this->assertEquals('EntityTagTest', $result['tag_id.name']);
319 // This should return no results by restricting contact_type
320 $result = $this->callAPISuccess('EntityTag', 'get', array(
321 'return' => array("entity_id.organization_name"),
322 'entity_id' => $org['id'],
323 'entity_table' => "civicrm_contact",
324 'entity_id.contact_type' => "Individual",
326 $this->assertEquals(0, $result['count']);