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