Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 |
39 | class 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(); |
466fce54 | 67 | $this->_tag = $this->tagCreate(array('name' => 'EntityTagTest')); |
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 | ||
ffcc1d11 | 142 | /** |
143 | * Test memory usage does not escalate crazily. | |
144 | */ | |
145 | public function testMemoryLeak() { | |
146 | $start = memory_get_usage(); | |
147 | for ($i = 0; $i < 100; $i++) { | |
148 | $this->callAPISuccess('EntityTag', 'get', []); | |
149 | $memUsage = memory_get_usage(); | |
150 | } | |
151 | $max = $start + 2000000; | |
152 | $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) "); | |
153 | } | |
154 | ||
24602943 | 155 | /** |
156 | * Test tag can be added to a household. | |
157 | */ | |
158 | public function testHouseholdEntityCreate() { | |
92915c55 | 159 | $params = array( |
24602943 | 160 | 'contact_id' => $this->_householdID, |
161 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
162 | ); |
163 | ||
fc928539 | 164 | $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 | 165 | $this->assertEquals($householdEntity['added'], 1); |
6a488035 TO |
166 | } |
167 | ||
24602943 | 168 | /** |
169 | * Test tag can be added to an organization. | |
170 | */ | |
00be9182 | 171 | public function testOrganizationEntityGet() { |
24602943 | 172 | |
92915c55 | 173 | $params = array( |
24602943 | 174 | 'contact_id' => $this->_organizationID, |
175 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
176 | ); |
177 | ||
fc928539 | 178 | $organizationEntity = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 TO |
179 | $this->assertEquals($organizationEntity['added'], 1); |
180 | ||
24602943 | 181 | $this->callAPISuccess('entity_tag', 'getsingle', array('contact_id' => $this->_organizationID)); |
6a488035 TO |
182 | } |
183 | ||
28a04ea9 | 184 | /** |
24602943 | 185 | * Civicrm_entity_tag_Delete methods. |
28a04ea9 | 186 | */ |
00be9182 | 187 | public function testEntityTagDeleteNoTagId() { |
6a488035 TO |
188 | $entityTagParams = array( |
189 | 'contact_id_i' => $this->_individualID, | |
190 | 'contact_id_h' => $this->_householdID, | |
191 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
192 | ); |
193 | $this->entityTagAdd($entityTagParams); | |
194 | ||
195 | $params = array( | |
196 | 'contact_id_i' => $this->_individualID, | |
197 | 'contact_id_h' => $this->_householdID, | |
6a488035 TO |
198 | ); |
199 | ||
c3a18b64 WDC |
200 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
201 | ||
202 | $this->assertEquals($result['not_removed'], 0); | |
203 | $this->assertEquals($result['removed'], 2); | |
204 | $this->assertEquals($result['total_count'], 2); | |
6a488035 TO |
205 | } |
206 | ||
00be9182 | 207 | public function testEntityTagDeleteINDHH() { |
6a488035 TO |
208 | $entityTagParams = array( |
209 | 'contact_id_i' => $this->_individualID, | |
210 | 'contact_id_h' => $this->_householdID, | |
211 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
212 | ); |
213 | $this->entityTagAdd($entityTagParams); | |
214 | ||
215 | $params = array( | |
216 | 'contact_id_i' => $this->_individualID, | |
217 | 'contact_id_h' => $this->_householdID, | |
218 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
219 | ); |
220 | ||
fc928539 | 221 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 | 222 | |
6a488035 TO |
223 | $this->assertEquals($result['removed'], 2); |
224 | } | |
225 | ||
00be9182 | 226 | public function testEntityTagDeleteHH() { |
6a488035 TO |
227 | $entityTagParams = array( |
228 | 'contact_id_i' => $this->_individualID, | |
229 | 'contact_id_h' => $this->_householdID, | |
230 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
231 | ); |
232 | $this->entityTagAdd($entityTagParams); | |
233 | ||
234 | $params = array( | |
235 | 'contact_id_h' => $this->_householdID, | |
236 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
237 | ); |
238 | ||
fc928539 | 239 | $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
240 | $this->assertEquals($result['removed'], 1); |
241 | } | |
242 | ||
00be9182 | 243 | public function testEntityTagDeleteHHORG() { |
6a488035 TO |
244 | $entityTagParams = array( |
245 | 'contact_id_i' => $this->_individualID, | |
246 | 'contact_id_h' => $this->_householdID, | |
247 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
248 | ); |
249 | $this->entityTagAdd($entityTagParams); | |
250 | ||
251 | $params = array( | |
252 | 'contact_id_h' => $this->_householdID, | |
253 | 'contact_id_o' => $this->_organizationID, | |
254 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
255 | ); |
256 | ||
fc928539 | 257 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
258 | $this->assertEquals($result['removed'], 1); |
259 | $this->assertEquals($result['not_removed'], 1); | |
260 | } | |
261 | ||
00be9182 | 262 | public function testEntityTagCommonDeleteINDHH() { |
6a488035 TO |
263 | $entityTagParams = array( |
264 | 'contact_id_i' => $this->_individualID, | |
265 | 'contact_id_h' => $this->_householdID, | |
266 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
267 | ); |
268 | $this->entityTagAdd($entityTagParams); | |
269 | ||
270 | $params = array( | |
271 | 'contact_id_i' => $this->_individualID, | |
272 | 'contact_id_h' => $this->_householdID, | |
273 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
274 | ); |
275 | ||
fc928539 | 276 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
277 | $this->assertEquals($result['removed'], 2); |
278 | } | |
279 | ||
00be9182 | 280 | public function testEntityTagCommonDeleteHH() { |
6a488035 TO |
281 | $entityTagParams = array( |
282 | 'contact_id_i' => $this->_individualID, | |
283 | 'contact_id_h' => $this->_householdID, | |
284 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
285 | ); |
286 | $this->entityTagAdd($entityTagParams); | |
287 | ||
288 | $params = array( | |
289 | 'contact_id_h' => $this->_householdID, | |
290 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
291 | ); |
292 | ||
fc928539 | 293 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
294 | $this->assertEquals($result['removed'], 1); |
295 | } | |
296 | ||
00be9182 | 297 | public function testEntityTagCommonDeleteHHORG() { |
6a488035 TO |
298 | $entityTagParams = array( |
299 | 'contact_id_i' => $this->_individualID, | |
300 | 'contact_id_h' => $this->_householdID, | |
301 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
302 | ); |
303 | $this->entityTagAdd($entityTagParams); | |
304 | ||
305 | $params = array( | |
306 | 'contact_id_h' => $this->_householdID, | |
307 | 'contact_id_o' => $this->_organizationID, | |
308 | 'tag_id' => $this->_tagID, | |
6a488035 TO |
309 | ); |
310 | ||
fc928539 | 311 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
312 | $this->assertEquals($result['removed'], 1); |
313 | $this->assertEquals($result['not_removed'], 1); | |
314 | } | |
96025800 | 315 | |
466fce54 CW |
316 | public function testEntityTagJoin() { |
317 | $org = $this->callAPISuccess('Contact', 'create', array( | |
318 | 'contact_type' => 'Organization', | |
319 | 'organization_name' => 'Org123', | |
320 | 'api.EntityTag.create' => array( | |
321 | 'tag_id' => $this->_tagID, | |
322 | ), | |
323 | )); | |
324 | // Fetch contact info via join | |
325 | $result = $this->callAPISuccessGetSingle('EntityTag', array( | |
326 | 'return' => array("entity_id.organization_name", "tag_id.name"), | |
327 | 'entity_id' => $org['id'], | |
328 | 'entity_table' => "civicrm_contact", | |
329 | )); | |
330 | $this->assertEquals('Org123', $result['entity_id.organization_name']); | |
331 | $this->assertEquals('EntityTagTest', $result['tag_id.name']); | |
332 | // This should return no results by restricting contact_type | |
333 | $result = $this->callAPISuccess('EntityTag', 'get', array( | |
334 | 'return' => array("entity_id.organization_name"), | |
335 | 'entity_id' => $org['id'], | |
336 | 'entity_table' => "civicrm_contact", | |
337 | 'entity_id.contact_type' => "Individual", | |
338 | )); | |
339 | $this->assertEquals(0, $result['count']); | |
340 | } | |
341 | ||
6a488035 | 342 | } |