Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / EntityTagTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_entity_tag_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Core
33 */
34
35 /**
36 * Class api_v3_EntityTagTest.
37 * @group headless
38 */
39 class api_v3_EntityTagTest extends CiviUnitTestCase {
40
41 /**
42 * @var int
43 */
44 protected $_individualID;
45 protected $_householdID;
46 protected $_organizationID;
47 protected $_tagID;
48 protected $_apiversion = 3;
49 protected $_tag;
50 protected $_entity = 'entity_tag';
51
52 /**
53 * Basic parameters for create.
54 *
55 * @var array
56 */
57 protected $_params = array();
58
59 /**
60 * Set up for test.
61 */
62 public function setUp() {
63 parent::setUp();
64 $this->useTransaction(TRUE);
65
66 $this->_individualID = $this->individualCreate();
67 $this->_tag = $this->tagCreate();
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,
74 );
75 }
76
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),
84 'tag_id is a required field'
85 );
86 $this->callAPIFailure('entity_tag', 'create', array('tag_id' => $this->_tagID),
87 'contact_id is a required field'
88 );
89 }
90
91 /**
92 * Test basic create.
93 */
94 public function testContactEntityTagCreate() {
95 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
96 $this->assertEquals($result['added'], 1);
97 }
98
99 /**
100 * Test multiple add functionality.
101 *
102 * This needs review for api v4 as it makes for a very non standard api.
103 */
104 public function testAddDouble() {
105
106 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
107 $this->assertEquals($result['added'], 1);
108
109 $params = array(
110 'contact_id_i' => $this->_individualID,
111 'contact_id_o' => $this->_organizationID,
112 'tag_id' => $this->_tagID,
113 );
114
115 $result = $this->callAPISuccess('entity_tag', 'create', $params);
116 $this->assertEquals($result['added'], 1);
117 $this->assertEquals($result['not_added'], 1);
118 }
119
120 /**
121 * Test that get works without an entity.
122 */
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']);
127 }
128
129 /**
130 * Basic get functionality test.
131 */
132 public function testIndividualEntityTagGet() {
133 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params);
134 $this->assertEquals($individualEntity['added'], 1);
135
136 $paramsEntity = array(
137 'contact_id' => $this->_individualID,
138 );
139 $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
140 }
141
142 /**
143 * Test tag can be added to a household.
144 */
145 public function testHouseholdEntityCreate() {
146 $params = array(
147 'contact_id' => $this->_householdID,
148 'tag_id' => $this->_tagID,
149 );
150
151 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
152 $this->assertEquals($householdEntity['added'], 1);
153 }
154
155 /**
156 * Test tag can be added to an organization.
157 */
158 public function testOrganizationEntityGet() {
159
160 $params = array(
161 'contact_id' => $this->_organizationID,
162 'tag_id' => $this->_tagID,
163 );
164
165 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
166 $this->assertEquals($organizationEntity['added'], 1);
167
168 $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID));
169 }
170
171 /**
172 * Civicrm_entity_tag_Delete methods.
173 */
174 public function testEntityTagDeleteNoTagId() {
175 $entityTagParams = array(
176 'contact_id_i' => $this->_individualID,
177 'contact_id_h' => $this->_householdID,
178 'tag_id' => $this->_tagID,
179 );
180 $this->entityTagAdd($entityTagParams);
181
182 $params = array(
183 'contact_id_i' => $this->_individualID,
184 'contact_id_h' => $this->_householdID,
185 );
186
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);
192 }
193
194 public function testEntityTagDeleteINDHH() {
195 $entityTagParams = array(
196 'contact_id_i' => $this->_individualID,
197 'contact_id_h' => $this->_householdID,
198 'tag_id' => $this->_tagID,
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,
206 );
207
208 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
209
210 $this->assertEquals($result['removed'], 2);
211 }
212
213 public function testEntityTagDeleteHH() {
214 $entityTagParams = array(
215 'contact_id_i' => $this->_individualID,
216 'contact_id_h' => $this->_householdID,
217 'tag_id' => $this->_tagID,
218 );
219 $this->entityTagAdd($entityTagParams);
220
221 $params = array(
222 'contact_id_h' => $this->_householdID,
223 'tag_id' => $this->_tagID,
224 );
225
226 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
227 $this->assertEquals($result['removed'], 1);
228 }
229
230 public function testEntityTagDeleteHHORG() {
231 $entityTagParams = array(
232 'contact_id_i' => $this->_individualID,
233 'contact_id_h' => $this->_householdID,
234 'tag_id' => $this->_tagID,
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,
242 );
243
244 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
245 $this->assertEquals($result['removed'], 1);
246 $this->assertEquals($result['not_removed'], 1);
247 }
248
249 public function testEntityTagCommonDeleteINDHH() {
250 $entityTagParams = array(
251 'contact_id_i' => $this->_individualID,
252 'contact_id_h' => $this->_householdID,
253 'tag_id' => $this->_tagID,
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,
261 );
262
263 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
264 $this->assertEquals($result['removed'], 2);
265 }
266
267 public function testEntityTagCommonDeleteHH() {
268 $entityTagParams = array(
269 'contact_id_i' => $this->_individualID,
270 'contact_id_h' => $this->_householdID,
271 'tag_id' => $this->_tagID,
272 );
273 $this->entityTagAdd($entityTagParams);
274
275 $params = array(
276 'contact_id_h' => $this->_householdID,
277 'tag_id' => $this->_tagID,
278 );
279
280 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
281 $this->assertEquals($result['removed'], 1);
282 }
283
284 public function testEntityTagCommonDeleteHHORG() {
285 $entityTagParams = array(
286 'contact_id_i' => $this->_individualID,
287 'contact_id_h' => $this->_householdID,
288 'tag_id' => $this->_tagID,
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,
296 );
297
298 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
299 $this->assertEquals($result['removed'], 1);
300 $this->assertEquals($result['not_removed'], 1);
301 }
302
303 }