Merge pull request #7661 from agileware/crm-17848
[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-2015 |
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 */
38 class api_v3_EntityTagTest extends CiviUnitTestCase {
39
40 /**
41 * @var int
42 */
43 protected $_individualID;
44 protected $_householdID;
45 protected $_organizationID;
46 protected $_tagID;
47 protected $_apiversion = 3;
48 protected $_tag;
49 protected $_entity = 'entity_tag';
50
51 /**
52 * Basic parameters for create.
53 *
54 * @var array
55 */
56 protected $_params = array();
57
58 /**
59 * Set up for test.
60 */
61 public function setUp() {
62 parent::setUp();
63 $this->useTransaction(TRUE);
64
65 $this->_individualID = $this->individualCreate();
66 $this->_tag = $this->tagCreate();
67 $this->_tagID = $this->_tag['id'];
68 $this->_householdID = $this->houseHoldCreate();
69 $this->_organizationID = $this->organizationCreate();
70 $this->_params = array(
71 'contact_id' => $this->_individualID,
72 'tag_id' => $this->_tagID,
73 );
74 }
75
76 /**
77 * Test required parameters.
78 *
79 * These failure tests are low value and may not be worth putting in v4.
80 */
81 public function testFailureTests() {
82 $this->callAPIFailure('entity_tag', 'create', array('contact_id' => $this->_individualID),
83 'tag_id is a required field'
84 );
85 $this->callAPIFailure('entity_tag', 'create', array('tag_id' => $this->_tagID),
86 'contact_id is a required field'
87 );
88 }
89
90 /**
91 * Test basic create.
92 */
93 public function testContactEntityTagCreate() {
94 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
95 $this->assertEquals($result['added'], 1);
96 }
97
98 /**
99 * Test multiple add functionality.
100 *
101 * This needs review for api v4 as it makes for a very non standard api.
102 */
103 public function testAddDouble() {
104
105 $result = $this->callAPISuccess('entity_tag', 'create', $this->_params);
106 $this->assertEquals($result['added'], 1);
107
108 $params = array(
109 'contact_id_i' => $this->_individualID,
110 'contact_id_o' => $this->_organizationID,
111 'tag_id' => $this->_tagID,
112 );
113
114 $result = $this->callAPISuccess('entity_tag', 'create', $params);
115 $this->assertEquals($result['added'], 1);
116 $this->assertEquals($result['not_added'], 1);
117 }
118
119 /**
120 * Test that get works without an entity.
121 */
122 public function testGetNoEntityID() {
123 $this->callAPISuccess('entity_tag', 'create', $this->_params);
124 $result = $this->callAPISuccess($this->_entity, 'get', array('sequential' => 1, 'tag_id' => $this->_tagID));
125 $this->assertEquals($this->_individualID, $result['values'][0]['entity_id']);
126 }
127
128 /**
129 * Basic get functionality test.
130 */
131 public function testIndividualEntityTagGet() {
132 $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params);
133 $this->assertEquals($individualEntity['added'], 1);
134
135 $paramsEntity = array(
136 'contact_id' => $this->_individualID,
137 );
138 $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__);
139 }
140
141 /**
142 * Test tag can be added to a household.
143 */
144 public function testHouseholdEntityCreate() {
145 $params = array(
146 'contact_id' => $this->_householdID,
147 'tag_id' => $this->_tagID,
148 );
149
150 $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params);
151 $this->assertEquals($householdEntity['added'], 1);
152 }
153
154 /**
155 * Test tag can be added to an organization.
156 */
157 public function testOrganizationEntityGet() {
158
159 $params = array(
160 'contact_id' => $this->_organizationID,
161 'tag_id' => $this->_tagID,
162 );
163
164 $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params);
165 $this->assertEquals($organizationEntity['added'], 1);
166
167 $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID));
168 }
169
170 /**
171 * Civicrm_entity_tag_Delete methods.
172 */
173 public function testEntityTagDeleteNoTagId() {
174 $entityTagParams = array(
175 'contact_id_i' => $this->_individualID,
176 'contact_id_h' => $this->_householdID,
177 'tag_id' => $this->_tagID,
178 );
179 $this->entityTagAdd($entityTagParams);
180
181 $params = array(
182 'contact_id_i' => $this->_individualID,
183 'contact_id_h' => $this->_householdID,
184 );
185
186 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
187
188 $this->assertEquals($result['not_removed'], 0);
189 $this->assertEquals($result['removed'], 2);
190 $this->assertEquals($result['total_count'], 2);
191 }
192
193 public function testEntityTagDeleteINDHH() {
194 $entityTagParams = array(
195 'contact_id_i' => $this->_individualID,
196 'contact_id_h' => $this->_householdID,
197 'tag_id' => $this->_tagID,
198 );
199 $this->entityTagAdd($entityTagParams);
200
201 $params = array(
202 'contact_id_i' => $this->_individualID,
203 'contact_id_h' => $this->_householdID,
204 'tag_id' => $this->_tagID,
205 );
206
207 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
208
209 $this->assertEquals($result['removed'], 2);
210 }
211
212 public function testEntityTagDeleteHH() {
213 $entityTagParams = array(
214 'contact_id_i' => $this->_individualID,
215 'contact_id_h' => $this->_householdID,
216 'tag_id' => $this->_tagID,
217 );
218 $this->entityTagAdd($entityTagParams);
219
220 $params = array(
221 'contact_id_h' => $this->_householdID,
222 'tag_id' => $this->_tagID,
223 );
224
225 $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__);
226 $this->assertEquals($result['removed'], 1);
227 }
228
229 public function testEntityTagDeleteHHORG() {
230 $entityTagParams = array(
231 'contact_id_i' => $this->_individualID,
232 'contact_id_h' => $this->_householdID,
233 'tag_id' => $this->_tagID,
234 );
235 $this->entityTagAdd($entityTagParams);
236
237 $params = array(
238 'contact_id_h' => $this->_householdID,
239 'contact_id_o' => $this->_organizationID,
240 'tag_id' => $this->_tagID,
241 );
242
243 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
244 $this->assertEquals($result['removed'], 1);
245 $this->assertEquals($result['not_removed'], 1);
246 }
247
248 public function testEntityTagCommonDeleteINDHH() {
249 $entityTagParams = array(
250 'contact_id_i' => $this->_individualID,
251 'contact_id_h' => $this->_householdID,
252 'tag_id' => $this->_tagID,
253 );
254 $this->entityTagAdd($entityTagParams);
255
256 $params = array(
257 'contact_id_i' => $this->_individualID,
258 'contact_id_h' => $this->_householdID,
259 'tag_id' => $this->_tagID,
260 );
261
262 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
263 $this->assertEquals($result['removed'], 2);
264 }
265
266 public function testEntityTagCommonDeleteHH() {
267 $entityTagParams = array(
268 'contact_id_i' => $this->_individualID,
269 'contact_id_h' => $this->_householdID,
270 'tag_id' => $this->_tagID,
271 );
272 $this->entityTagAdd($entityTagParams);
273
274 $params = array(
275 'contact_id_h' => $this->_householdID,
276 'tag_id' => $this->_tagID,
277 );
278
279 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
280 $this->assertEquals($result['removed'], 1);
281 }
282
283 public function testEntityTagCommonDeleteHHORG() {
284 $entityTagParams = array(
285 'contact_id_i' => $this->_individualID,
286 'contact_id_h' => $this->_householdID,
287 'tag_id' => $this->_tagID,
288 );
289 $this->entityTagAdd($entityTagParams);
290
291 $params = array(
292 'contact_id_h' => $this->_householdID,
293 'contact_id_o' => $this->_organizationID,
294 'tag_id' => $this->_tagID,
295 );
296
297 $result = $this->callAPISuccess('entity_tag', 'delete', $params);
298 $this->assertEquals($result['removed'], 1);
299 $this->assertEquals($result['not_removed'], 1);
300 }
301
302 }