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