Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_entity_tag_* functions | |
14 | * | |
92915c55 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Core | |
6a488035 TO |
17 | */ |
18 | ||
4cbe18b8 | 19 | /** |
24602943 | 20 | * Class api_v3_EntityTagTest. |
acb109b7 | 21 | * @group headless |
4cbe18b8 | 22 | */ |
6a488035 TO |
23 | class api_v3_EntityTagTest extends CiviUnitTestCase { |
24 | ||
24602943 | 25 | /** |
26 | * @var int | |
27 | */ | |
6a488035 TO |
28 | protected $_individualID; |
29 | protected $_householdID; | |
30 | protected $_organizationID; | |
31 | protected $_tagID; | |
fc928539 | 32 | protected $_apiversion = 3; |
6a488035 | 33 | protected $_tag; |
283c85e9 | 34 | protected $_entity = 'entity_tag'; |
b7c9bc4c | 35 | |
24602943 | 36 | /** |
37 | * Basic parameters for create. | |
38 | * | |
39 | * @var array | |
40 | */ | |
9099cab3 | 41 | protected $_params = []; |
6a488035 | 42 | |
24602943 | 43 | /** |
44 | * Set up for test. | |
45 | */ | |
00be9182 | 46 | public function setUp() { |
6a488035 | 47 | parent::setUp(); |
56b010a3 | 48 | $this->useTransaction(TRUE); |
6a488035 | 49 | |
e4d5f1e2 | 50 | $this->_individualID = $this->individualCreate(); |
9099cab3 | 51 | $this->_tag = $this->tagCreate(['name' => 'EntityTagTest']); |
6a488035 | 52 | $this->_tagID = $this->_tag['id']; |
678fbf79 | 53 | $this->_householdID = $this->houseHoldCreate(); |
54 | $this->_organizationID = $this->organizationCreate(); | |
9099cab3 | 55 | $this->_params = [ |
2d932085 | 56 | 'entity_id' => $this->_individualID, |
24602943 | 57 | 'tag_id' => $this->_tagID, |
9099cab3 | 58 | ]; |
6a488035 TO |
59 | } |
60 | ||
24602943 | 61 | /** |
62 | * Test required parameters. | |
63 | * | |
64 | * These failure tests are low value and may not be worth putting in v4. | |
65 | */ | |
66 | public function testFailureTests() { | |
9099cab3 | 67 | $this->callAPIFailure('entity_tag', 'create', ['contact_id' => $this->_individualID], |
fc928539 | 68 | 'tag_id is a required field' |
69 | ); | |
9099cab3 | 70 | $this->callAPIFailure('entity_tag', 'create', ['tag_id' => $this->_tagID], |
24602943 | 71 | 'contact_id is a required field' |
6a488035 | 72 | ); |
6a488035 TO |
73 | } |
74 | ||
24602943 | 75 | /** |
76 | * Test basic create. | |
2d932085 CW |
77 | * @param int $version |
78 | * @dataProvider versionThreeAndFour | |
24602943 | 79 | */ |
2d932085 CW |
80 | public function testContactEntityTagCreate($version) { |
81 | $this->_apiversion = $version; | |
24602943 | 82 | $result = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 TO |
83 | } |
84 | ||
24602943 | 85 | /** |
86 | * Test multiple add functionality. | |
87 | * | |
88 | * This needs review for api v4 as it makes for a very non standard api. | |
89 | */ | |
00be9182 | 90 | public function testAddDouble() { |
6a488035 | 91 | |
24602943 | 92 | $result = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 TO |
93 | $this->assertEquals($result['added'], 1); |
94 | ||
9099cab3 | 95 | $params = [ |
24602943 | 96 | 'contact_id_i' => $this->_individualID, |
97 | 'contact_id_o' => $this->_organizationID, | |
98 | 'tag_id' => $this->_tagID, | |
9099cab3 | 99 | ]; |
6a488035 | 100 | |
fc928539 | 101 | $result = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 TO |
102 | $this->assertEquals($result['added'], 1); |
103 | $this->assertEquals($result['not_added'], 1); | |
104 | } | |
105 | ||
28a04ea9 | 106 | /** |
24602943 | 107 | * Test that get works without an entity. |
28a04ea9 | 108 | */ |
00be9182 | 109 | public function testGetNoEntityID() { |
24602943 | 110 | $this->callAPISuccess('entity_tag', 'create', $this->_params); |
9099cab3 | 111 | $result = $this->callAPISuccess($this->_entity, 'get', ['sequential' => 1, 'tag_id' => $this->_tagID]); |
24602943 | 112 | $this->assertEquals($this->_individualID, $result['values'][0]['entity_id']); |
6a488035 TO |
113 | } |
114 | ||
24602943 | 115 | /** |
116 | * Basic get functionality test. | |
2d932085 CW |
117 | * @param int $version |
118 | * @dataProvider versionThreeAndFour | |
24602943 | 119 | */ |
2d932085 CW |
120 | public function testIndividualEntityTagGet($version) { |
121 | $this->_apiversion = $version; | |
24602943 | 122 | $individualEntity = $this->callAPISuccess('entity_tag', 'create', $this->_params); |
6a488035 | 123 | |
9099cab3 | 124 | $paramsEntity = [ |
24602943 | 125 | 'contact_id' => $this->_individualID, |
9099cab3 | 126 | ]; |
2d932085 CW |
127 | $result = $this->callAPIAndDocument('entity_tag', 'get', $paramsEntity, __FUNCTION__, __FILE__); |
128 | $this->assertEquals(1, $result['count']); | |
129 | $this->assertEquals($this->_tagID, $result['values'][$result['id']]['tag_id']); | |
6a488035 TO |
130 | } |
131 | ||
ffcc1d11 | 132 | /** |
133 | * Test memory usage does not escalate crazily. | |
134 | */ | |
135 | public function testMemoryLeak() { | |
136 | $start = memory_get_usage(); | |
137 | for ($i = 0; $i < 100; $i++) { | |
138 | $this->callAPISuccess('EntityTag', 'get', []); | |
139 | $memUsage = memory_get_usage(); | |
140 | } | |
141 | $max = $start + 2000000; | |
142 | $this->assertTrue($memUsage < $max, "mem usage ( $memUsage ) should be less than $max (start was $start) "); | |
143 | } | |
144 | ||
24602943 | 145 | /** |
146 | * Test tag can be added to a household. | |
147 | */ | |
148 | public function testHouseholdEntityCreate() { | |
9099cab3 | 149 | $params = [ |
24602943 | 150 | 'contact_id' => $this->_householdID, |
151 | 'tag_id' => $this->_tagID, | |
9099cab3 | 152 | ]; |
6a488035 | 153 | |
fc928539 | 154 | $householdEntity = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 | 155 | $this->assertEquals($householdEntity['added'], 1); |
6a488035 TO |
156 | } |
157 | ||
24602943 | 158 | /** |
159 | * Test tag can be added to an organization. | |
2d932085 CW |
160 | * @param int $version |
161 | * @dataProvider versionThreeAndFour | |
24602943 | 162 | */ |
2d932085 CW |
163 | public function testOrganizationEntityGet($version) { |
164 | $this->_apiversion = $version; | |
24602943 | 165 | |
9099cab3 | 166 | $params = [ |
2d932085 | 167 | 'entity_id' => $this->_organizationID, |
24602943 | 168 | 'tag_id' => $this->_tagID, |
9099cab3 | 169 | ]; |
6a488035 | 170 | |
2d932085 | 171 | $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 | 172 | |
9099cab3 | 173 | $tag = $this->callAPISuccess('entity_tag', 'getsingle', ['contact_id' => $this->_organizationID]); |
2d932085 CW |
174 | $this->assertEquals($this->_organizationID, $tag['entity_id']); |
175 | $this->assertEquals($this->_tagID, $tag['tag_id']); | |
6a488035 TO |
176 | } |
177 | ||
28a04ea9 | 178 | /** |
24602943 | 179 | * Civicrm_entity_tag_Delete methods. |
28a04ea9 | 180 | */ |
00be9182 | 181 | public function testEntityTagDeleteNoTagId() { |
9099cab3 | 182 | $entityTagParams = [ |
6a488035 TO |
183 | 'contact_id_i' => $this->_individualID, |
184 | 'contact_id_h' => $this->_householdID, | |
185 | 'tag_id' => $this->_tagID, | |
9099cab3 | 186 | ]; |
6a488035 TO |
187 | $this->entityTagAdd($entityTagParams); |
188 | ||
9099cab3 | 189 | $params = [ |
6a488035 TO |
190 | 'contact_id_i' => $this->_individualID, |
191 | 'contact_id_h' => $this->_householdID, | |
9099cab3 | 192 | ]; |
6a488035 | 193 | |
c3a18b64 WDC |
194 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
195 | ||
196 | $this->assertEquals($result['not_removed'], 0); | |
197 | $this->assertEquals($result['removed'], 2); | |
198 | $this->assertEquals($result['total_count'], 2); | |
6a488035 TO |
199 | } |
200 | ||
00be9182 | 201 | public function testEntityTagDeleteINDHH() { |
9099cab3 | 202 | $entityTagParams = [ |
6a488035 TO |
203 | 'contact_id_i' => $this->_individualID, |
204 | 'contact_id_h' => $this->_householdID, | |
205 | 'tag_id' => $this->_tagID, | |
9099cab3 | 206 | ]; |
6a488035 TO |
207 | $this->entityTagAdd($entityTagParams); |
208 | ||
9099cab3 | 209 | $params = [ |
6a488035 TO |
210 | 'contact_id_i' => $this->_individualID, |
211 | 'contact_id_h' => $this->_householdID, | |
212 | 'tag_id' => $this->_tagID, | |
9099cab3 | 213 | ]; |
6a488035 | 214 | |
fc928539 | 215 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 | 216 | |
6a488035 TO |
217 | $this->assertEquals($result['removed'], 2); |
218 | } | |
219 | ||
00be9182 | 220 | public function testEntityTagDeleteHH() { |
9099cab3 | 221 | $entityTagParams = [ |
6a488035 TO |
222 | 'contact_id_i' => $this->_individualID, |
223 | 'contact_id_h' => $this->_householdID, | |
224 | 'tag_id' => $this->_tagID, | |
9099cab3 | 225 | ]; |
6a488035 TO |
226 | $this->entityTagAdd($entityTagParams); |
227 | ||
9099cab3 | 228 | $params = [ |
6a488035 TO |
229 | 'contact_id_h' => $this->_householdID, |
230 | 'tag_id' => $this->_tagID, | |
9099cab3 | 231 | ]; |
6a488035 | 232 | |
fc928539 | 233 | $result = $this->callAPIAndDocument('entity_tag', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
234 | $this->assertEquals($result['removed'], 1); |
235 | } | |
236 | ||
00be9182 | 237 | public function testEntityTagDeleteHHORG() { |
9099cab3 | 238 | $entityTagParams = [ |
6a488035 TO |
239 | 'contact_id_i' => $this->_individualID, |
240 | 'contact_id_h' => $this->_householdID, | |
241 | 'tag_id' => $this->_tagID, | |
9099cab3 | 242 | ]; |
6a488035 TO |
243 | $this->entityTagAdd($entityTagParams); |
244 | ||
9099cab3 | 245 | $params = [ |
6a488035 TO |
246 | 'contact_id_h' => $this->_householdID, |
247 | 'contact_id_o' => $this->_organizationID, | |
248 | 'tag_id' => $this->_tagID, | |
9099cab3 | 249 | ]; |
6a488035 | 250 | |
fc928539 | 251 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
252 | $this->assertEquals($result['removed'], 1); |
253 | $this->assertEquals($result['not_removed'], 1); | |
254 | } | |
255 | ||
00be9182 | 256 | public function testEntityTagCommonDeleteINDHH() { |
9099cab3 | 257 | $entityTagParams = [ |
6a488035 TO |
258 | 'contact_id_i' => $this->_individualID, |
259 | 'contact_id_h' => $this->_householdID, | |
260 | 'tag_id' => $this->_tagID, | |
9099cab3 | 261 | ]; |
6a488035 TO |
262 | $this->entityTagAdd($entityTagParams); |
263 | ||
9099cab3 | 264 | $params = [ |
6a488035 TO |
265 | 'contact_id_i' => $this->_individualID, |
266 | 'contact_id_h' => $this->_householdID, | |
267 | 'tag_id' => $this->_tagID, | |
9099cab3 | 268 | ]; |
6a488035 | 269 | |
fc928539 | 270 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
271 | $this->assertEquals($result['removed'], 2); |
272 | } | |
273 | ||
00be9182 | 274 | public function testEntityTagCommonDeleteHH() { |
9099cab3 | 275 | $entityTagParams = [ |
6a488035 TO |
276 | 'contact_id_i' => $this->_individualID, |
277 | 'contact_id_h' => $this->_householdID, | |
278 | 'tag_id' => $this->_tagID, | |
9099cab3 | 279 | ]; |
6a488035 TO |
280 | $this->entityTagAdd($entityTagParams); |
281 | ||
9099cab3 | 282 | $params = [ |
6a488035 TO |
283 | 'contact_id_h' => $this->_householdID, |
284 | 'tag_id' => $this->_tagID, | |
9099cab3 | 285 | ]; |
6a488035 | 286 | |
fc928539 | 287 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
288 | $this->assertEquals($result['removed'], 1); |
289 | } | |
290 | ||
00be9182 | 291 | public function testEntityTagCommonDeleteHHORG() { |
9099cab3 | 292 | $entityTagParams = [ |
6a488035 TO |
293 | 'contact_id_i' => $this->_individualID, |
294 | 'contact_id_h' => $this->_householdID, | |
295 | 'tag_id' => $this->_tagID, | |
9099cab3 | 296 | ]; |
6a488035 TO |
297 | $this->entityTagAdd($entityTagParams); |
298 | ||
9099cab3 | 299 | $params = [ |
6a488035 TO |
300 | 'contact_id_h' => $this->_householdID, |
301 | 'contact_id_o' => $this->_organizationID, | |
302 | 'tag_id' => $this->_tagID, | |
9099cab3 | 303 | ]; |
6a488035 | 304 | |
fc928539 | 305 | $result = $this->callAPISuccess('entity_tag', 'delete', $params); |
6a488035 TO |
306 | $this->assertEquals($result['removed'], 1); |
307 | $this->assertEquals($result['not_removed'], 1); | |
308 | } | |
96025800 | 309 | |
466fce54 | 310 | public function testEntityTagJoin() { |
9099cab3 | 311 | $org = $this->callAPISuccess('Contact', 'create', [ |
466fce54 CW |
312 | 'contact_type' => 'Organization', |
313 | 'organization_name' => 'Org123', | |
9099cab3 | 314 | 'api.EntityTag.create' => [ |
466fce54 | 315 | 'tag_id' => $this->_tagID, |
9099cab3 CW |
316 | ], |
317 | ]); | |
466fce54 | 318 | // Fetch contact info via join |
9099cab3 CW |
319 | $result = $this->callAPISuccessGetSingle('EntityTag', [ |
320 | 'return' => ["entity_id.organization_name", "tag_id.name"], | |
466fce54 CW |
321 | 'entity_id' => $org['id'], |
322 | 'entity_table' => "civicrm_contact", | |
9099cab3 | 323 | ]); |
466fce54 CW |
324 | $this->assertEquals('Org123', $result['entity_id.organization_name']); |
325 | $this->assertEquals('EntityTagTest', $result['tag_id.name']); | |
326 | // This should return no results by restricting contact_type | |
9099cab3 CW |
327 | $result = $this->callAPISuccess('EntityTag', 'get', [ |
328 | 'return' => ["entity_id.organization_name"], | |
466fce54 CW |
329 | 'entity_id' => $org['id'], |
330 | 'entity_table' => "civicrm_contact", | |
331 | 'entity_id.contact_type' => "Individual", | |
9099cab3 | 332 | ]); |
466fce54 CW |
333 | $this->assertEquals(0, $result['count']); |
334 | } | |
335 | ||
6a488035 | 336 | } |