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