Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
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(); | |
6a488035 TO |
56 | $this->_cId_a = $this->individualCreate(NULL); |
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() { | |
26dcc9eb | 83 | $this->quickCleanup(array('civicrm_relationship'), TRUE); |
6a488035 TO |
84 | $this->relationshipTypeDelete($this->_relTypeID); |
85 | $this->contactDelete($this->_cId_a); | |
86 | $this->contactDelete($this->_cId_b); | |
87 | } | |
88 | ||
89 | ///////////////// civicrm_relationship_create methods | |
90 | ||
91 | /** | |
92 | * check with empty array | |
93 | */ | |
94 | function testRelationshipCreateEmpty() { | |
b2402735 | 95 | $this->callAPIFailure('relationship', 'create', array()); |
6a488035 TO |
96 | } |
97 | ||
98 | /** | |
99 | * check if required fields are not passed | |
100 | */ | |
101 | function testRelationshipCreateWithoutRequired() { | |
102 | $params = array( | |
103 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
104 | 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'), | |
105 | 'is_active' => 1, | |
106 | ); | |
107 | ||
d0e1eff2 | 108 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
109 | } |
110 | ||
111 | /** | |
112 | * check with incorrect required fields | |
113 | */ | |
114 | function testRelationshipCreateWithIncorrectData() { | |
115 | ||
116 | $params = array( | |
117 | 'contact_id_a' => $this->_cId_a, | |
118 | 'contact_id_b' => $this->_cId_b, | |
119 | 'relationship_type_id' => 'Breaking Relationship', | |
6a488035 TO |
120 | ); |
121 | ||
d0e1eff2 | 122 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
123 | |
124 | //contact id is not an integer | |
125 | $params = array( | |
126 | 'contact_id_a' => 'invalid', | |
127 | 'contact_id_b' => $this->_cId_b, | |
128 | 'relationship_type_id' => $this->_relTypeID, | |
129 | 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'), | |
130 | 'is_active' => 1, | |
131 | ); | |
d0e1eff2 | 132 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
133 | |
134 | //contact id does not exists | |
135 | $params['contact_id_a'] = 999; | |
d0e1eff2 | 136 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
137 | |
138 | //invalid date | |
139 | $params['contact_id_a'] = $this->_cId_a; | |
140 | $params['start_date'] = array('d' => '1', 'M' => '1'); | |
d0e1eff2 | 141 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
142 | } |
143 | ||
144 | /** | |
145 | * check relationship creation with invalid Relationship | |
146 | */ | |
147 | function testRelationshipCreatInvalidRelationship() { | |
148 | // both the contact of type Individual | |
149 | $params = array( | |
150 | 'contact_id_a' => $this->_cId_a, | |
151 | 'contact_id_b' => $this->_cId_a, | |
152 | 'relationship_type_id' => $this->_relTypeID, | |
153 | 'start_date' => '2008-01-10', | |
154 | 'is_active' => 1, | |
6a488035 TO |
155 | ); |
156 | ||
d0e1eff2 | 157 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
158 | |
159 | // both the contact of type Organization | |
160 | $params = array( | |
161 | 'contact_id_a' => $this->_cId_b, | |
162 | 'contact_id_b' => $this->_cId_b, | |
163 | 'relationship_type_id' => $this->_relTypeID, | |
164 | 'start_date' => '2008-01-10', | |
165 | 'is_active' => 1, | |
6a488035 TO |
166 | ); |
167 | ||
d0e1eff2 | 168 | $this->callAPIFailure('relationship', 'create', $params); |
6a488035 TO |
169 | } |
170 | ||
171 | /** | |
172 | * check relationship already exists | |
173 | */ | |
174 | function testRelationshipCreateAlreadyExists() { | |
175 | $params = array( | |
176 | 'contact_id_a' => $this->_cId_a, | |
177 | 'contact_id_b' => $this->_cId_b, | |
178 | 'relationship_type_id' => $this->_relTypeID, | |
179 | 'start_date' => '2008-12-20', 'end_date' => NULL, | |
180 | 'is_active' => 1, | |
6a488035 | 181 | ); |
b2402735 | 182 | $relationship = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
183 | |
184 | $params = array( | |
185 | 'contact_id_a' => $this->_cId_a, | |
186 | 'contact_id_b' => $this->_cId_b, | |
187 | 'relationship_type_id' => $this->_relTypeID, | |
188 | 'start_date' => '2008-12-20', | |
189 | 'is_active' => 1, | |
6a488035 | 190 | ); |
b2402735 | 191 | $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists'); |
6a488035 TO |
192 | |
193 | $params['id'] = $relationship['id']; | |
b2402735 | 194 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
195 | } |
196 | ||
197 | /** | |
198 | * check relationship already exists | |
199 | */ | |
200 | function testRelationshipCreateUpdateAlreadyExists() { | |
201 | $params = array( | |
202 | 'contact_id_a' => $this->_cId_a, | |
203 | 'contact_id_b' => $this->_cId_b, | |
204 | 'relationship_type_id' => $this->_relTypeID, | |
205 | 'start_date' => '2008-12-20', | |
206 | 'end_date' => NULL, | |
207 | 'is_active' => 1, | |
6a488035 | 208 | |
26dcc9eb | 209 | ); |
210 | $relationship = $this->callAPISuccess('relationship', 'create', $params); | |
6a488035 TO |
211 | $params = array( |
212 | 'id' => $relationship['id'], | |
213 | 'is_active' => 0, | |
6a488035 TO |
214 | 'debug' => 1, |
215 | ); | |
26dcc9eb | 216 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 | 217 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
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__); |
6a488035 TO |
266 | $this->assertNotNull($result['id'], 'in line ' . __LINE__); |
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 | } | |
279 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
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); |
6a488035 TO |
300 | $this->assertNotNull($result['id'], 'in line ' . __LINE__); |
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 | ||
d0e1eff2 CW |
503 | $result = $this->callAPIFailure('relationship', 'delete', $params); |
504 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
505 | } |
506 | ||
507 | /** | |
508 | * check with incorrect required fields | |
509 | */ | |
510 | function testRelationshipDeleteWithIncorrectData() { | |
511 | $params = array( | |
512 | 'contact_id_a' => $this->_cId_a, | |
513 | 'contact_id_b' => $this->_cId_b, | |
514 | 'relationship_type_id' => 'Breaking Relationship', | |
6a488035 TO |
515 | ); |
516 | ||
b2402735 | 517 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__); |
6a488035 TO |
518 | |
519 | $params['id'] = "Invalid"; | |
b2402735 | 520 | $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID', 'in line ' . __LINE__); |
6a488035 TO |
521 | } |
522 | ||
523 | /** | |
524 | * check relationship creation | |
525 | */ | |
526 | function testRelationshipDelete() { | |
527 | $params = array( | |
528 | 'contact_id_a' => $this->_cId_a, | |
529 | 'contact_id_b' => $this->_cId_b, | |
530 | 'relationship_type_id' => $this->_relTypeID, | |
531 | 'start_date' => '2008-12-20', | |
532 | 'is_active' => 1, | |
6a488035 TO |
533 | ); |
534 | ||
26dcc9eb | 535 | $result = $this->callAPISuccess('relationship', 'create', $params); |
6a488035 TO |
536 | |
537 | //Delete relationship | |
538 | $params = array(); | |
539 | $params['id'] = $result['id']; | |
540 | ||
26dcc9eb | 541 | $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
542 | $this->relationshipTypeDelete($this->_relTypeID); |
543 | } | |
544 | ||
545 | ///////////////// civicrm_relationship_update methods | |
546 | ||
547 | /** | |
548 | * check with empty array | |
549 | */ | |
550 | function testRelationshipUpdateEmpty() { | |
d0e1eff2 | 551 | $result = $this->callAPIFailure('relationship', 'create', array()); |
6a488035 TO |
552 | $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__); |
553 | } | |
554 | ||
6a488035 TO |
555 | /** |
556 | * check if required fields are not passed | |
557 | */ | |
558 | ||
559 | /** | |
560 | * check relationship update | |
561 | */ | |
82a0ae6b | 562 | function testRelationshipCreateDuplicate() { |
6a488035 TO |
563 | $relParams = array( |
564 | 'contact_id_a' => $this->_cId_a, | |
565 | 'contact_id_b' => $this->_cId_b, | |
566 | 'relationship_type_id' => $this->_relTypeID, | |
567 | 'start_date' => '20081214', | |
568 | 'end_date' => '20091214', | |
569 | 'is_active' => 1, | |
6a488035 TO |
570 | ); |
571 | ||
26dcc9eb | 572 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
573 | |
574 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
575 | $this->_relationID = $result['id']; | |
576 | ||
577 | $params = array( | |
6a488035 TO |
578 | 'contact_id_a' => $this->_cId_a, |
579 | 'contact_id_b' => $this->_cId_b, | |
580 | 'relationship_type_id' => $this->_relTypeID, | |
581 | 'start_date' => '20081214', | |
b2402735 | 582 | 'end_date' => '20091214', |
583 | 'is_active' => 0, | |
6a488035 TO |
584 | ); |
585 | ||
b2402735 | 586 | $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists', 'In line ' . __LINE__); |
6a488035 TO |
587 | |
588 | //delete created relationship | |
589 | $params = array( | |
590 | 'id' => $this->_relationID, | |
6a488035 TO |
591 | ); |
592 | ||
b2402735 | 593 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
594 | |
595 | //delete created relationship type | |
596 | $this->relationshipTypeDelete($this->_relTypeID); | |
597 | } | |
598 | ||
599 | /** | |
600 | * check with valid params array. | |
601 | */ | |
602 | function testRelationshipsGet() { | |
603 | $relParams = array( | |
604 | 'contact_id_a' => $this->_cId_a, | |
605 | 'contact_id_b' => $this->_cId_b, | |
606 | 'relationship_type_id' => $this->_relTypeID, | |
607 | 'start_date' => '2011-01-01', | |
608 | 'end_date' => '2013-01-01', | |
609 | 'is_active' => 1, | |
6a488035 TO |
610 | ); |
611 | ||
b2402735 | 612 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
613 | |
614 | //get relationship | |
615 | $params = array( | |
616 | 'contact_id' => $this->_cId_b, | |
6a488035 | 617 | ); |
26dcc9eb | 618 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
619 | $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); |
620 | $params = array( | |
621 | 'contact_id_a' => $this->_cId_a, | |
6a488035 | 622 | ); |
26dcc9eb | 623 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
624 | $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); |
625 | // contact_id_a is wrong so should be no matches | |
626 | $params = array( | |
627 | 'contact_id_a' => $this->_cId_b, | |
6a488035 | 628 | ); |
26dcc9eb | 629 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
630 | $this->assertEquals($result['count'], 0, 'in line ' . __LINE__); |
631 | } | |
632 | ||
633 | /** | |
634 | * check with valid params array. | |
635 | * (The get function will behave differently without 'contact_id' passed | |
636 | */ | |
637 | function testRelationshipsGetGeneric() { | |
638 | $relParams = array( | |
639 | 'contact_id_a' => $this->_cId_a, | |
640 | 'contact_id_b' => $this->_cId_b, | |
641 | 'relationship_type_id' => $this->_relTypeID, | |
642 | 'start_date' => '2011-01-01', | |
643 | 'end_date' => '2013-01-01', | |
644 | 'is_active' => 1, | |
6a488035 TO |
645 | ); |
646 | ||
26dcc9eb | 647 | $result = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
648 | |
649 | //get relationship | |
650 | $params = array( | |
651 | 'contact_id_b' => $this->_cId_b, | |
6a488035 | 652 | ); |
26dcc9eb | 653 | $result = $this->callAPISuccess('relationship', 'get', $params); |
6a488035 TO |
654 | } |
655 | ||
656 | function testGetIsCurrent() { | |
657 | $rel2Params =array( | |
658 | 'contact_id_a' => $this->_cId_a, | |
659 | 'contact_id_b' => $this->_cId_b2, | |
660 | 'relationship_type_id' => $this->_relTypeID, | |
661 | 'start_date' => '2008-12-20', | |
662 | 'is_active' => 0, | |
6a488035 | 663 | ); |
26dcc9eb | 664 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
665 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); | |
666 | ||
6a488035 | 667 | $getParams = array( |
6a488035 TO |
668 | 'filters' => array('is_current' => 1) |
669 | ); | |
670 | $description = "demonstrates is_current filter"; | |
671 | $subfile = 'filterIsCurrent'; | |
672 | //no relationship has been created | |
26dcc9eb | 673 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
674 | $this->assertEquals($result['count'], 1); |
675 | $this->AssertEquals($rel1['id'], $result['id']); | |
676 | ||
677 | // now try not started | |
678 | $rel2Params['is_active'] =1; | |
679 | $rel2Params['start_date'] ='tomorrow'; | |
26dcc9eb | 680 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
681 | $result = $this->callAPISuccess('relationship', 'get', $getParams); | |
6a488035 TO |
682 | $this->assertEquals($result['count'], 1); |
683 | $this->AssertEquals($rel1['id'], $result['id']); | |
684 | ||
685 | // now try finished | |
686 | $rel2Params['is_active'] =1; | |
687 | $rel2Params['start_date'] ='last week'; | |
688 | $rel2Params['end_date'] ='yesterday'; | |
26dcc9eb | 689 | $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); |
6a488035 TO |
690 | } |
691 | /* | |
692 | * Test using various operators | |
693 | */ | |
694 | function testGetTypeOperators() { | |
695 | $relTypeParams = array( | |
696 | 'name_a_b' => 'Relation 3 for delete', | |
697 | 'name_b_a' => 'Relation 6 for delete', | |
698 | 'description' => 'Testing relationship type 2', | |
699 | 'contact_type_a' => 'Individual', | |
700 | 'contact_type_b' => 'Organization', | |
701 | 'is_reserved' => 1, | |
702 | 'is_active' => 1, | |
6a488035 TO |
703 | ); |
704 | $relationType2 = $this->relationshipTypeCreate($relTypeParams); | |
705 | $relTypeParams = array( | |
706 | 'name_a_b' => 'Relation 8 for delete', | |
707 | 'name_b_a' => 'Relation 9 for delete', | |
708 | 'description' => 'Testing relationship type 7', | |
709 | 'contact_type_a' => 'Individual', | |
710 | 'contact_type_b' => 'Organization', | |
711 | 'is_reserved' => 1, | |
712 | 'is_active' => 1, | |
6a488035 TO |
713 | ); |
714 | $relationType3 = $this->relationshipTypeCreate($relTypeParams); | |
715 | ||
716 | $relTypeParams = array( | |
717 | 'name_a_b' => 'Relation 6 for delete', | |
718 | 'name_b_a' => 'Relation 88for delete', | |
719 | 'description' => 'Testing relationship type 00', | |
720 | 'contact_type_a' => 'Individual', | |
721 | 'contact_type_b' => 'Organization', | |
722 | 'is_reserved' => 1, | |
723 | 'is_active' => 1, | |
6a488035 TO |
724 | ); |
725 | $relationType4 = $this->relationshipTypeCreate($relTypeParams); | |
726 | ||
26dcc9eb | 727 | $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); |
728 | $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, | |
6a488035 | 729 | array('relationship_type_id' => $relationType2,))); |
26dcc9eb | 730 | $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 731 | array('relationship_type_id' => $relationType3,))); |
26dcc9eb | 732 | $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, |
6a488035 | 733 | array('relationship_type_id' => $relationType4,))); |
6a488035 TO |
734 | |
735 | $getParams = array( | |
26dcc9eb | 736 | 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)) |
6a488035 TO |
737 | ); |
738 | ||
6a488035 TO |
739 | $description = "demonstrates use of IN filter"; |
740 | $subfile = 'INRelationshipType'; | |
741 | ||
26dcc9eb | 742 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
743 | $this->assertEquals($result['count'], 2); |
744 | $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values'])); | |
745 | ||
746 | $description = "demonstrates use of NOT IN filter"; | |
747 | $subfile = 'NotInRelationshipType'; | |
748 | $getParams = array( | |
6a488035 TO |
749 | 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)) |
750 | ); | |
26dcc9eb | 751 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
752 | $this->assertEquals($result['count'], 2); |
753 | $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values'])); | |
754 | ||
755 | $description = "demonstrates use of BETWEEN filter"; | |
756 | $subfile = 'BetweenRelationshipType'; | |
757 | $getParams = array( | |
6a488035 TO |
758 | 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)) |
759 | ); | |
26dcc9eb | 760 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
761 | $this->assertEquals($result['count'], 3); |
762 | $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values'])); | |
763 | ||
764 | $description = "demonstrates use of Not BETWEEN filter"; | |
765 | $subfile = 'NotBetweenRelationshipType'; | |
766 | $getParams = array( | |
6a488035 TO |
767 | 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)) |
768 | ); | |
26dcc9eb | 769 | $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
770 | $this->assertEquals($result['count'], 1); |
771 | $this->AssertEquals(array($rel1['id'],), array_keys($result['values'])); | |
772 | ||
773 | } | |
774 | /** | |
775 | * check with invalid relationshipType Id | |
776 | */ | |
777 | function testRelationshipTypeAddInvalidId() { | |
778 | $relTypeParams = array( | |
779 | 'id' => 'invalid', | |
780 | 'name_a_b' => 'Relation 1 for delete', | |
781 | 'name_b_a' => 'Relation 2 for delete', | |
782 | 'contact_type_a' => 'Individual', | |
783 | 'contact_type_b' => 'Organization', | |
6a488035 | 784 | ); |
d0e1eff2 CW |
785 | $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams); |
786 | $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__); | |
6a488035 TO |
787 | } |
788 | ||
6a488035 TO |
789 | /** |
790 | * check with valid data with contact_b | |
791 | */ | |
792 | function testGetRelationshipWithContactB() { | |
793 | $relParams = array( | |
794 | 'contact_id_a' => $this->_cId_a, | |
795 | 'contact_id_b' => $this->_cId_b, | |
796 | 'relationship_type_id' => $this->_relTypeID, | |
797 | 'start_date' => '2011-01-01', | |
798 | 'end_date' => '2013-01-01', | |
799 | 'is_active' => 1, | |
6a488035 TO |
800 | ); |
801 | ||
26dcc9eb | 802 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
803 | |
804 | $contacts = array( | |
805 | 'contact_id' => $this->_cId_a, | |
6a488035 TO |
806 | ); |
807 | ||
26dcc9eb | 808 | $result = $this->callAPISuccess('relationship', 'get', $contacts); |
6a488035 TO |
809 | $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__); |
810 | $params = array( | |
811 | 'id' => $relationship['id'], | |
6a488035 | 812 | ); |
26dcc9eb | 813 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
814 | $this->relationshipTypeDelete($this->_relTypeID); |
815 | } | |
816 | ||
817 | /** | |
818 | * check with valid data with relationshipTypes | |
819 | */ | |
820 | function testGetRelationshipWithRelTypes() { | |
821 | $relParams = array( | |
822 | 'contact_id_a' => $this->_cId_a, | |
823 | 'contact_id_b' => $this->_cId_b, | |
824 | 'relationship_type_id' => $this->_relTypeID, | |
825 | 'start_date' => '2011-01-01', | |
826 | 'end_date' => '2013-01-01', | |
827 | 'is_active' => 1, | |
6a488035 TO |
828 | ); |
829 | ||
26dcc9eb | 830 | $relationship = $this->callAPISuccess('relationship', 'create', $relParams); |
6a488035 TO |
831 | |
832 | $contact_a = array( | |
833 | 'contact_id' => $this->_cId_a, | |
6a488035 | 834 | ); |
26dcc9eb | 835 | $result = $this->callAPISuccess('relationship', 'get', $contact_a); |
6a488035 TO |
836 | |
837 | $params = array( | |
838 | 'id' => $relationship['id'], | |
6a488035 | 839 | ); |
26dcc9eb | 840 | $result = $this->callAPISuccess('relationship', 'delete', $params); |
6a488035 TO |
841 | $this->relationshipTypeDelete($this->_relTypeID); |
842 | } | |
843 | } | |
844 |