tests/ - Remove get_info()
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
CommitLineData
6a488035 1<?php
7fbb4198 2/**
6a488035 3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Class contains api test cases for "civicrm_relationship"
32 *
33 */
34class api_v3_RelationshipTest extends CiviUnitTestCase {
b2402735 35 protected $_apiversion = 3;
6a488035 36 protected $_cId_a;
c9c41397 37 /**
100fef9d 38 * Second individual
c9c41397 39 * @var integer
40 */
41 protected $_cId_a_2;
6a488035
TO
42 protected $_cId_b;
43 protected $_cId_b2;// second org
44 protected $_relTypeID;
45 protected $_ids = array();
46 protected $_customGroupId = NULL;
47 protected $_customFieldId = NULL;
48 protected $_params;
b7c9bc4c 49
6a488035 50 protected $_entity;
4cbe18b8 51
6a488035
TO
52 function setUp() {
53 parent::setUp();
c9c41397 54 $this->_cId_a = $this->individualCreate();
55 $this->_cId_a_2 = $this->individualCreate(array('last_name' => 'c2', 'email' => 'c@w.com', 'contact_type' => 'Individual'));
56 $this->_cId_b = $this->organizationCreate();
57 $this->_cId_b2 = $this->organizationCreate(array('organization_name' => ' Org 2'));
58 $this->_entity = 'relationship';
6a488035
TO
59 //Create a relationship type
60 $relTypeParams = array(
61 'name_a_b' => 'Relation 1 for delete',
62 'name_b_a' => 'Relation 2 for delete',
63 'description' => 'Testing relationship type',
64 'contact_type_a' => 'Individual',
65 'contact_type_b' => 'Organization',
66 'is_reserved' => 1,
67 'is_active' => 1,
6a488035 68 );
b2402735 69
6a488035
TO
70 $this->_relTypeID = $this->relationshipTypeCreate($relTypeParams);
71 $this->_params = array(
72 'contact_id_a' => $this->_cId_a,
73 'contact_id_b' => $this->_cId_b,
74 'relationship_type_id' => $this->_relTypeID,
75 'start_date' => '2008-12-20',
76 'is_active' => 1,
6a488035
TO
77 );
78
79 }
80
81 function tearDown() {
6a488035 82 $this->contactDelete($this->_cId_a);
c9c41397 83 $this->contactDelete($this->_cId_a_2);
6a488035 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 /**
100fef9d 93 * Check with empty array
6a488035
TO
94 */
95 function testRelationshipCreateEmpty() {
b2402735 96 $this->callAPIFailure('relationship', 'create', array());
6a488035
TO
97 }
98
99 /**
100fef9d 100 * Check if required fields are not passed
6a488035
TO
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 /**
100fef9d 113 * Check with incorrect required fields
6a488035
TO
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 /**
100fef9d 146 * Check relationship creation with invalid Relationship
6a488035
TO
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 /**
100fef9d 173 * Check relationship already exists
6a488035
TO
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 /**
100fef9d 199 * Check relationship already exists
6a488035
TO
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 /**
100fef9d 224 * Checkupdate doesn't reset stuff badly - CRM-11789
6a488035
TO
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 /**
100fef9d 252 * Check relationship creation
6a488035
TO
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 283 }
49f8272d 284 /**
100fef9d 285 * Ensure disabling works
49f8272d
E
286 */
287 function testRelationshipUpdate() {
288 $result = $this->callAPISuccess('relationship', 'create', $this->_params);
289 $relID = $result['id'];
290 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
291 $this->assertEquals($relID, $result['id']);
292 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
293 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
294 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
295 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
296 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
297 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
298 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
299 }
6a488035 300 /**
100fef9d 301 * Check relationship creation
6a488035
TO
302 */
303 function testRelationshipCreateEmptyEndDate() {
304 $params = array(
305 'contact_id_a' => $this->_cId_a,
306 'contact_id_b' => $this->_cId_b,
307 'relationship_type_id' => $this->_relTypeID,
308 'start_date' => '2010-10-30',
309 'end_date' => '',
310 'is_active' => 1,
311 'note' => 'note',
6a488035
TO
312 );
313
26dcc9eb 314 $result = $this->callAPISuccess('relationship', 'create', $params);
75638074 315 $this->assertNotNull($result['id']);
6a488035
TO
316 $relationParams = array(
317 'id' => $result['id'],
318 );
319
320 // assertDBState compares expected values in $result to actual values in the DB
321 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
26dcc9eb 322 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
6a488035
TO
323 $values = $result['values'][$result['id']];
324 foreach ($params as $key => $value) {
b2402735 325 if ($key == 'note') {
6a488035
TO
326 continue;
327 }
328 if($key == 'end_date'){
329 $this->assertTrue(empty($values[$key]));
330 continue;
331 }
332 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
333 }
334 $params['id'] = $result['id'];
26dcc9eb 335 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
336 }
337
338 /**
100fef9d 339 * Check relationship creation with custom data
6a488035
TO
340 */
341 function testRelationshipCreateWithCustomData() {
342 $customGroup = $this->createCustomGroup();
343 $this->_ids = $this->createCustomField();
344 //few custom Values for comparing
345 $custom_params = array(
346 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
347 "custom_{$this->_ids[1]}" => 'Y',
348 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
349 "custom_{$this->_ids[3]}" => 'http://example.com',
350 );
351
352 $params = array(
353 'contact_id_a' => $this->_cId_a,
354 'contact_id_b' => $this->_cId_b,
355 'relationship_type_id' => $this->_relTypeID,
356 'start_date' => '2008-12-20',
357 'is_active' => 1,
6a488035
TO
358 );
359 $params = array_merge($params, $custom_params);
26dcc9eb 360 $result = $this->callAPISuccess('relationship', 'create', $params);
6a488035 361
6a488035
TO
362 $relationParams = array(
363 'id' => $result['id'],
364 );
365 // assertDBState compares expected values in $result to actual values in the DB
366 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
367
368 $params['id'] = $result['id'];
26dcc9eb 369 $result = $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
370 $this->relationshipTypeDelete($this->_relTypeID);
371 }
372
373 /**
100fef9d 374 * Check with complete array + custom field
6a488035
TO
375 * Note that the test is written on purpose without any
376 * variables specific to participant so it can be replicated into other entities
377 * and / or moved to the automated test suite
378 */
379 function testGetWithCustom() {
380 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
381
382 $params = $this->_params;
383 $params['custom_' . $ids['custom_field_id']] = "custom string";
384
26dcc9eb 385 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035
TO
386 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
387
b2402735 388 $getParams = array('id' => $result['id']);
26dcc9eb 389 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
390 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
391
392 $this->customFieldDelete($ids['custom_field_id']);
393 $this->customGroupDelete($ids['custom_group_id']);
394 }
395
4cbe18b8
EM
396 /**
397 * @return mixed
398 */
6a488035
TO
399 function createCustomGroup() {
400 $params = array(
401 'title' => 'Custom Group',
402 'extends' => array('Relationship'),
403 'weight' => 5,
404 'style' => 'Inline',
405 'is_active' => 1,
406 'max_multiple' => 0,
6a488035 407 );
26dcc9eb 408 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
6a488035
TO
409 $this->_customGroupId = $customGroup['id'];
410 return $customGroup['id'];
411 }
412
4cbe18b8
EM
413 /**
414 * @return array
415 */
6a488035
TO
416 function createCustomField() {
417 $ids = array();
418 $params = array(
419 'custom_group_id' => $this->_customGroupId,
420 'label' => 'Enter text about relationship',
421 'html_type' => 'Text',
422 'data_type' => 'String',
423 'default_value' => 'xyz',
424 'weight' => 1,
425 'is_required' => 1,
426 'is_searchable' => 0,
427 'is_active' => 1,
6a488035
TO
428 );
429
430
26dcc9eb 431 $result = $this->callAPISuccess('CustomField', 'create', $params);
6a488035
TO
432
433 $customField = NULL;
434 $ids[] = $customField['result']['customFieldId'];
435
436 $optionValue[] = array(
437 'label' => 'Red',
438 'value' => 'R',
439 'weight' => 1,
440 'is_active' => 1,
441 );
442 $optionValue[] = array(
443 'label' => 'Yellow',
444 'value' => 'Y',
445 'weight' => 2,
446 'is_active' => 1,
447 );
448 $optionValue[] = array(
449 'label' => 'Green',
450 'value' => 'G',
451 'weight' => 3,
452 'is_active' => 1,
453 );
454
455 $params = array(
456 'label' => 'Pick Color',
457 'html_type' => 'Select',
458 'data_type' => 'String',
459 'weight' => 2,
460 'is_required' => 1,
461 'is_searchable' => 0,
462 'is_active' => 1,
463 'option_values' => $optionValue,
464 'custom_group_id' => $this->_customGroupId,
6a488035
TO
465 );
466
26dcc9eb 467 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
468 $ids[] = $customField['id'];
469
470 $params = array(
471 'custom_group_id' => $this->_customGroupId,
472 'name' => 'test_date',
473 'label' => 'test_date',
474 'html_type' => 'Select Date',
475 'data_type' => 'Date',
476 'default_value' => '20090711',
477 'weight' => 3,
478 'is_required' => 1,
479 'is_searchable' => 0,
480 'is_active' => 1,
6a488035
TO
481 );
482
26dcc9eb 483 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
484
485 $ids[] = $customField['id'];
486 $params = array(
487 'custom_group_id' => $this->_customGroupId,
488 'name' => 'test_link',
489 'label' => 'test_link',
490 'html_type' => 'Link',
491 'data_type' => 'Link',
492 'default_value' => 'http://civicrm.org',
493 'weight' => 4,
494 'is_required' => 1,
495 'is_searchable' => 0,
496 'is_active' => 1,
6a488035
TO
497 );
498
26dcc9eb 499 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
500 $ids[] = $customField['id'];
501 return $ids;
502 }
503
504 ///////////////// civicrm_relationship_delete methods
505
506 /**
100fef9d 507 * Check with empty array
6a488035
TO
508 */
509 function testRelationshipDeleteEmpty() {
b2402735 510 $params = array();
511 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
512 }
513
6a488035 514 /**
100fef9d 515 * Check if required fields are not passed
6a488035
TO
516 */
517 function testRelationshipDeleteWithoutRequired() {
518 $params = array(
519 'start_date' => '2008-12-20',
520 'end_date' => '2009-12-20',
521 'is_active' => 1,
522 );
523
75638074 524 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
525 }
526
527 /**
100fef9d 528 * Check with incorrect required fields
6a488035
TO
529 */
530 function testRelationshipDeleteWithIncorrectData() {
531 $params = array(
532 'contact_id_a' => $this->_cId_a,
533 'contact_id_b' => $this->_cId_b,
534 'relationship_type_id' => 'Breaking Relationship',
6a488035
TO
535 );
536
75638074 537 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
538
539 $params['id'] = "Invalid";
75638074 540 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
6a488035
TO
541 }
542
543 /**
100fef9d 544 * Check relationship creation
6a488035
TO
545 */
546 function testRelationshipDelete() {
547 $params = array(
548 'contact_id_a' => $this->_cId_a,
549 'contact_id_b' => $this->_cId_b,
550 'relationship_type_id' => $this->_relTypeID,
551 'start_date' => '2008-12-20',
552 'is_active' => 1,
6a488035
TO
553 );
554
26dcc9eb 555 $result = $this->callAPISuccess('relationship', 'create', $params);
6a488035
TO
556
557 //Delete relationship
558 $params = array();
559 $params['id'] = $result['id'];
560
26dcc9eb 561 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
562 $this->relationshipTypeDelete($this->_relTypeID);
563 }
564
565 ///////////////// civicrm_relationship_update methods
566
567 /**
100fef9d 568 * Check with empty array
6a488035
TO
569 */
570 function testRelationshipUpdateEmpty() {
75638074 571 $result = $this->callAPIFailure('relationship', 'create', array(),
572 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
6a488035
TO
573 }
574
6a488035 575 /**
100fef9d 576 * Check if required fields are not passed
6a488035
TO
577 */
578
579 /**
100fef9d 580 * Check relationship update
6a488035 581 */
82a0ae6b 582 function testRelationshipCreateDuplicate() {
6a488035
TO
583 $relParams = array(
584 'contact_id_a' => $this->_cId_a,
585 'contact_id_b' => $this->_cId_b,
586 'relationship_type_id' => $this->_relTypeID,
587 'start_date' => '20081214',
588 'end_date' => '20091214',
589 'is_active' => 1,
6a488035
TO
590 );
591
26dcc9eb 592 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 593
75638074 594 $this->assertNotNull($result['id']);
6a488035
TO
595 $this->_relationID = $result['id'];
596
597 $params = array(
6a488035
TO
598 'contact_id_a' => $this->_cId_a,
599 'contact_id_b' => $this->_cId_b,
600 'relationship_type_id' => $this->_relTypeID,
601 'start_date' => '20081214',
b2402735 602 'end_date' => '20091214',
603 'is_active' => 0,
6a488035
TO
604 );
605
75638074 606 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
6a488035
TO
607
608 //delete created relationship
609 $params = array(
610 'id' => $this->_relationID,
6a488035
TO
611 );
612
b2402735 613 $result = $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
614
615 //delete created relationship type
616 $this->relationshipTypeDelete($this->_relTypeID);
617 }
618
619 /**
100fef9d 620 * Check with valid params array.
6a488035
TO
621 */
622 function testRelationshipsGet() {
623 $relParams = array(
624 'contact_id_a' => $this->_cId_a,
625 'contact_id_b' => $this->_cId_b,
626 'relationship_type_id' => $this->_relTypeID,
627 'start_date' => '2011-01-01',
628 'end_date' => '2013-01-01',
629 'is_active' => 1,
6a488035
TO
630 );
631
b2402735 632 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
633
634 //get relationship
635 $params = array(
636 'contact_id' => $this->_cId_b,
6a488035 637 );
26dcc9eb 638 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 639 $this->assertEquals($result['count'], 1);
6a488035
TO
640 $params = array(
641 'contact_id_a' => $this->_cId_a,
6a488035 642 );
26dcc9eb 643 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 644 $this->assertEquals($result['count'], 1);
6a488035
TO
645 // contact_id_a is wrong so should be no matches
646 $params = array(
647 'contact_id_a' => $this->_cId_b,
6a488035 648 );
26dcc9eb 649 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 650 $this->assertEquals($result['count'], 0);
6a488035
TO
651 }
652
653 /**
100fef9d 654 * Check with valid params array.
6a488035
TO
655 * (The get function will behave differently without 'contact_id' passed
656 */
657 function testRelationshipsGetGeneric() {
658 $relParams = array(
659 'contact_id_a' => $this->_cId_a,
660 'contact_id_b' => $this->_cId_b,
661 'relationship_type_id' => $this->_relTypeID,
662 'start_date' => '2011-01-01',
663 'end_date' => '2013-01-01',
664 'is_active' => 1,
6a488035
TO
665 );
666
26dcc9eb 667 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
668
669 //get relationship
670 $params = array(
671 'contact_id_b' => $this->_cId_b,
6a488035 672 );
26dcc9eb 673 $result = $this->callAPISuccess('relationship', 'get', $params);
6a488035
TO
674 }
675
676 function testGetIsCurrent() {
677 $rel2Params =array(
678 'contact_id_a' => $this->_cId_a,
679 'contact_id_b' => $this->_cId_b2,
680 'relationship_type_id' => $this->_relTypeID,
681 'start_date' => '2008-12-20',
682 'is_active' => 0,
6a488035 683 );
26dcc9eb 684 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
685 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
686
6a488035 687 $getParams = array(
6a488035
TO
688 'filters' => array('is_current' => 1)
689 );
690 $description = "demonstrates is_current filter";
691 $subfile = 'filterIsCurrent';
692 //no relationship has been created
26dcc9eb 693 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
694 $this->assertEquals($result['count'], 1);
695 $this->AssertEquals($rel1['id'], $result['id']);
696
697 // now try not started
698 $rel2Params['is_active'] =1;
699 $rel2Params['start_date'] ='tomorrow';
26dcc9eb 700 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
701 $result = $this->callAPISuccess('relationship', 'get', $getParams);
6a488035
TO
702 $this->assertEquals($result['count'], 1);
703 $this->AssertEquals($rel1['id'], $result['id']);
704
705 // now try finished
706 $rel2Params['is_active'] =1;
707 $rel2Params['start_date'] ='last week';
708 $rel2Params['end_date'] ='yesterday';
26dcc9eb 709 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
6a488035
TO
710 }
711 /*
712 * Test using various operators
713 */
714 function testGetTypeOperators() {
715 $relTypeParams = array(
716 'name_a_b' => 'Relation 3 for delete',
717 'name_b_a' => 'Relation 6 for delete',
718 'description' => 'Testing relationship type 2',
719 'contact_type_a' => 'Individual',
720 'contact_type_b' => 'Organization',
721 'is_reserved' => 1,
722 'is_active' => 1,
6a488035
TO
723 );
724 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
725 $relTypeParams = array(
726 'name_a_b' => 'Relation 8 for delete',
727 'name_b_a' => 'Relation 9 for delete',
728 'description' => 'Testing relationship type 7',
729 'contact_type_a' => 'Individual',
730 'contact_type_b' => 'Organization',
731 'is_reserved' => 1,
732 'is_active' => 1,
6a488035
TO
733 );
734 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
735
736 $relTypeParams = array(
737 'name_a_b' => 'Relation 6 for delete',
738 'name_b_a' => 'Relation 88for delete',
739 'description' => 'Testing relationship type 00',
740 'contact_type_a' => 'Individual',
741 'contact_type_b' => 'Organization',
742 'is_reserved' => 1,
743 'is_active' => 1,
6a488035
TO
744 );
745 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
746
26dcc9eb 747 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
748 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6a488035 749 array('relationship_type_id' => $relationType2,)));
26dcc9eb 750 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6a488035 751 array('relationship_type_id' => $relationType3,)));
26dcc9eb 752 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
6a488035 753 array('relationship_type_id' => $relationType4,)));
6a488035
TO
754
755 $getParams = array(
26dcc9eb 756 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
6a488035
TO
757 );
758
6a488035
TO
759 $description = "demonstrates use of IN filter";
760 $subfile = 'INRelationshipType';
761
26dcc9eb 762 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
763 $this->assertEquals($result['count'], 2);
764 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
765
766 $description = "demonstrates use of NOT IN filter";
767 $subfile = 'NotInRelationshipType';
768 $getParams = array(
6a488035
TO
769 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
770 );
26dcc9eb 771 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
772 $this->assertEquals($result['count'], 2);
773 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
774
775 $description = "demonstrates use of BETWEEN filter";
776 $subfile = 'BetweenRelationshipType';
777 $getParams = array(
6a488035
TO
778 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
779 );
26dcc9eb 780 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
781 $this->assertEquals($result['count'], 3);
782 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
783
784 $description = "demonstrates use of Not BETWEEN filter";
785 $subfile = 'NotBetweenRelationshipType';
786 $getParams = array(
6a488035
TO
787 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
788 );
26dcc9eb 789 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
790 $this->assertEquals($result['count'], 1);
791 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
792
793 }
794 /**
100fef9d 795 * Check with invalid relationshipType Id
6a488035
TO
796 */
797 function testRelationshipTypeAddInvalidId() {
798 $relTypeParams = array(
799 'id' => 'invalid',
800 'name_a_b' => 'Relation 1 for delete',
801 'name_b_a' => 'Relation 2 for delete',
802 'contact_type_a' => 'Individual',
803 'contact_type_b' => 'Organization',
6a488035 804 );
75638074 805 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
806 'id is not a valid integer');
6a488035
TO
807 }
808
6a488035 809 /**
100fef9d 810 * Check with valid data with contact_b
6a488035
TO
811 */
812 function testGetRelationshipWithContactB() {
813 $relParams = array(
814 'contact_id_a' => $this->_cId_a,
815 'contact_id_b' => $this->_cId_b,
816 'relationship_type_id' => $this->_relTypeID,
817 'start_date' => '2011-01-01',
818 'end_date' => '2013-01-01',
819 'is_active' => 1,
6a488035
TO
820 );
821
26dcc9eb 822 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
823
824 $contacts = array(
825 'contact_id' => $this->_cId_a,
6a488035
TO
826 );
827
26dcc9eb 828 $result = $this->callAPISuccess('relationship', 'get', $contacts);
75638074 829 $this->assertGreaterThan(0, $result['count']);
6a488035
TO
830 $params = array(
831 'id' => $relationship['id'],
6a488035 832 );
26dcc9eb 833 $result = $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
834 $this->relationshipTypeDelete($this->_relTypeID);
835 }
836
837 /**
100fef9d 838 * Check with valid data with relationshipTypes
6a488035
TO
839 */
840 function testGetRelationshipWithRelTypes() {
841 $relParams = array(
842 'contact_id_a' => $this->_cId_a,
843 'contact_id_b' => $this->_cId_b,
844 'relationship_type_id' => $this->_relTypeID,
845 'start_date' => '2011-01-01',
846 'end_date' => '2013-01-01',
847 'is_active' => 1,
6a488035
TO
848 );
849
26dcc9eb 850 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
851
852 $contact_a = array(
853 'contact_id' => $this->_cId_a,
6a488035 854 );
26dcc9eb 855 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
6a488035
TO
856
857 $params = array(
858 'id' => $relationship['id'],
6a488035 859 );
26dcc9eb 860 $result = $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
861 $this->relationshipTypeDelete($this->_relTypeID);
862 }
75638074 863
864 /**
865 * Checks that passing in 'contact_id' + a relationship type
866 * will filter by relationship type (relationships go in both directions)
867 * as relationship api does a reciprocal check if contact_id provided
868 *
869 * We should get 1 result without or with correct relationship type id & 0 with
870 * an incorrect one
871 */
872 function testGetRelationshipByTypeReciprocal() {
873 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
874 $result = $this->callAPISuccess($this->_entity, 'get', array(
875 'contact_id' => $this->_cId_a,
876 'relationship_type_id' => $this->_relTypeID,
877 ));
878 $this->assertEquals(1, $result['count']);
879 $result = $this->callAPISuccess($this->_entity, 'get', array(
880 'contact_id' => $this->_cId_a,
881 'relationship_type_id' => $this->_relTypeID + 1,
882 ));
883 $this->assertEquals(0, $result['count']);
884 $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
885 }
886
887 /**
888 * Checks that passing in 'contact_id_b' + a relationship type
889 * will filter by relationship type for contact b
890 *
891 * We should get 1 result without or with correct relationship type id & 0 with
892 * an incorrect one
893 */
894 function testGetRelationshipByTypeDAO() {
895 $this->ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE,) + $this->_params);
896 $result = $this->callAPISuccess($this->_entity, 'getcount', array(
897 'contact_id_a' => $this->_cId_a,),
898 1);
899 $result = $this->callAPISuccess($this->_entity, 'get', array(
900 'contact_id_a' => $this->_cId_a,
901 'relationship_type_id' => $this->_relTypeID,
902 ));
903 $this->assertEquals(1, $result['count']);
904 $result = $this->callAPISuccess($this->_entity, 'get', array(
905 'contact_id_a' => $this->_cId_a,
906 'relationship_type_id' => $this->_relTypeID + 1,
907 ));
908 $this->assertEquals(0, $result['count']);
909 }
910
911 /**
912 * Checks that passing in 'contact_id_b' + a relationship type
913 * will filter by relationship type for contact b
914 *
915 * We should get 1 result without or with correct relationship type id & 0 with
916 * an incorrect one
917 */
918 function testGetRelationshipByTypeArrayDAO() {
919 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
920 $org3 = $this->organizationCreate();
921 $relType2 = 5; // lets just assume built in ones aren't being messed with!
922 $relType3 = 6; // lets just assume built in ones aren't being messed with!
923
924 //relationshp 2
925 $this->callAPISuccess($this->_entity, 'create',
926 array_merge($this->_params, array(
927 'relationship_type_id' => $relType2,
928 'contact_id_b' => $this->_cId_b2))
929 );
930
931 //relationshp 3
932 $this->callAPISuccess($this->_entity, 'create',
933 array_merge($this->_params, array(
934 'relationship_type_id' => $relType3,
935 'contact_id_b' => $org3))
936 );
937
938 $result = $this->callAPISuccess($this->_entity, 'get', array(
939 'contact_id_a' => $this->_cId_a,
940 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
941 ));
942
943 $this->assertEquals(2, $result['count']);
944 foreach ($result['values'] as $key => $value) {
945 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
946 }
947 }
ac8aaa49 948
949 /**
950 * Checks that passing in 'contact_id_b' + a relationship type
951 * will filter by relationship type for contact b
952 *
953 * We should get 1 result without or with correct relationship type id & 0 with
954 * an incorrect one
955 */
956 function testGetRelationshipByTypeArrayReciprocal() {
957 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
958 $org3 = $this->organizationCreate();
959 $relType2 = 5; // lets just assume built in ones aren't being messed with!
960 $relType3 = 6; // lets just assume built in ones aren't being messed with!
961
962 //relationshp 2
963 $this->callAPISuccess($this->_entity, 'create',
964 array_merge($this->_params, array(
965 'relationship_type_id' => $relType2,
966 'contact_id_b' => $this->_cId_b2))
967 );
968
969 //relationshp 3
970 $this->callAPISuccess($this->_entity, 'create',
971 array_merge($this->_params, array(
972 'relationship_type_id' => $relType3,
973 'contact_id_b' => $org3))
974 );
975
976 $result = $this->callAPISuccess($this->_entity, 'get', array(
977 'contact_id' => $this->_cId_a,
978 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
979 ));
980
981 $this->assertEquals(2, $result['count']);
982 foreach ($result['values'] as $key => $value) {
983 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
984 }
985 }
6a488035 986
c9c41397 987 /**
988 * Checks that passing in 'contact_id_b' + a relationship type
989 * will filter by relationship type for contact b
990 *
991 * We should get 1 result without or with correct relationship type id & 0 with
992 * an incorrect one
993 */
994 function testGetRelationshipByMembershipTypeDAO() {
995 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
996 $org3 = $this->organizationCreate();
997
998 $relType2 = 5; // lets just assume built in ones aren't being messed with!
999 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1000 $relType1 = 1;
1001 $memberType = $this->membershipTypeCreate(array(
1002 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1003 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1004 ));
1005
1006 //relationshp 2
1007 $this->callAPISuccess($this->_entity, 'create',
1008 array_merge($this->_params, array(
1009 'relationship_type_id' => $relType2,
1010 'contact_id_b' => $this->_cId_b2))
1011 );
1012
1013 //relationshp 3
1014 $this->callAPISuccess($this->_entity, 'create',
1015 array_merge($this->_params, array(
1016 'relationship_type_id' => $relType3,
1017 'contact_id_b' => $org3))
1018 );
1019
1020 //relationshp 4 with reveral
1021 $this->callAPISuccess($this->_entity, 'create',
1022 array_merge($this->_params, array(
1023 'relationship_type_id' => $relType1,
1024 'contact_id_a' => $this->_cId_a,
1025 'contact_id_b' => $this->_cId_a_2))
1026 );
1027
1028 $result = $this->callAPISuccess($this->_entity, 'get', array(
1029 'contact_id_a' => $this->_cId_a,
1030 'membership_type_id' => $memberType,
1031 ));
1032 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1033 $this->assertEquals(1, $result['count']);
1034 foreach ($result['values'] as $key => $value) {
1035 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1036 }
1037 }
1038
1039 /**
1040 * Checks that passing in 'contact_id_b' + a relationship type
1041 * will filter by relationship type for contact b
1042 *
1043 * We should get 1 result without or with correct relationship type id & 0 with
1044 * an incorrect one
1045 */
1046 function testGetRelationshipByMembershipTypeReciprocal() {
1047 $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1048 $org3 = $this->organizationCreate();
1049
1050 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1051 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1052 $relType1 = 1;
1053 $memberType = $this->membershipTypeCreate(array(
1054 'relationship_type_id' => CRM_Core_DAO::VALUE_SEPARATOR . $relType1 . CRM_Core_DAO::VALUE_SEPARATOR . $relType3 . CRM_Core_DAO::VALUE_SEPARATOR,
1055 'relationship_direction' => CRM_Core_DAO::VALUE_SEPARATOR . 'a_b' . CRM_Core_DAO::VALUE_SEPARATOR . 'b_a' . CRM_Core_DAO::VALUE_SEPARATOR,
1056 ));
1057
1058 //relationshp 2
1059 $this->callAPISuccess($this->_entity, 'create',
1060 array_merge($this->_params, array(
1061 'relationship_type_id' => $relType2,
1062 'contact_id_b' => $this->_cId_b2))
1063 );
1064
1065 //relationshp 3
1066 $this->callAPISuccess($this->_entity, 'create',
1067 array_merge($this->_params, array(
1068 'relationship_type_id' => $relType3,
1069 'contact_id_b' => $org3))
1070 );
1071
1072 //relationshp 4 with reveral
1073 $this->callAPISuccess($this->_entity, 'create',
1074 array_merge($this->_params, array(
1075 'relationship_type_id' => $relType1,
1076 'contact_id_a' => $this->_cId_a,
1077 'contact_id_b' => $this->_cId_a_2))
1078 );
1079
1080 $result = $this->callAPISuccess($this->_entity, 'get', array(
1081 'contact_id' => $this->_cId_a,
1082 'membership_type_id' => $memberType,
1083 ));
1084 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1085 $this->assertEquals(2, $result['count']);
1086
1087 foreach ($result['values'] as $key => $value) {
1088 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1089 }
1090 }
a7361ba3
EM
1091
1092 /**
1093 * Check for enotices on enable & disable as reported in CRM-14350
1094 */
1095 function testSetActive() {
1096 $relationship = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1097 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1098 $this->callAPISuccess($this->_entity, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1099 }
c9c41397 1100}