Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
7fbb4198 | 2 | /** |
6a488035 TO |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * Class contains api test cases for "civicrm_relationship" | |
32 | * | |
33 | */ | |
34 | class api_v3_RelationshipTest extends CiviUnitTestCase { | |
b2402735 | 35 | protected $_apiversion = 3; |
6a488035 TO |
36 | protected $_cId_a; |
37 | protected $_cId_b; | |
38 | protected $_cId_b2;// second org | |
39 | protected $_relTypeID; | |
40 | protected $_ids = array(); | |
41 | protected $_customGroupId = NULL; | |
42 | protected $_customFieldId = NULL; | |
43 | protected $_params; | |
26dcc9eb | 44 | public $_eNoticeCompliant = TRUE; |
6a488035 TO |
45 | protected $_entity; |
46 | function get_info() { | |
47 | return array( | |
48 | 'name' => 'Relationship Create', | |
49 | 'description' => 'Test all Relationship Create API methods.', | |
50 | 'group' => 'CiviCRM API Tests', | |
51 | ); | |
52 | } | |
53 | ||
54 | function setUp() { | |
55 | parent::setUp(); | |
e4d5f1e2 | 56 | $this->_cId_a = $this->individualCreate(); |
6a488035 TO |
57 | $this->_cId_b = $this->organizationCreate(); |
58 | $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2')); | |
59 | $this->_entity = 'relationship'; | |
60 | //Create a relationship type | |
61 | $relTypeParams = array( | |
62 | 'name_a_b' => 'Relation 1 for delete', | |
63 | 'name_b_a' => 'Relation 2 for delete', | |
64 | 'description' => 'Testing relationship type', | |
65 | 'contact_type_a' => 'Individual', | |
66 | 'contact_type_b' => 'Organization', | |
67 | 'is_reserved' => 1, | |
68 | 'is_active' => 1, | |
6a488035 | 69 | ); |
b2402735 | 70 | |
6a488035 TO |
71 | $this->_relTypeID = $this->relationshipTypeCreate($relTypeParams); |
72 | $this->_params = array( | |
73 | 'contact_id_a' => $this->_cId_a, | |
74 | 'contact_id_b' => $this->_cId_b, | |
75 | 'relationship_type_id' => $this->_relTypeID, | |
76 | 'start_date' => '2008-12-20', | |
77 | 'is_active' => 1, | |
6a488035 TO |
78 | ); |
79 | ||
80 | } | |
81 | ||
82 | function tearDown() { | |
6a488035 TO |
83 | $this->contactDelete($this->_cId_a); |
84 | $this->contactDelete($this->_cId_b); | |
75638074 | 85 | $this->contactDelete($this->_cId_b2); |
86 | $this->quickCleanup(array('civicrm_relationship'), TRUE); | |
87 | $this->relationshipTypeDelete($this->_relTypeID); | |
6a488035 TO |
88 | } |
89 | ||
90 | ///////////////// civicrm_relationship_create methods | |
91 | ||
92 | /** | |
93 | * check with empty array | |
94 | */ | |
95 | function testRelationshipCreateEmpty() { | |
b2402735 | 96 | $this->callAPIFailure('relationship', 'create', array()); |
6a488035 TO |
97 | } |
98 | ||
99 | /** | |
100 | * check if required fields are not passed | |
101 | */ | |
102 | function testRelationshipCreateWithoutRequired() { | |
103 | $params = array( | |
104 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
105 | 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'), | |
106 | 'is_active' => 1, | |
107 | ); | |
108 | ||
d0e1eff2 | 109 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
110 | } |
111 | ||
112 | /** | |
113 | * check with incorrect required fields | |
114 | */ | |
115 | function testRelationshipCreateWithIncorrectData() { | |
116 | ||
117 | $params = array( | |
118 | 'contact_id_a' => $this->_cId_a, | |
119 | 'contact_id_b' => $this->_cId_b, | |
120 | 'relationship_type_id' => 'Breaking Relationship', | |
6a488035 TO |
121 | ); |
122 | ||
d0e1eff2 | 123 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
124 | |
125 | //contact id is not an integer | |
126 | $params = array( | |
127 | 'contact_id_a' => 'invalid', | |
128 | 'contact_id_b' => $this->_cId_b, | |
129 | 'relationship_type_id' => $this->_relTypeID, | |
130 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
131 | 'is_active' => 1, | |
132 | ); | |
d0e1eff2 | 133 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
134 | |
135 | //contact id does not exists | |
136 | $params['contact_id_a'] = 999; | |
d0e1eff2 | 137 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
138 | |
139 | //invalid date | |
140 | $params['contact_id_a'] = $this->_cId_a; | |
141 | $params['start_date'] = array('d' => '1', 'M' => '1'); | |
d0e1eff2 | 142 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
143 | } |
144 | ||
145 | /** | |
146 | * check relationship creation with invalid Relationship | |
147 | */ | |
148 | function testRelationshipCreatInvalidRelationship() { | |
149 | // both the contact of type Individual | |
150 | $params = array( | |
151 | 'contact_id_a' => $this->_cId_a, | |
152 | 'contact_id_b' => $this->_cId_a, | |
153 | 'relationship_type_id' => $this->_relTypeID, | |
154 | 'start_date' => '2008-01-10', | |
155 | 'is_active' => 1, | |
6a488035 TO |
156 | ); |
157 | ||
d0e1eff2 | 158 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
159 | |
160 | // both the contact of type Organization | |
161 | $params = array( | |
162 | 'contact_id_a' => $this->_cId_b, | |
163 | 'contact_id_b' => $this->_cId_b, | |
164 | 'relationship_type_id' => $this->_relTypeID, | |
165 | 'start_date' => '2008-01-10', | |
166 | 'is_active' => 1, | |
6a488035 TO |
167 | ); |
168 | ||
d0e1eff2 | 169 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
170 | } |
171 | ||
172 | /** | |
173 | * check relationship already exists | |
174 | */ | |
175 | function testRelationshipCreateAlreadyExists() { | |
176 | $params = array( | |
177 | 'contact_id_a' => $this->_cId_a, | |
178 | 'contact_id_b' => $this->_cId_b, | |
179 | 'relationship_type_id' => $this->_relTypeID, | |
180 | 'start_date' => '2008-12-20', 'end_date' => NULL, | |
181 | 'is_active' => 1, | |
6a488035 | 182 | ); |
b2402735 | 183 | $relationship = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
184 | |
185 | $params = array( | |
186 | 'contact_id_a' => $this->_cId_a, | |
187 | 'contact_id_b' => $this->_cId_b, | |
188 | 'relationship_type_id' => $this->_relTypeID, | |
189 | 'start_date' => '2008-12-20', | |
190 | 'is_active' => 1, | |
6a488035 | 191 | ); |
b2402735 | 192 | $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists'); |
6a488035 TO |
193 | |
194 | $params['id'] = $relationship['id']; | |
b2402735 | 195 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
196 | } |
197 | ||
198 | /** | |
199 | * check relationship already exists | |
200 | */ | |
201 | function testRelationshipCreateUpdateAlreadyExists() { | |
202 | $params = array( | |
203 | 'contact_id_a' => $this->_cId_a, | |
204 | 'contact_id_b' => $this->_cId_b, | |
205 | 'relationship_type_id' => $this->_relTypeID, | |
206 | 'start_date' => '2008-12-20', | |
207 | 'end_date' => NULL, | |
208 | 'is_active' => 1, | |
6a488035 | 209 | |
26dcc9eb | 210 | ); |
211 | $relationship = $this->callAPISuccess('relationship', 'create', $params); | |
6a488035 TO |
212 | $params = array( |
213 | 'id' => $relationship['id'], | |
214 | 'is_active' => 0, | |
6a488035 TO |
215 | 'debug' => 1, |
216 | ); | |
26dcc9eb | 217 | $result = $this->callAPISuccess('relationship', 'create', $params); |
26dcc9eb | 218 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 | 219 | $params['id'] = $relationship['id']; |
26dcc9eb | 220 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
221 | } |
222 | ||
223 | /** | |
224 | * checkupdate doesn't reset stuff badly - CRM-11789 | |
225 | */ | |
226 | function testRelationshipCreateUpdateDoesntMangle() { | |
227 | $params = array( | |
228 | 'contact_id_a' => $this->_cId_a, | |
229 | 'contact_id_b' => $this->_cId_b, | |
230 | 'relationship_type_id' => $this->_relTypeID, | |
231 | 'start_date' => '2008-12-20', | |
6a488035 TO |
232 | 'is_active' => 1, |
233 | 'is_permission_a_b' => 1, | |
234 | 'description' => 'my desc', | |
6a488035 | 235 | ); |
26dcc9eb | 236 | $relationship = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
237 | |
238 | $updateparams = array( | |
239 | 'id' => $relationship['id'], | |
6a488035 TO |
240 | 'relationship_type_id' => $this->_relTypeID, |
241 | ); | |
26dcc9eb | 242 | $result = $this->callAPISuccess('relationship', 'create', $updateparams); |
6a488035 | 243 | |
6a488035 TO |
244 | //make sure the orig params didn't get changed |
245 | $this->getAndCheck($params, $relationship['id'], 'relationship'); | |
246 | ||
247 | } | |
248 | ||
249 | ||
250 | ||
251 | /** | |
252 | * check relationship creation | |
253 | */ | |
254 | function testRelationshipCreate() { | |
255 | $params = array( | |
256 | 'contact_id_a' => $this->_cId_a, | |
257 | 'contact_id_b' => $this->_cId_b, | |
258 | 'relationship_type_id' => $this->_relTypeID, | |
259 | 'start_date' => '2010-10-30', | |
260 | 'end_date' => '2010-12-30', | |
261 | 'is_active' => 1, | |
262 | 'note' => 'note', | |
6a488035 TO |
263 | ); |
264 | ||
26dcc9eb | 265 | $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__); |
75638074 | 266 | $this->assertNotNull($result['id']); |
6a488035 TO |
267 | $relationParams = array( |
268 | 'id' => $result['id'], | |
269 | ); | |
270 | ||
271 | // assertDBState compares expected values in $result to actual values in the DB | |
272 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
b2402735 | 273 | $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id'])); |
6a488035 TO |
274 | $values = $result['values'][$result['id']]; |
275 | foreach ($params as $key => $value) { | |
b2402735 | 276 | if ($key == 'note') { |
6a488035 TO |
277 | continue; |
278 | } | |
75638074 | 279 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE)); |
6a488035 TO |
280 | } |
281 | $params['id'] = $result['id']; | |
26dcc9eb | 282 | $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
283 | } |
284 | ||
285 | /** | |
286 | * check relationship creation | |
287 | */ | |
288 | function testRelationshipCreateEmptyEndDate() { | |
289 | $params = array( | |
290 | 'contact_id_a' => $this->_cId_a, | |
291 | 'contact_id_b' => $this->_cId_b, | |
292 | 'relationship_type_id' => $this->_relTypeID, | |
293 | 'start_date' => '2010-10-30', | |
294 | 'end_date' => '', | |
295 | 'is_active' => 1, | |
296 | 'note' => 'note', | |
6a488035 TO |
297 | ); |
298 | ||
26dcc9eb | 299 | $result = $this->callAPISuccess('relationship', 'create', $params); |
75638074 | 300 | $this->assertNotNull($result['id']); |
6a488035 TO |
301 | $relationParams = array( |
302 | 'id' => $result['id'], | |
303 | ); | |
304 | ||
305 | // assertDBState compares expected values in $result to actual values in the DB | |
306 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
26dcc9eb | 307 | $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id'])); |
6a488035 TO |
308 | $values = $result['values'][$result['id']]; |
309 | foreach ($params as $key => $value) { | |
b2402735 | 310 | if ($key == 'note') { |
6a488035 TO |
311 | continue; |
312 | } | |
313 | if($key == 'end_date'){ | |
314 | $this->assertTrue(empty($values[$key])); | |
315 | continue; | |
316 | } | |
317 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
318 | } | |
319 | $params['id'] = $result['id']; | |
26dcc9eb | 320 | $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
321 | } |
322 | ||
323 | /** | |
324 | * check relationship creation with custom data | |
325 | */ | |
326 | function testRelationshipCreateWithCustomData() { | |
327 | $customGroup = $this->createCustomGroup(); | |
328 | $this->_ids = $this->createCustomField(); | |
329 | //few custom Values for comparing | |
330 | $custom_params = array( | |
331 | "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship', | |
332 | "custom_{$this->_ids[1]}" => 'Y', | |
333 | "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00', | |
334 | "custom_{$this->_ids[3]}" => 'http://example.com', | |
335 | ); | |
336 | ||
337 | $params = array( | |
338 | 'contact_id_a' => $this->_cId_a, | |
339 | 'contact_id_b' => $this->_cId_b, | |
340 | 'relationship_type_id' => $this->_relTypeID, | |
341 | 'start_date' => '2008-12-20', | |
342 | 'is_active' => 1, | |
6a488035 TO |
343 | ); |
344 | $params = array_merge($params, $custom_params); | |
26dcc9eb | 345 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 | 346 | |
6a488035 TO |
347 | $relationParams = array( |
348 | 'id' => $result['id'], | |
349 | ); | |
350 | // assertDBState compares expected values in $result to actual values in the DB | |
351 | $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); | |
352 | ||
353 | $params['id'] = $result['id']; | |
26dcc9eb | 354 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
355 | $this->relationshipTypeDelete($this->_relTypeID); |
356 | } | |
357 | ||
358 | /** | |
359 | * check with complete array + custom field | |
360 | * Note that the test is written on purpose without any | |
361 | * variables specific to participant so it can be replicated into other entities | |
362 | * and / or moved to the automated test suite | |
363 | */ | |
364 | function testGetWithCustom() { | |
365 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
366 | ||
367 | $params = $this->_params; | |
368 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
369 | ||
26dcc9eb | 370 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
6a488035 TO |
371 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); |
372 | ||
b2402735 | 373 | $getParams = array('id' => $result['id']); |
26dcc9eb | 374 | $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__); |
6a488035 TO |
375 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
376 | ||
377 | $this->customFieldDelete($ids['custom_field_id']); | |
378 | $this->customGroupDelete($ids['custom_group_id']); | |
379 | } | |
380 | ||
381 | function createCustomGroup() { | |
382 | $params = array( | |
383 | 'title' => 'Custom Group', | |
384 | 'extends' => array('Relationship'), | |
385 | 'weight' => 5, | |
386 | 'style' => 'Inline', | |
387 | 'is_active' => 1, | |
388 | 'max_multiple' => 0, | |
6a488035 | 389 | ); |
26dcc9eb | 390 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
391 | $this->_customGroupId = $customGroup['id']; |
392 | return $customGroup['id']; | |
393 | } | |
394 | ||
395 | function createCustomField() { | |
396 | $ids = array(); | |
397 | $params = array( | |
398 | 'custom_group_id' => $this->_customGroupId, | |
399 | 'label' => 'Enter text about relationship', | |
400 | 'html_type' => 'Text', | |
401 | 'data_type' => 'String', | |
402 | 'default_value' => 'xyz', | |
403 | 'weight' => 1, | |
404 | 'is_required' => 1, | |
405 | 'is_searchable' => 0, | |
406 | 'is_active' => 1, | |
6a488035 TO |
407 | ); |
408 | ||
409 | ||
26dcc9eb | 410 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
6a488035 TO |
411 | |
412 | $customField = NULL; | |
413 | $ids[] = $customField['result']['customFieldId']; | |
414 | ||
415 | $optionValue[] = array( | |
416 | 'label' => 'Red', | |
417 | 'value' => 'R', | |
418 | 'weight' => 1, | |
419 | 'is_active' => 1, | |
420 | ); | |
421 | $optionValue[] = array( | |
422 | 'label' => 'Yellow', | |
423 | 'value' => 'Y', | |
424 | 'weight' => 2, | |
425 | 'is_active' => 1, | |
426 | ); | |
427 | $optionValue[] = array( | |
428 | 'label' => 'Green', | |
429 | 'value' => 'G', | |
430 | 'weight' => 3, | |
431 | 'is_active' => 1, | |
432 | ); | |
433 | ||
434 | $params = array( | |
435 | 'label' => 'Pick Color', | |
436 | 'html_type' => 'Select', | |
437 | 'data_type' => 'String', | |
438 | 'weight' => 2, | |
439 | 'is_required' => 1, | |
440 | 'is_searchable' => 0, | |
441 | 'is_active' => 1, | |
442 | 'option_values' => $optionValue, | |
443 | 'custom_group_id' => $this->_customGroupId, | |
6a488035 TO |
444 | ); |
445 | ||
26dcc9eb | 446 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
447 | $ids[] = $customField['id']; |
448 | ||
449 | $params = array( | |
450 | 'custom_group_id' => $this->_customGroupId, | |
451 | 'name' => 'test_date', | |
452 | 'label' => 'test_date', | |
453 | 'html_type' => 'Select Date', | |
454 | 'data_type' => 'Date', | |
455 | 'default_value' => '20090711', | |
456 | 'weight' => 3, | |
457 | 'is_required' => 1, | |
458 | 'is_searchable' => 0, | |
459 | 'is_active' => 1, | |
6a488035 TO |
460 | ); |
461 | ||
26dcc9eb | 462 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
463 | |
464 | $ids[] = $customField['id']; | |
465 | $params = array( | |
466 | 'custom_group_id' => $this->_customGroupId, | |
467 | 'name' => 'test_link', | |
468 | 'label' => 'test_link', | |
469 | 'html_type' => 'Link', | |
470 | 'data_type' => 'Link', | |
471 | 'default_value' => 'http://civicrm.org', | |
472 | 'weight' => 4, | |
473 | 'is_required' => 1, | |
474 | 'is_searchable' => 0, | |
475 | 'is_active' => 1, | |
6a488035 TO |
476 | ); |
477 | ||
26dcc9eb | 478 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
479 | $ids[] = $customField['id']; |
480 | return $ids; | |
481 | } | |
482 | ||
483 | ///////////////// civicrm_relationship_delete methods | |
484 | ||
485 | /** | |
486 | * check with empty array | |
487 | */ | |
488 | function testRelationshipDeleteEmpty() { | |
b2402735 | 489 | $params = array(); |
490 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
491 | } |
492 | ||
6a488035 TO |
493 | /** |
494 | * check if required fields are not passed | |
495 | */ | |
496 | function testRelationshipDeleteWithoutRequired() { | |
497 | $params = array( | |
498 | 'start_date' => '2008-12-20', | |
499 | 'end_date' => '2009-12-20', | |
500 | 'is_active' => 1, | |
501 | ); | |
502 | ||
75638074 | 503 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
504 | } |
505 | ||
506 | /** | |
507 | * check with incorrect required fields | |
508 | */ | |
509 | function testRelationshipDeleteWithIncorrectData() { | |
510 | $params = array( | |
511 | 'contact_id_a' => $this->_cId_a, | |
512 | 'contact_id_b' => $this->_cId_b, | |
513 | 'relationship_type_id' => 'Breaking Relationship', | |
6a488035 TO |
514 | ); |
515 | ||
75638074 | 516 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
517 | |
518 | $params['id'] = "Invalid"; | |
75638074 | 519 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID'); |
6a488035 TO |
520 | } |
521 | ||
522 | /** | |
523 | * check relationship creation | |
524 | */ | |
525 | function testRelationshipDelete() { | |
526 | $params = array( | |
527 | 'contact_id_a' => $this->_cId_a, | |
528 | 'contact_id_b' => $this->_cId_b, | |
529 | 'relationship_type_id' => $this->_relTypeID, | |
530 | 'start_date' => '2008-12-20', | |
531 | 'is_active' => 1, | |
6a488035 TO |
532 | ); |
533 | ||
26dcc9eb | 534 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
535 | |
536 | //Delete relationship | |
537 | $params = array(); | |
538 | $params['id'] = $result['id']; | |
539 | ||
26dcc9eb | 540 | $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
541 | $this->relationshipTypeDelete($this->_relTypeID); |
542 | } | |
543 | ||
544 | ///////////////// civicrm_relationship_update methods | |
545 | ||
546 | /** | |
547 | * check with empty array | |
548 | */ | |
549 | function testRelationshipUpdateEmpty() { | |
75638074 | 550 | $result = $this->callAPIFailure('relationship', 'create', array(), |
551 | 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id'); | |
6a488035 TO |
552 | } |
553 | ||
6a488035 TO |
554 | /** |
555 | * check if required fields are not passed | |
556 | */ | |
557 | ||
558 | /** | |
559 | * check relationship update | |
560 | */ | |
82a0ae6b | 561 | function testRelationshipCreateDuplicate() { |
6a488035 TO |
562 | $relParams = array( |
563 | 'contact_id_a' => $this->_cId_a, | |
564 | 'contact_id_b' => $this->_cId_b, | |
565 | 'relationship_type_id' => $this->_relTypeID, | |
566 | 'start_date' => '20081214', | |
567 | 'end_date' => '20091214', | |
568 | 'is_active' => 1, | |
6a488035 TO |
569 | ); |
570 | ||
26dcc9eb | 571 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 | 572 | |
75638074 | 573 | $this->assertNotNull($result['id']); |
6a488035 TO |
574 | $this->_relationID = $result['id']; |
575 | ||
576 | $params = array( | |
6a488035 TO |
577 | 'contact_id_a' => $this->_cId_a, |
578 | 'contact_id_b' => $this->_cId_b, | |
579 | 'relationship_type_id' => $this->_relTypeID, | |
580 | 'start_date' => '20081214', | |
b2402735 | 581 | 'end_date' => '20091214', |
582 | 'is_active' => 0, | |
6a488035 TO |
583 | ); |
584 | ||
75638074 | 585 | $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists'); |
6a488035 TO |
586 | |
587 | //delete created relationship | |
588 | $params = array( | |
589 | 'id' => $this->_relationID, | |
6a488035 TO |
590 | ); |
591 | ||
b2402735 | 592 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
593 | |
594 | //delete created relationship type | |
595 | $this->relationshipTypeDelete($this->_relTypeID); | |
596 | } | |
597 | ||
598 | /** | |
599 | * check with valid params array. | |
600 | */ | |
601 | function testRelationshipsGet() { | |
602 | $relParams = array( | |
603 | 'contact_id_a' => $this->_cId_a, | |
604 | 'contact_id_b' => $this->_cId_b, | |
605 | 'relationship_type_id' => $this->_relTypeID, | |
606 | 'start_date' => '2011-01-01', | |
607 | 'end_date' => '2013-01-01', | |
608 | 'is_active' => 1, | |
6a488035 TO |
609 | ); |
610 | ||
b2402735 | 611 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
612 | |
613 | //get relationship | |
614 | $params = array( | |
615 | 'contact_id' => $this->_cId_b, | |
6a488035 | 616 | ); |
26dcc9eb | 617 | $result = $this->callAPISuccess('relationship', 'get', $params); |
75638074 | 618 | $this->assertEquals($result['count'], 1); |
6a488035 TO |
619 | $params = array( |
620 | 'contact_id_a' => $this->_cId_a, | |
6a488035 | 621 | ); |
26dcc9eb | 622 | $result = $this->callAPISuccess('relationship', 'get', $params); |
75638074 | 623 | $this->assertEquals($result['count'], 1); |
6a488035 TO |
624 | // contact_id_a is wrong so should be no matches |
625 | $params = array( | |
626 | 'contact_id_a' => $this->_cId_b, | |
6a488035 | 627 | ); |
26dcc9eb | 628 | $result = $this->callAPISuccess('relationship', 'get', $params); |
75638074 | 629 | $this->assertEquals($result['count'], 0); |
6a488035 TO |
630 | } |
631 | ||
632 | /** | |
633 | * check with valid params array. | |
634 | * (The get function will behave differently without 'contact_id' passed | |
635 | */ | |
636 | function testRelationshipsGetGeneric() { | |
637 | $relParams = array( | |
638 | 'contact_id_a' => $this->_cId_a, | |
639 | 'contact_id_b' => $this->_cId_b, | |
640 | 'relationship_type_id' => $this->_relTypeID, | |
641 | 'start_date' => '2011-01-01', | |
642 | 'end_date' => '2013-01-01', | |
643 | 'is_active' => 1, | |
6a488035 TO |
644 | ); |
645 | ||
26dcc9eb | 646 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
647 | |
648 | //get relationship | |
649 | $params = array( | |
650 | 'contact_id_b' => $this->_cId_b, | |
6a488035 | 651 | ); |
26dcc9eb | 652 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
653 | } |
654 | ||
655 | function testGetIsCurrent() { | |
656 | $rel2Params =array( | |
657 | 'contact_id_a' => $this->_cId_a, | |
658 | 'contact_id_b' => $this->_cId_b2, | |
659 | 'relationship_type_id' => $this->_relTypeID, | |
660 | 'start_date' => '2008-12-20', | |
661 | 'is_active' => 0, | |
6a488035 | 662 | ); |
26dcc9eb | 663 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
664 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); | |
665 | ||
6a488035 | 666 | $getParams = array( |
6a488035 TO |
667 | 'filters' => array('is_current' => 1) |
668 | ); | |
669 | $description = "demonstrates is_current filter"; | |
670 | $subfile = 'filterIsCurrent'; | |
671 | //no relationship has been created | |
26dcc9eb | 672 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
673 | $this->assertEquals($result['count'], 1); |
674 | $this->AssertEquals($rel1['id'], $result['id']); | |
675 | ||
676 | // now try not started | |
677 | $rel2Params['is_active'] =1; | |
678 | $rel2Params['start_date'] ='tomorrow'; | |
26dcc9eb | 679 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
680 | $result = $this->callAPISuccess('relationship', 'get', $getParams); | |
6a488035 TO |
681 | $this->assertEquals($result['count'], 1); |
682 | $this->AssertEquals($rel1['id'], $result['id']); | |
683 | ||
684 | // now try finished | |
685 | $rel2Params['is_active'] =1; | |
686 | $rel2Params['start_date'] ='last week'; | |
687 | $rel2Params['end_date'] ='yesterday'; | |
26dcc9eb | 688 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
6a488035 TO |
689 | } |
690 | /* | |
691 | * Test using various operators | |
692 | */ | |
693 | function testGetTypeOperators() { | |
694 | $relTypeParams = array( | |
695 | 'name_a_b' => 'Relation 3 for delete', | |
696 | 'name_b_a' => 'Relation 6 for delete', | |
697 | 'description' => 'Testing relationship type 2', | |
698 | 'contact_type_a' => 'Individual', | |
699 | 'contact_type_b' => 'Organization', | |
700 | 'is_reserved' => 1, | |
701 | 'is_active' => 1, | |
6a488035 TO |
702 | ); |
703 | $relationType2 = $this->relationshipTypeCreate($relTypeParams); | |
704 | $relTypeParams = array( | |
705 | 'name_a_b' => 'Relation 8 for delete', | |
706 | 'name_b_a' => 'Relation 9 for delete', | |
707 | 'description' => 'Testing relationship type 7', | |
708 | 'contact_type_a' => 'Individual', | |
709 | 'contact_type_b' => 'Organization', | |
710 | 'is_reserved' => 1, | |
711 | 'is_active' => 1, | |
6a488035 TO |
712 | ); |
713 | $relationType3 = $this->relationshipTypeCreate($relTypeParams); | |
714 | ||
715 | $relTypeParams = array( | |
716 | 'name_a_b' => 'Relation 6 for delete', | |
717 | 'name_b_a' => 'Relation 88for delete', | |
718 | 'description' => 'Testing relationship type 00', | |
719 | 'contact_type_a' => 'Individual', | |
720 | 'contact_type_b' => 'Organization', | |
721 | 'is_reserved' => 1, | |
722 | 'is_active' => 1, | |
6a488035 TO |
723 | ); |
724 | $relationType4 = $this->relationshipTypeCreate($relTypeParams); | |
725 | ||
26dcc9eb | 726 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); |
727 | $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, | |
6a488035 | 728 | array('relationship_type_id' => $relationType2,))); |
26dcc9eb | 729 | $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 730 | array('relationship_type_id' => $relationType3,))); |
26dcc9eb | 731 | $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 732 | array('relationship_type_id' => $relationType4,))); |
6a488035 TO |
733 | |
734 | $getParams = array( | |
26dcc9eb | 735 | 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)) |
6a488035 TO |
736 | ); |
737 | ||
6a488035 TO |
738 | $description = "demonstrates use of IN filter"; |
739 | $subfile = 'INRelationshipType'; | |
740 | ||
26dcc9eb | 741 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
742 | $this->assertEquals($result['count'], 2); |
743 | $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values'])); | |
744 | ||
745 | $description = "demonstrates use of NOT IN filter"; | |
746 | $subfile = 'NotInRelationshipType'; | |
747 | $getParams = array( | |
6a488035 TO |
748 | 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)) |
749 | ); | |
26dcc9eb | 750 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
751 | $this->assertEquals($result['count'], 2); |
752 | $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values'])); | |
753 | ||
754 | $description = "demonstrates use of BETWEEN filter"; | |
755 | $subfile = 'BetweenRelationshipType'; | |
756 | $getParams = array( | |
6a488035 TO |
757 | 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)) |
758 | ); | |
26dcc9eb | 759 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
760 | $this->assertEquals($result['count'], 3); |
761 | $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values'])); | |
762 | ||
763 | $description = "demonstrates use of Not BETWEEN filter"; | |
764 | $subfile = 'NotBetweenRelationshipType'; | |
765 | $getParams = array( | |
6a488035 TO |
766 | 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)) |
767 | ); | |
26dcc9eb | 768 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
769 | $this->assertEquals($result['count'], 1); |
770 | $this->AssertEquals(array($rel1['id'],), array_keys($result['values'])); | |
771 | ||
772 | } | |
773 | /** | |
774 | * check with invalid relationshipType Id | |
775 | */ | |
776 | function testRelationshipTypeAddInvalidId() { | |
777 | $relTypeParams = array( | |
778 | 'id' => 'invalid', | |
779 | 'name_a_b' => 'Relation 1 for delete', | |
780 | 'name_b_a' => 'Relation 2 for delete', | |
781 | 'contact_type_a' => 'Individual', | |
782 | 'contact_type_b' => 'Organization', | |
6a488035 | 783 | ); |
75638074 | 784 | $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams, |
785 | 'id is not a valid integer'); | |
6a488035 TO |
786 | } |
787 | ||
6a488035 TO |
788 | /** |
789 | * check with valid data with contact_b | |
790 | */ | |
791 | function testGetRelationshipWithContactB() { | |
792 | $relParams = array( | |
793 | 'contact_id_a' => $this->_cId_a, | |
794 | 'contact_id_b' => $this->_cId_b, | |
795 | 'relationship_type_id' => $this->_relTypeID, | |
796 | 'start_date' => '2011-01-01', | |
797 | 'end_date' => '2013-01-01', | |
798 | 'is_active' => 1, | |
6a488035 TO |
799 | ); |
800 | ||
26dcc9eb | 801 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
802 | |
803 | $contacts = array( | |
804 | 'contact_id' => $this->_cId_a, | |
6a488035 TO |
805 | ); |
806 | ||
26dcc9eb | 807 | $result = $this->callAPISuccess('relationship', 'get', $contacts); |
75638074 | 808 | $this->assertGreaterThan(0, $result['count']); |
6a488035 TO |
809 | $params = array( |
810 | 'id' => $relationship['id'], | |
6a488035 | 811 | ); |
26dcc9eb | 812 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
813 | $this->relationshipTypeDelete($this->_relTypeID); |
814 | } | |
815 | ||
816 | /** | |
817 | * check with valid data with relationshipTypes | |
818 | */ | |
819 | function testGetRelationshipWithRelTypes() { | |
820 | $relParams = array( | |
821 | 'contact_id_a' => $this->_cId_a, | |
822 | 'contact_id_b' => $this->_cId_b, | |
823 | 'relationship_type_id' => $this->_relTypeID, | |
824 | 'start_date' => '2011-01-01', | |
825 | 'end_date' => '2013-01-01', | |
826 | 'is_active' => 1, | |
6a488035 TO |
827 | ); |
828 | ||
26dcc9eb | 829 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
830 | |
831 | $contact_a = array( | |
832 | 'contact_id' => $this->_cId_a, | |
6a488035 | 833 | ); |
26dcc9eb | 834 | $result = $this->callAPISuccess('relationship', 'get', $contact_a); |
6a488035 TO |
835 | |
836 | $params = array( | |
837 | 'id' => $relationship['id'], | |
6a488035 | 838 | ); |
26dcc9eb | 839 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
840 | $this->relationshipTypeDelete($this->_relTypeID); |
841 | } | |
75638074 | 842 | |
843 | /** | |
844 | * Checks that passing in 'contact_id' + a relationship type | |
845 | * will filter by relationship type (relationships go in both directions) | |
846 | * as relationship api does a reciprocal check if contact_id provided | |
847 | * | |
848 | * We should get 1 result without or with correct relationship type id & 0 with | |
849 | * an incorrect one | |
850 | */ | |
851 | function testGetRelationshipByTypeReciprocal() { | |
852 | $created = $this->callAPISuccess($this->_entity, 'create', $this->_params); | |
853 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
854 | 'contact_id' => $this->_cId_a, | |
855 | 'relationship_type_id' => $this->_relTypeID, | |
856 | )); | |
857 | $this->assertEquals(1, $result['count']); | |
858 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
859 | 'contact_id' => $this->_cId_a, | |
860 | 'relationship_type_id' => $this->_relTypeID + 1, | |
861 | )); | |
862 | $this->assertEquals(0, $result['count']); | |
863 | $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id'])); | |
864 | } | |
865 | ||
866 | /** | |
867 | * Checks that passing in 'contact_id_b' + a relationship type | |
868 | * will filter by relationship type for contact b | |
869 | * | |
870 | * We should get 1 result without or with correct relationship type id & 0 with | |
871 | * an incorrect one | |
872 | */ | |
873 | function testGetRelationshipByTypeDAO() { | |
874 | $this->ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE,) + $this->_params); | |
875 | $result = $this->callAPISuccess($this->_entity, 'getcount', array( | |
876 | 'contact_id_a' => $this->_cId_a,), | |
877 | 1); | |
878 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
879 | 'contact_id_a' => $this->_cId_a, | |
880 | 'relationship_type_id' => $this->_relTypeID, | |
881 | )); | |
882 | $this->assertEquals(1, $result['count']); | |
883 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
884 | 'contact_id_a' => $this->_cId_a, | |
885 | 'relationship_type_id' => $this->_relTypeID + 1, | |
886 | )); | |
887 | $this->assertEquals(0, $result['count']); | |
888 | } | |
889 | ||
890 | /** | |
891 | * Checks that passing in 'contact_id_b' + a relationship type | |
892 | * will filter by relationship type for contact b | |
893 | * | |
894 | * We should get 1 result without or with correct relationship type id & 0 with | |
895 | * an incorrect one | |
896 | */ | |
897 | function testGetRelationshipByTypeArrayDAO() { | |
898 | $created = $this->callAPISuccess($this->_entity, 'create', $this->_params); | |
899 | $org3 = $this->organizationCreate(); | |
900 | $relType2 = 5; // lets just assume built in ones aren't being messed with! | |
901 | $relType3 = 6; // lets just assume built in ones aren't being messed with! | |
902 | ||
903 | //relationshp 2 | |
904 | $this->callAPISuccess($this->_entity, 'create', | |
905 | array_merge($this->_params, array( | |
906 | 'relationship_type_id' => $relType2, | |
907 | 'contact_id_b' => $this->_cId_b2)) | |
908 | ); | |
909 | ||
910 | //relationshp 3 | |
911 | $this->callAPISuccess($this->_entity, 'create', | |
912 | array_merge($this->_params, array( | |
913 | 'relationship_type_id' => $relType3, | |
914 | 'contact_id_b' => $org3)) | |
915 | ); | |
916 | ||
917 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
918 | 'contact_id_a' => $this->_cId_a, | |
919 | 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)), | |
920 | )); | |
921 | ||
922 | $this->assertEquals(2, $result['count']); | |
923 | foreach ($result['values'] as $key => $value) { | |
924 | $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3))); | |
925 | } | |
926 | } | |
ac8aaa49 | 927 | |
928 | /** | |
929 | * Checks that passing in 'contact_id_b' + a relationship type | |
930 | * will filter by relationship type for contact b | |
931 | * | |
932 | * We should get 1 result without or with correct relationship type id & 0 with | |
933 | * an incorrect one | |
934 | */ | |
935 | function testGetRelationshipByTypeArrayReciprocal() { | |
936 | $created = $this->callAPISuccess($this->_entity, 'create', $this->_params); | |
937 | $org3 = $this->organizationCreate(); | |
938 | $relType2 = 5; // lets just assume built in ones aren't being messed with! | |
939 | $relType3 = 6; // lets just assume built in ones aren't being messed with! | |
940 | ||
941 | //relationshp 2 | |
942 | $this->callAPISuccess($this->_entity, 'create', | |
943 | array_merge($this->_params, array( | |
944 | 'relationship_type_id' => $relType2, | |
945 | 'contact_id_b' => $this->_cId_b2)) | |
946 | ); | |
947 | ||
948 | //relationshp 3 | |
949 | $this->callAPISuccess($this->_entity, 'create', | |
950 | array_merge($this->_params, array( | |
951 | 'relationship_type_id' => $relType3, | |
952 | 'contact_id_b' => $org3)) | |
953 | ); | |
954 | ||
955 | $result = $this->callAPISuccess($this->_entity, 'get', array( | |
956 | 'contact_id' => $this->_cId_a, | |
957 | 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)), | |
958 | )); | |
959 | ||
960 | $this->assertEquals(2, $result['count']); | |
961 | foreach ($result['values'] as $key => $value) { | |
962 | $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3))); | |
963 | } | |
964 | } | |
6a488035 TO |
965 | } |
966 |