Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
39class 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 */
57 protected $_params = array();
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();
fb32de45 67 $this->_tag = $this->tagCreate();
6a488035 68 $this->_tagID = $this->_tag['id'];
678fbf79 69 $this->_householdID = $this->houseHoldCreate();
70 $this->_organizationID = $this->organizationCreate();
24602943 71 $this->_params = array(
72 'contact_id' => $this->_individualID,
73 'tag_id' => $this->_tagID,
fc928539 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() {
83 $this->callAPIFailure('entity_tag', 'create', array('contact_id' => $this->_individualID),
fc928539 84 'tag_id is a required field'
85 );
24602943 86 $this->callAPIFailure('entity_tag', 'create', array('tag_id' => $this->_tagID),
87 'contact_id is a required field'
6a488035 88 );
6a488035
TO
89 }
90
24602943 91 /**
92 * Test basic create.
93 */
00be9182 94 public function testContactEntityTagCreate() {
24602943 95 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
6a488035
TO
96 $this->assertEquals($result['added'], 1);
97 }
98
24602943 99 /**
100 * Test multiple add functionality.
101 *
102 * This needs review for api v4 as it makes for a very non standard api.
103 */
00be9182 104 public function testAddDouble() {
6a488035 105
24602943 106 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
6a488035
TO
107 $this->assertEquals($result['added'], 1);
108
109 $params = array(
24602943 110 'contact_id_i' => $this->_individualID,
111 'contact_id_o' => $this->_organizationID,
112 'tag_id' => $this->_tagID,
6a488035
TO
113 );
114
fc928539 115 $result = $this->callAPISuccess('entity_tag', 'create', $params);
6a488035
TO
116 $this->assertEquals($result['added'], 1);
117 $this->assertEquals($result['not_added'], 1);
118 }
119
28a04ea9 120 /**
24602943 121 * Test that get works without an entity.
28a04ea9 122 */
00be9182 123 public function testGetNoEntityID() {
24602943 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']);
6a488035
TO
127 }
128
24602943 129 /**
130 * Basic get functionality test.
131 */
00be9182 132 public function testIndividualEntityTagGet() {
24602943 133 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params);
6a488035
TO
134 $this->assertEquals($individualEntity['added'], 1);
135
136 $paramsEntity = array(
24602943 137 'contact_id' => $this->_individualID,
6a488035 138 );
24602943 139 $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
6a488035
TO
140 }
141
24602943 142 /**
143 * Test tag can be added to a household.
144 */
145 public function testHouseholdEntityCreate() {
92915c55 146 $params = array(
24602943 147 'contact_id' => $this->_householdID,
148 'tag_id' => $this->_tagID,
6a488035
TO
149 );
150
fc928539 151 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
6a488035 152 $this->assertEquals($householdEntity['added'], 1);
6a488035
TO
153 }
154
24602943 155 /**
156 * Test tag can be added to an organization.
157 */
00be9182 158 public function testOrganizationEntityGet() {
24602943 159
92915c55 160 $params = array(
24602943 161 'contact_id' => $this->_organizationID,
162 'tag_id' => $this->_tagID,
6a488035
TO
163 );
164
fc928539 165 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
6a488035
TO
166 $this->assertEquals($organizationEntity['added'], 1);
167
24602943 168 $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID));
6a488035
TO
169 }
170
28a04ea9 171 /**
24602943 172 * Civicrm_entity_tag_Delete methods.
28a04ea9 173 */
00be9182 174 public function testEntityTagDeleteNoTagId() {
6a488035
TO
175 $entityTagParams = array(
176 'contact_id_i' => $this->_individualID,
177 'contact_id_h' => $this->_householdID,
178 'tag_id' => $this->_tagID,
6a488035
TO
179 );
180 $this->entityTagAdd($entityTagParams);
181
182 $params = array(
183 'contact_id_i' => $this->_individualID,
184 'contact_id_h' => $this->_householdID,
6a488035
TO
185 );
186
c3a18b64
WDC
187 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
188
189 $this->assertEquals($result['not_removed'], 0);
190 $this->assertEquals($result['removed'], 2);
191 $this->assertEquals($result['total_count'], 2);
6a488035
TO
192 }
193
00be9182 194 public function testEntityTagDeleteINDHH() {
6a488035
TO
195 $entityTagParams = array(
196 'contact_id_i' => $this->_individualID,
197 'contact_id_h' => $this->_householdID,
198 'tag_id' => $this->_tagID,
6a488035
TO
199 );
200 $this->entityTagAdd($entityTagParams);
201
202 $params = array(
203 'contact_id_i' => $this->_individualID,
204 'contact_id_h' => $this->_householdID,
205 'tag_id' => $this->_tagID,
6a488035
TO
206 );
207
fc928539 208 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
6a488035 209
6a488035
TO
210 $this->assertEquals($result['removed'], 2);
211 }
212
00be9182 213 public function testEntityTagDeleteHH() {
6a488035
TO
214 $entityTagParams = array(
215 'contact_id_i' => $this->_individualID,
216 'contact_id_h' => $this->_householdID,
217 'tag_id' => $this->_tagID,
6a488035
TO
218 );
219 $this->entityTagAdd($entityTagParams);
220
221 $params = array(
222 'contact_id_h' => $this->_householdID,
223 'tag_id' => $this->_tagID,
6a488035
TO
224 );
225
fc928539 226 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
227 $this->assertEquals($result['removed'], 1);
228 }
229
00be9182 230 public function testEntityTagDeleteHHORG() {
6a488035
TO
231 $entityTagParams = array(
232 'contact_id_i' => $this->_individualID,
233 'contact_id_h' => $this->_householdID,
234 'tag_id' => $this->_tagID,
6a488035
TO
235 );
236 $this->entityTagAdd($entityTagParams);
237
238 $params = array(
239 'contact_id_h' => $this->_householdID,
240 'contact_id_o' => $this->_organizationID,
241 'tag_id' => $this->_tagID,
6a488035
TO
242 );
243
fc928539 244 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
6a488035
TO
245 $this->assertEquals($result['removed'], 1);
246 $this->assertEquals($result['not_removed'], 1);
247 }
248
00be9182 249 public function testEntityTagCommonDeleteINDHH() {
6a488035
TO
250 $entityTagParams = array(
251 'contact_id_i' => $this->_individualID,
252 'contact_id_h' => $this->_householdID,
253 'tag_id' => $this->_tagID,
6a488035
TO
254 );
255 $this->entityTagAdd($entityTagParams);
256
257 $params = array(
258 'contact_id_i' => $this->_individualID,
259 'contact_id_h' => $this->_householdID,
260 'tag_id' => $this->_tagID,
6a488035
TO
261 );
262
fc928539 263 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
6a488035
TO
264 $this->assertEquals($result['removed'], 2);
265 }
266
00be9182 267 public function testEntityTagCommonDeleteHH() {
6a488035
TO
268 $entityTagParams = array(
269 'contact_id_i' => $this->_individualID,
270 'contact_id_h' => $this->_householdID,
271 'tag_id' => $this->_tagID,
6a488035
TO
272 );
273 $this->entityTagAdd($entityTagParams);
274
275 $params = array(
276 'contact_id_h' => $this->_householdID,
277 'tag_id' => $this->_tagID,
6a488035
TO
278 );
279
fc928539 280 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
6a488035
TO
281 $this->assertEquals($result['removed'], 1);
282 }
283
00be9182 284 public function testEntityTagCommonDeleteHHORG() {
6a488035
TO
285 $entityTagParams = array(
286 'contact_id_i' => $this->_individualID,
287 'contact_id_h' => $this->_householdID,
288 'tag_id' => $this->_tagID,
6a488035
TO
289 );
290 $this->entityTagAdd($entityTagParams);
291
292 $params = array(
293 'contact_id_h' => $this->_householdID,
294 'contact_id_o' => $this->_organizationID,
295 'tag_id' => $this->_tagID,
6a488035
TO
296 );
297
fc928539 298 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
6a488035
TO
299 $this->assertEquals($result['removed'], 1);
300 $this->assertEquals($result['not_removed'], 1);
301 }
96025800 302
6a488035 303}