Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTest.php
CommitLineData
6a488035 1<?php
7d61e75f
TO
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
6c6e6187 10 */
6a488035 11
6a488035
TO
12/**
13 * Class contains api test cases for "civicrm_relationship"
acb109b7 14 * @group headless
6a488035
TO
15 */
16class api_v3_RelationshipTest extends CiviUnitTestCase {
8ba5884d 17
18 use CRMTraits_Custom_CustomDataTrait;
19
6a488035 20 protected $_cId_a;
c9c41397 21 /**
eceb18cc 22 * Second individual.
9f266042 23 *
24 * @var int
c9c41397 25 */
26 protected $_cId_a_2;
6a488035 27 protected $_cId_b;
b85df283
EM
28 /**
29 * Second organization contact.
30 *
31 * @var int
32 */
33 protected $_cId_b2;
6a488035 34 protected $_relTypeID;
8ba5884d 35 protected $_ids = [];
6a488035
TO
36 protected $_customFieldId = NULL;
37 protected $_params;
b7c9bc4c 38
8ba5884d 39 protected $entity;
4cbe18b8 40
b85df283
EM
41 /**
42 * Set up function.
43 */
00be9182 44 public function setUp() {
6a488035 45 parent::setUp();
c9c41397 46 $this->_cId_a = $this->individualCreate();
9099cab3 47 $this->_cId_a_2 = $this->individualCreate([
92915c55
TO
48 'last_name' => 'c2',
49 'email' => 'c@w.com',
50 'contact_type' => 'Individual',
9099cab3 51 ]);
c9c41397 52 $this->_cId_b = $this->organizationCreate();
9099cab3 53 $this->_cId_b2 = $this->organizationCreate(['organization_name' => ' Org 2']);
8ba5884d 54 $this->entity = 'Relationship';
b85df283 55 //Create a relationship type.
9099cab3 56 $relTypeParams = [
6a488035
TO
57 'name_a_b' => 'Relation 1 for delete',
58 'name_b_a' => 'Relation 2 for delete',
59 'description' => 'Testing relationship type',
60 'contact_type_a' => 'Individual',
61 'contact_type_b' => 'Organization',
62 'is_reserved' => 1,
63 'is_active' => 1,
9099cab3 64 ];
b2402735 65
6a488035 66 $this->_relTypeID = $this->relationshipTypeCreate($relTypeParams);
9099cab3 67 $this->_params = [
6a488035
TO
68 'contact_id_a' => $this->_cId_a,
69 'contact_id_b' => $this->_cId_b,
70 'relationship_type_id' => $this->_relTypeID,
71 'start_date' => '2008-12-20',
72 'is_active' => 1,
9099cab3 73 ];
6a488035
TO
74
75 }
76
b85df283
EM
77 /**
78 * Tear down function.
79 *
80 * @throws \Exception
81 */
00be9182 82 public function tearDown() {
6a488035 83 $this->contactDelete($this->_cId_a);
c9c41397 84 $this->contactDelete($this->_cId_a_2);
6a488035 85 $this->contactDelete($this->_cId_b);
75638074 86 $this->contactDelete($this->_cId_b2);
9099cab3 87 $this->quickCleanup(['civicrm_relationship'], TRUE);
75638074 88 $this->relationshipTypeDelete($this->_relTypeID);
fda18dc3 89 parent::tearDown();
6a488035
TO
90 }
91
6a488035 92 /**
eceb18cc 93 * Check with empty array.
2d932085
CW
94 * @param int $version
95 * @dataProvider versionThreeAndFour
6a488035 96 */
2d932085
CW
97 public function testRelationshipCreateEmpty($version) {
98 $this->_apiversion = $version;
9099cab3 99 $this->callAPIFailure('relationship', 'create', []);
6a488035
TO
100 }
101
df0c42cc
JP
102 /**
103 * Test Current Employer is correctly set.
f42bcfb1
MD
104 *
105 * @throws \CRM_Core_Exception
df0c42cc
JP
106 */
107 public function testCurrentEmployerRelationship() {
9099cab3 108 $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', [
f42bcfb1
MD
109 'return' => 'id',
110 'name_b_a' => 'Employer Of',
9099cab3
CW
111 ]);
112 $employerRelationship = $this->callAPISuccess('Relationship', 'create', [
df0c42cc
JP
113 'contact_id_a' => $this->_cId_a,
114 'contact_id_b' => $this->_cId_b,
115 'relationship_type_id' => $employerRelationshipID,
f42bcfb1 116 'is_current_employer' => 1,
9099cab3 117 ]);
df0c42cc
JP
118
119 //Check if current employer is correctly set.
9099cab3 120 $employer = $this->callAPISuccessGetValue('Contact', [
f42bcfb1 121 'return' => 'current_employer',
df0c42cc 122 'id' => $this->_cId_a,
9099cab3
CW
123 ]);
124 $organisation = $this->callAPISuccessGetValue('Contact', [
df0c42cc
JP
125 'return' => "sort_name",
126 'id' => $this->_cId_b,
9099cab3 127 ]);
df0c42cc
JP
128 $this->assertEquals($employer, $organisation);
129
130 //Update relationship type
58bcd8be 131 $this->callAPISuccess('Relationship', 'create', [
df0c42cc
JP
132 'id' => $employerRelationship['id'],
133 'relationship_type_id' => $this->_relTypeID,
9099cab3
CW
134 ]);
135 $employeeContact = $this->callAPISuccessGetSingle('Contact', [
f42bcfb1 136 'return' => ['current_employer'],
df0c42cc 137 'id' => $this->_cId_a,
9099cab3 138 ]);
df0c42cc
JP
139 //current employer should be removed.
140 $this->assertEmpty($employeeContact['current_employer']);
141 }
142
6a488035 143 /**
eceb18cc 144 * Check if required fields are not passed.
2d932085
CW
145 * @param int $version
146 * @dataProvider versionThreeAndFour
6a488035 147 */
2d932085
CW
148 public function testRelationshipCreateWithoutRequired($version) {
149 $this->_apiversion = $version;
9099cab3
CW
150 $params = [
151 'start_date' => ['d' => '10', 'M' => '1', 'Y' => '2008'],
152 'end_date' => ['d' => '10', 'M' => '1', 'Y' => '2009'],
6a488035 153 'is_active' => 1,
9099cab3 154 ];
6a488035 155
d0e1eff2 156 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
157 }
158
159 /**
eceb18cc 160 * Check with incorrect required fields.
2d932085
CW
161 * @param int $version
162 * @dataProvider versionThreeAndFour
6a488035 163 */
2d932085
CW
164 public function testRelationshipCreateWithIncorrectData($version) {
165 $this->_apiversion = $version;
6a488035 166
9099cab3 167 $params = [
6a488035
TO
168 'contact_id_a' => $this->_cId_a,
169 'contact_id_b' => $this->_cId_b,
170 'relationship_type_id' => 'Breaking Relationship',
9099cab3 171 ];
6a488035 172
d0e1eff2 173 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
174
175 //contact id is not an integer
9099cab3 176 $params = [
6a488035
TO
177 'contact_id_a' => 'invalid',
178 'contact_id_b' => $this->_cId_b,
179 'relationship_type_id' => $this->_relTypeID,
9099cab3 180 'start_date' => ['d' => '10', 'M' => '1', 'Y' => '2008'],
6a488035 181 'is_active' => 1,
9099cab3 182 ];
d0e1eff2 183 $this->callAPIFailure('relationship', 'create', $params);
6a488035 184
b85df283 185 // Contact id does not exist.
6a488035 186 $params['contact_id_a'] = 999;
d0e1eff2 187 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
188
189 //invalid date
190 $params['contact_id_a'] = $this->_cId_a;
9099cab3 191 $params['start_date'] = ['d' => '1', 'M' => '1'];
d0e1eff2 192 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
193 }
194
195 /**
eceb18cc 196 * Check relationship creation with invalid Relationship.
2d932085
CW
197 * @param int $version
198 * @dataProvider versionThreeAndFour
6a488035 199 */
2d932085
CW
200 public function testRelationshipCreateInvalidRelationship($version) {
201 $this->_apiversion = $version;
b85df283 202 // Both have the contact type Individual.
9099cab3 203 $params = [
6a488035
TO
204 'contact_id_a' => $this->_cId_a,
205 'contact_id_b' => $this->_cId_a,
206 'relationship_type_id' => $this->_relTypeID,
207 'start_date' => '2008-01-10',
208 'is_active' => 1,
9099cab3 209 ];
6a488035 210
d0e1eff2 211 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
212
213 // both the contact of type Organization
9099cab3 214 $params = [
6a488035
TO
215 'contact_id_a' => $this->_cId_b,
216 'contact_id_b' => $this->_cId_b,
217 'relationship_type_id' => $this->_relTypeID,
218 'start_date' => '2008-01-10',
219 'is_active' => 1,
9099cab3 220 ];
6a488035 221
d0e1eff2 222 $this->callAPIFailure('relationship', 'create', $params);
6a488035
TO
223 }
224
225 /**
eceb18cc 226 * Check relationship already exists.
2d932085
CW
227 * @param int $version
228 * @dataProvider versionThreeAndFour
6a488035 229 */
2d932085
CW
230 public function testRelationshipCreateAlreadyExists($version) {
231 $this->_apiversion = $version;
9099cab3 232 $params = [
6a488035
TO
233 'contact_id_a' => $this->_cId_a,
234 'contact_id_b' => $this->_cId_b,
235 'relationship_type_id' => $this->_relTypeID,
6c6e6187 236 'start_date' => '2008-12-20',
5896d037 237 'end_date' => NULL,
6a488035 238 'is_active' => 1,
9099cab3 239 ];
b2402735 240 $relationship = $this->callAPISuccess('relationship', 'create', $params);
6a488035 241
9099cab3 242 $params = [
6a488035
TO
243 'contact_id_a' => $this->_cId_a,
244 'contact_id_b' => $this->_cId_b,
245 'relationship_type_id' => $this->_relTypeID,
246 'start_date' => '2008-12-20',
247 'is_active' => 1,
9099cab3 248 ];
d235daf6 249 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
6a488035
TO
250
251 $params['id'] = $relationship['id'];
b85df283 252 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
253 }
254
255 /**
eceb18cc 256 * Check relationship already exists.
2d932085
CW
257 * @param int $version
258 * @dataProvider versionThreeAndFour
6a488035 259 */
2d932085
CW
260 public function testRelationshipCreateUpdateAlreadyExists($version) {
261 $this->_apiversion = $version;
9099cab3 262 $params = [
6a488035
TO
263 'contact_id_a' => $this->_cId_a,
264 'contact_id_b' => $this->_cId_b,
265 'relationship_type_id' => $this->_relTypeID,
266 'start_date' => '2008-12-20',
267 'end_date' => NULL,
268 'is_active' => 1,
6a488035 269
9099cab3 270 ];
26dcc9eb 271 $relationship = $this->callAPISuccess('relationship', 'create', $params);
9099cab3 272 $params = [
6a488035
TO
273 'id' => $relationship['id'],
274 'is_active' => 0,
6a488035 275 'debug' => 1,
9099cab3 276 ];
b85df283
EM
277 $this->callAPISuccess('relationship', 'create', $params);
278 $this->callAPISuccess('relationship', 'get', $params);
6a488035 279 $params['id'] = $relationship['id'];
b85df283 280 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
281 }
282
283 /**
b85df283 284 * Check update doesn't reset stuff badly - CRM-11789.
2d932085
CW
285 * @param int $version
286 * @dataProvider versionThreeAndFour
6a488035 287 */
2d932085
CW
288 public function testRelationshipCreateUpdateDoesNotMangle($version) {
289 $this->_apiversion = $version;
9099cab3 290 $params = [
6a488035
TO
291 'contact_id_a' => $this->_cId_a,
292 'contact_id_b' => $this->_cId_b,
293 'relationship_type_id' => $this->_relTypeID,
294 'start_date' => '2008-12-20',
6a488035
TO
295 'is_active' => 1,
296 'is_permission_a_b' => 1,
297 'description' => 'my desc',
9099cab3 298 ];
26dcc9eb 299 $relationship = $this->callAPISuccess('relationship', 'create', $params);
6a488035 300
9099cab3 301 $updateParams = [
6a488035 302 'id' => $relationship['id'],
6a488035 303 'relationship_type_id' => $this->_relTypeID,
9099cab3 304 ];
b85df283 305 $this->callAPISuccess('relationship', 'create', $updateParams);
6a488035 306
6a488035
TO
307 //make sure the orig params didn't get changed
308 $this->getAndCheck($params, $relationship['id'], 'relationship');
309
310 }
311
6a488035 312 /**
eceb18cc 313 * Check relationship creation.
2d932085
CW
314 * @param int $version
315 * @dataProvider versionThreeAndFour
6a488035 316 */
2d932085
CW
317 public function testRelationshipCreate($version) {
318 $this->_apiversion = $version;
9099cab3 319 $params = [
6a488035
TO
320 'contact_id_a' => $this->_cId_a,
321 'contact_id_b' => $this->_cId_b,
322 'relationship_type_id' => $this->_relTypeID,
323 'start_date' => '2010-10-30',
324 'end_date' => '2010-12-30',
325 'is_active' => 1,
326 'note' => 'note',
9099cab3 327 ];
6a488035 328
26dcc9eb 329 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__);
75638074 330 $this->assertNotNull($result['id']);
9099cab3 331 $relationParams = [
6a488035 332 'id' => $result['id'],
9099cab3 333 ];
6a488035
TO
334
335 // assertDBState compares expected values in $result to actual values in the DB
336 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
9099cab3 337 $result = $this->callAPISuccess('relationship', 'get', ['id' => $result['id']]);
6a488035
TO
338 $values = $result['values'][$result['id']];
339 foreach ($params as $key => $value) {
b2402735 340 if ($key == 'note') {
6a488035
TO
341 continue;
342 }
75638074 343 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
6a488035
TO
344 }
345 $params['id'] = $result['id'];
26dcc9eb 346 $this->callAPISuccess('relationship', 'delete', $params);
6a488035 347 }
5896d037 348
49f8272d 349 /**
eceb18cc 350 * Ensure disabling works.
2d932085
CW
351 * @param int $version
352 * @dataProvider versionThreeAndFour
49f8272d 353 */
2d932085
CW
354 public function testRelationshipUpdate($version) {
355 $this->_apiversion = $version;
49f8272d
E
356 $result = $this->callAPISuccess('relationship', 'create', $this->_params);
357 $relID = $result['id'];
9099cab3 358 $result = $this->callAPISuccess('relationship', 'create', ['id' => $relID, 'description' => 'blah']);
49f8272d 359 $this->assertEquals($relID, $result['id']);
936e43d6 360
49f8272d 361 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
936e43d6 362
9099cab3 363 $result = $this->callAPISuccess('relationship', 'create', ['id' => $relID, 'is_permission_b_a' => 1]);
49f8272d 364 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
9099cab3
CW
365 $result = $this->callAPISuccess('relationship', 'create', ['id' => $result['id'], 'is_active' => 0]);
366 $result = $this->callAPISuccess('relationship', 'get', ['id' => $result['id']]);
49f8272d
E
367 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
368 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
369 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
370 }
5896d037 371
6a488035 372 /**
eceb18cc 373 * Check relationship creation.
2d932085
CW
374 * @param int $version
375 * @dataProvider versionThreeAndFour
6a488035 376 */
2d932085
CW
377 public function testRelationshipCreateEmptyEndDate($version) {
378 $this->_apiversion = $version;
9099cab3 379 $params = [
6a488035
TO
380 'contact_id_a' => $this->_cId_a,
381 'contact_id_b' => $this->_cId_b,
382 'relationship_type_id' => $this->_relTypeID,
383 'start_date' => '2010-10-30',
384 'end_date' => '',
385 'is_active' => 1,
386 'note' => 'note',
9099cab3 387 ];
6a488035 388
26dcc9eb 389 $result = $this->callAPISuccess('relationship', 'create', $params);
75638074 390 $this->assertNotNull($result['id']);
9099cab3 391 $relationParams = [
6a488035 392 'id' => $result['id'],
9099cab3 393 ];
6a488035
TO
394
395 // assertDBState compares expected values in $result to actual values in the DB
396 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
9099cab3 397 $result = $this->callAPISuccess('relationship', 'get', ['id' => $result['id']]);
6a488035
TO
398 $values = $result['values'][$result['id']];
399 foreach ($params as $key => $value) {
b2402735 400 if ($key == 'note') {
6a488035
TO
401 continue;
402 }
5896d037 403 if ($key == 'end_date') {
6a488035
TO
404 $this->assertTrue(empty($values[$key]));
405 continue;
406 }
407 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
408 }
409 $params['id'] = $result['id'];
26dcc9eb 410 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
411 }
412
413 /**
eceb18cc 414 * Check relationship creation with custom data.
2d932085 415 * FIXME: Api4
6a488035 416 */
00d84e9b 417 public function testRelationshipCreateEditWithCustomData() {
119664d6 418 $this->createCustomGroupWithFieldsOfAllTypes();
6a488035 419 //few custom Values for comparing
119664d6 420 $custom_params = [
421 $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
422 $this->getCustomFieldName('select_string') => 'Y',
423 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
424 $this->getCustomFieldName('link') => 'http://example.com',
425 ];
426
427 $params = [
6a488035
TO
428 'contact_id_a' => $this->_cId_a,
429 'contact_id_b' => $this->_cId_b,
430 'relationship_type_id' => $this->_relTypeID,
431 'start_date' => '2008-12-20',
432 'is_active' => 1,
119664d6 433 ];
6a488035 434 $params = array_merge($params, $custom_params);
26dcc9eb 435 $result = $this->callAPISuccess('relationship', 'create', $params);
6a488035 436
119664d6 437 $relationParams = ['id' => $result['id']];
6a488035
TO
438 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
439
00d84e9b 440 //Test Edit of custom field from the form.
9099cab3
CW
441 $getParams = ['id' => $result['id']];
442 $updateParams = array_merge($getParams, [
119664d6 443 $this->getCustomFieldName('text') => 'Edited Text Value',
00d84e9b
JP
444 'relationship_type_id' => $this->_relTypeID . '_b_a',
445 'related_contact_id' => $this->_cId_a,
9099cab3 446 ]);
00d84e9b
JP
447 $reln = new CRM_Contact_Form_Relationship();
448 $reln->_action = CRM_Core_Action::UPDATE;
449 $reln->_relationshipId = $result['id'];
450 $reln->submit($updateParams);
451
452 $check = $this->callAPISuccess('relationship', 'get', $getParams);
119664d6 453 $this->assertEquals("Edited Text Value", $check['values'][$check['id']][$this->getCustomFieldName('text')]);
00d84e9b 454
6a488035 455 $params['id'] = $result['id'];
b85df283 456 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
457 $this->relationshipTypeDelete($this->_relTypeID);
458 }
459
460 /**
100fef9d 461 * Check with complete array + custom field
6a488035
TO
462 * Note that the test is written on purpose without any
463 * variables specific to participant so it can be replicated into other entities
464 * and / or moved to the automated test suite
2d932085 465 * FIXME: Api4
6a488035 466 */
00be9182 467 public function testGetWithCustom() {
6a488035
TO
468 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
469
470 $params = $this->_params;
471 $params['custom_' . $ids['custom_field_id']] = "custom string";
472
8ba5884d 473 $result = $this->callAPISuccess($this->entity, 'create', $params);
6a488035
TO
474 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
475
9099cab3 476 $getParams = ['id' => $result['id']];
8ba5884d 477 $check = $this->callAPIAndDocument($this->entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
478 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
479
480 $this->customFieldDelete($ids['custom_field_id']);
481 $this->customGroupDelete($ids['custom_group_id']);
482 }
483
6a488035 484 /**
eceb18cc 485 * Check if required fields are not passed.
2d932085
CW
486 * @param int $version
487 * @dataProvider versionThreeAndFour
6a488035 488 */
2d932085
CW
489 public function testRelationshipDeleteWithoutRequired($version) {
490 $this->_apiversion = $version;
9099cab3 491 $params = [
6a488035
TO
492 'start_date' => '2008-12-20',
493 'end_date' => '2009-12-20',
494 'is_active' => 1,
9099cab3 495 ];
6a488035 496
2d932085 497 $this->callAPIFailure('relationship', 'delete', $params);
6a488035
TO
498 }
499
500 /**
eceb18cc 501 * Check with incorrect required fields.
6a488035 502 */
00be9182 503 public function testRelationshipDeleteWithIncorrectData() {
9099cab3 504 $params = [
6a488035
TO
505 'contact_id_a' => $this->_cId_a,
506 'contact_id_b' => $this->_cId_b,
507 'relationship_type_id' => 'Breaking Relationship',
9099cab3 508 ];
6a488035 509
b85df283 510 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
511
512 $params['id'] = "Invalid";
4f94e3fa 513 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
6a488035
TO
514 }
515
516 /**
eceb18cc 517 * Check relationship creation.
2d932085
CW
518 * @param int $version
519 * @dataProvider versionThreeAndFour
6a488035 520 */
2d932085
CW
521 public function testRelationshipDelete($version) {
522 $this->_apiversion = $version;
9099cab3 523 $params = [
6a488035
TO
524 'contact_id_a' => $this->_cId_a,
525 'contact_id_b' => $this->_cId_b,
526 'relationship_type_id' => $this->_relTypeID,
527 'start_date' => '2008-12-20',
528 'is_active' => 1,
9099cab3 529 ];
6a488035 530
26dcc9eb 531 $result = $this->callAPISuccess('relationship', 'create', $params);
9099cab3 532 $params = ['id' => $result['id']];
b85df283 533 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
534 $this->relationshipTypeDelete($this->_relTypeID);
535 }
536
537 ///////////////// civicrm_relationship_update methods
538
539 /**
eceb18cc 540 * Check with empty array.
2d932085
CW
541 * @param int $version
542 * @dataProvider versionThreeAndFour
6a488035 543 */
2d932085
CW
544 public function testRelationshipUpdateEmpty($version) {
545 $this->_apiversion = $version;
9099cab3 546 $this->callAPIFailure('relationship', 'create', [],
2d932085 547 'contact_id_a, contact_id_b, relationship_type_id');
6a488035
TO
548 }
549
6a488035 550 /**
eceb18cc 551 * Check if required fields are not passed.
6a488035
TO
552 */
553
554 /**
eceb18cc 555 * Check relationship update.
2d932085
CW
556 * @param int $version
557 * @dataProvider versionThreeAndFour
6a488035 558 */
2d932085
CW
559 public function testRelationshipCreateDuplicate($version) {
560 $this->_apiversion = $version;
9099cab3 561 $relParams = [
6a488035
TO
562 'contact_id_a' => $this->_cId_a,
563 'contact_id_b' => $this->_cId_b,
564 'relationship_type_id' => $this->_relTypeID,
565 'start_date' => '20081214',
566 'end_date' => '20091214',
567 'is_active' => 1,
9099cab3 568 ];
6a488035 569
26dcc9eb 570 $result = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 571
75638074 572 $this->assertNotNull($result['id']);
6a488035 573
9099cab3 574 $params = [
6a488035
TO
575 'contact_id_a' => $this->_cId_a,
576 'contact_id_b' => $this->_cId_b,
577 'relationship_type_id' => $this->_relTypeID,
578 'start_date' => '20081214',
b2402735 579 'end_date' => '20091214',
580 'is_active' => 0,
9099cab3 581 ];
6a488035 582
4f94e3fa 583 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
6a488035 584
9099cab3 585 $this->callAPISuccess('relationship', 'delete', ['id' => $result['id']]);
6a488035
TO
586 $this->relationshipTypeDelete($this->_relTypeID);
587 }
588
69f20614
JV
589 /**
590 * CRM-13725 - Two relationships of same type with same start and end date
591 * should be OK if the custom field values differ.
2d932085 592 * FIXME: Api4
69f20614
JV
593 */
594 public function testRelationshipCreateDuplicateWithCustomFields() {
119664d6 595 $this->createCustomGroupWithFieldsOfAllTypes();
69f20614 596
9099cab3 597 $custom_params_1 = [
119664d6 598 $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
599 $this->getCustomFieldName('select_string') => 'Y',
600 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
601 $this->getCustomFieldName('link') => 'http://example.com',
9099cab3 602 ];
69f20614 603
9099cab3 604 $custom_params_2 = [
119664d6 605 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
606 $this->getCustomFieldName('select_string') => 'Y',
607 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
608 $this->getCustomFieldName('link') => 'http://example.org',
9099cab3 609 ];
69f20614 610
9099cab3 611 $params = [
69f20614
JV
612 'contact_id_a' => $this->_cId_a,
613 'contact_id_b' => $this->_cId_b,
614 'relationship_type_id' => $this->_relTypeID,
615 'start_date' => '2008-12-20',
616 'is_active' => 1,
9099cab3 617 ];
69f20614
JV
618
619 $params_1 = array_merge($params, $custom_params_1);
620 $params_2 = array_merge($params, $custom_params_2);
621
622 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
623 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
624
625 $this->assertNotNull($result_2['id']);
626 $this->assertEquals(0, $result_2['is_error']);
627
628 $this->relationshipTypeDelete($this->_relTypeID);
629 }
630
a12d81e1
JV
631 /**
632 * CRM-13725 - Two relationships of same type with same start and end date
633 * should be OK if the custom field values differ. In this case, the
634 * existing relationship does not have custom values, but the new one
635 * does.
2d932085 636 * FIXME: Api4
a12d81e1
JV
637 */
638 public function testRelationshipCreateDuplicateWithCustomFields2() {
119664d6 639 $this->createCustomGroupWithFieldsOfAllTypes();
a12d81e1 640
9099cab3 641 $custom_params_2 = [
119664d6 642 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
643 $this->getCustomFieldName('select_string') => 'Y',
644 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
645 $this->getCustomFieldName('link') => 'http://example.org',
9099cab3 646 ];
a12d81e1 647
119664d6 648 $params_1 = [
a12d81e1
JV
649 'contact_id_a' => $this->_cId_a,
650 'contact_id_b' => $this->_cId_b,
651 'relationship_type_id' => $this->_relTypeID,
652 'start_date' => '2008-12-20',
653 'is_active' => 1,
119664d6 654 ];
a12d81e1
JV
655
656 $params_2 = array_merge($params_1, $custom_params_2);
657
658 $this->callAPISuccess('relationship', 'create', $params_1);
659 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
660
661 $this->assertNotNull($result_2['id']);
662 $this->assertEquals(0, $result_2['is_error']);
663
664 $this->relationshipTypeDelete($this->_relTypeID);
665 }
666
667 /**
668 * CRM-13725 - Two relationships of same type with same start and end date
669 * should be OK if the custom field values differ. In this case, the
670 * existing relationship does have custom values, but the new one
671 * does not.
2d932085 672 * FIXME: Api4
a12d81e1
JV
673 */
674 public function testRelationshipCreateDuplicateWithCustomFields3() {
119664d6 675 $this->createCustomGroupWithFieldsOfAllTypes();
676
677 $custom_params_1 = [
678 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
679 $this->getCustomFieldName('select_string') => 'Y',
680 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
681 $this->getCustomFieldName('link') => 'http://example.org',
682 ];
a12d81e1 683
9099cab3 684 $params_2 = [
a12d81e1
JV
685 'contact_id_a' => $this->_cId_a,
686 'contact_id_b' => $this->_cId_b,
687 'relationship_type_id' => $this->_relTypeID,
688 'start_date' => '2008-12-20',
689 'is_active' => 1,
9099cab3 690 ];
a12d81e1
JV
691
692 $params_1 = array_merge($params_2, $custom_params_1);
693
694 $this->callAPISuccess('relationship', 'create', $params_1);
695 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
696
697 $this->assertNotNull($result_2['id']);
698 $this->assertEquals(0, $result_2['is_error']);
699
700 $this->relationshipTypeDelete($this->_relTypeID);
701 }
702
6a488035 703 /**
100fef9d 704 * Check with valid params array.
6a488035 705 */
00be9182 706 public function testRelationshipsGet() {
9099cab3 707 $relParams = [
6a488035
TO
708 'contact_id_a' => $this->_cId_a,
709 'contact_id_b' => $this->_cId_b,
710 'relationship_type_id' => $this->_relTypeID,
711 'start_date' => '2011-01-01',
712 'end_date' => '2013-01-01',
713 'is_active' => 1,
9099cab3 714 ];
6a488035 715
b85df283 716 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
717
718 //get relationship
9099cab3 719 $params = [
6a488035 720 'contact_id' => $this->_cId_b,
9099cab3 721 ];
26dcc9eb 722 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 723 $this->assertEquals($result['count'], 1);
9099cab3 724 $params = [
6a488035 725 'contact_id_a' => $this->_cId_a,
9099cab3 726 ];
26dcc9eb 727 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 728 $this->assertEquals($result['count'], 1);
6a488035 729 // contact_id_a is wrong so should be no matches
9099cab3 730 $params = [
6a488035 731 'contact_id_a' => $this->_cId_b,
9099cab3 732 ];
26dcc9eb 733 $result = $this->callAPISuccess('relationship', 'get', $params);
75638074 734 $this->assertEquals($result['count'], 0);
6a488035
TO
735 }
736
d6238393
JV
737 /**
738 * Chain Relationship.get and to Contact.get.
2d932085
CW
739 * @param int $version
740 * @dataProvider versionThreeAndFour
d6238393 741 */
2d932085
CW
742 public function testRelationshipGetWithChainedCall($version) {
743 $this->_apiversion = $version;
d6238393 744 // Create a relationship.
a457852d 745 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params);
d6238393
JV
746 $id = $createResult['id'];
747
748 // Try to retrieve it using chaining.
9099cab3 749 $params = [
d6238393
JV
750 'relationship_type_id' => $this->_relTypeID,
751 'id' => $id,
9099cab3 752 'api.Contact.get' => [
d6238393 753 'id' => '$value.contact_id_b',
9099cab3
CW
754 ],
755 ];
d6238393
JV
756
757 $result = $this->callAPISuccess('relationship', 'get', $params);
758
759 $this->assertEquals(1, $result['count']);
760 $relationship = CRM_Utils_Array::first($result['values']);
761 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
762 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
763 $this->assertEquals($this->_cId_b, $contact['id']);
764 }
765
766 /**
767 * Chain Contact.get to Relationship.get and again to Contact.get.
2d932085
CW
768 * @param int $version
769 * @dataProvider versionThreeAndFour
d6238393 770 */
2d932085
CW
771 public function testRelationshipGetInChainedCall($version) {
772 $this->_apiversion = $version;
d6238393 773 // Create a relationship.
a457852d 774 $this->callAPISuccess('relationship', 'create', $this->_params);
d6238393
JV
775
776 // Try to retrieve it using chaining.
9099cab3 777 $params = [
d6238393 778 'id' => $this->_cId_a,
9099cab3 779 'api.Relationship.get' => [
d6238393
JV
780 'relationship_type_id' => $this->_relTypeID,
781 'contact_id_a' => '$value.id',
9099cab3 782 'api.Contact.get' => [
d6238393 783 'id' => '$value.contact_id_b',
9099cab3
CW
784 ],
785 ],
786 ];
d6238393
JV
787
788 $result = $this->callAPISuccess('contact', 'get', $params);
789 $this->assertEquals(1, $result['count']);
790 $contact = CRM_Utils_Array::first($result['values']);
791 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
792 $relationship = CRM_Utils_Array::first($contact['api.Relationship.get']['values']);
793 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
794 $contact = CRM_Utils_Array::first($relationship['api.Contact.get']['values']);
795 $this->assertEquals($this->_cId_b, $contact['id']);
796 }
797
6a488035 798 /**
100fef9d 799 * Check with valid params array.
6a488035 800 * (The get function will behave differently without 'contact_id' passed
2d932085
CW
801 * @param int $version
802 * @dataProvider versionThreeAndFour
6a488035 803 */
2d932085
CW
804 public function testRelationshipsGetGeneric($version) {
805 $this->_apiversion = $version;
9099cab3 806 $relParams = [
6a488035
TO
807 'contact_id_a' => $this->_cId_a,
808 'contact_id_b' => $this->_cId_b,
809 'relationship_type_id' => $this->_relTypeID,
810 'start_date' => '2011-01-01',
811 'end_date' => '2013-01-01',
812 'is_active' => 1,
9099cab3 813 ];
6a488035 814
b85df283 815 $this->callAPISuccess('relationship', 'create', $relParams);
6a488035
TO
816
817 //get relationship
9099cab3 818 $params = [
6a488035 819 'contact_id_b' => $this->_cId_b,
9099cab3 820 ];
b85df283 821 $this->callAPISuccess('relationship', 'get', $params);
6a488035
TO
822 }
823
b85df283
EM
824 /**
825 * Test retrieving only current relationships.
2d932085
CW
826 * @param int $version
827 * @dataProvider versionThreeAndFour
b85df283 828 */
2d932085
CW
829 public function testGetIsCurrent($version) {
830 $this->_apiversion = $version;
9099cab3 831 $rel2Params = [
6a488035
TO
832 'contact_id_a' => $this->_cId_a,
833 'contact_id_b' => $this->_cId_b2,
834 'relationship_type_id' => $this->_relTypeID,
835 'start_date' => '2008-12-20',
836 'is_active' => 0,
9099cab3 837 ];
2d932085 838 $rel0 = $this->callAPISuccess('relationship', 'create', $rel2Params);
26dcc9eb 839 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
840
2d932085 841 $getParams = ['filters' => ['is_current' => 1]];
5c49fee0 842 $description = "Demonstrates is_current filter.";
6a488035 843 $subfile = 'filterIsCurrent';
26dcc9eb 844 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
845 $this->assertEquals($result['count'], 1);
846 $this->AssertEquals($rel1['id'], $result['id']);
847
848 // now try not started
6c6e6187
TO
849 $rel2Params['is_active'] = 1;
850 $rel2Params['start_date'] = 'tomorrow';
2d932085 851 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
6a488035
TO
852
853 // now try finished
6c6e6187
TO
854 $rel2Params['start_date'] = 'last week';
855 $rel2Params['end_date'] = 'yesterday';
2d932085
CW
856 $rel3 = $this->callAPISuccess('relationship', 'create', $rel2Params);
857
858 $result = $this->callAPISuccess('relationship', 'get', $getParams);
859 $this->assertEquals($result['count'], 1);
860 $this->AssertEquals($rel1['id'], $result['id']);
861
862 foreach ([$rel0, $rel1, $rel2, $rel3] as $rel) {
863 $this->callAPISuccess('Relationship', 'delete', $rel);
864 }
6a488035 865 }
5896d037 866
408b79bf 867 /**
eceb18cc 868 * Test using various operators.
2d932085
CW
869 * @param int $version
870 * @dataProvider versionThreeAndFour
6a488035 871 */
2d932085
CW
872 public function testGetTypeOperators($version) {
873 $this->_apiversion = $version;
9099cab3 874 $relTypeParams = [
5896d037
TO
875 'name_a_b' => 'Relation 3 for delete',
876 'name_b_a' => 'Relation 6 for delete',
877 'description' => 'Testing relationship type 2',
878 'contact_type_a' => 'Individual',
879 'contact_type_b' => 'Organization',
880 'is_reserved' => 1,
881 'is_active' => 1,
9099cab3 882 ];
6a488035 883 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
9099cab3 884 $relTypeParams = [
5896d037
TO
885 'name_a_b' => 'Relation 8 for delete',
886 'name_b_a' => 'Relation 9 for delete',
887 'description' => 'Testing relationship type 7',
888 'contact_type_a' => 'Individual',
889 'contact_type_b' => 'Organization',
890 'is_reserved' => 1,
891 'is_active' => 1,
9099cab3 892 ];
6a488035
TO
893 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
894
9099cab3 895 $relTypeParams = [
5896d037
TO
896 'name_a_b' => 'Relation 6 for delete',
897 'name_b_a' => 'Relation 88for delete',
898 'description' => 'Testing relationship type 00',
899 'contact_type_a' => 'Individual',
900 'contact_type_b' => 'Organization',
901 'is_reserved' => 1,
902 'is_active' => 1,
9099cab3 903 ];
6a488035
TO
904 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
905
26dcc9eb 906 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params);
907 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
9099cab3 908 ['relationship_type_id' => $relationType2]));
26dcc9eb 909 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
9099cab3 910 ['relationship_type_id' => $relationType3]));
26dcc9eb 911 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params,
9099cab3 912 ['relationship_type_id' => $relationType4]));
6a488035 913
9099cab3
CW
914 $getParams = [
915 'relationship_type_id' => ['IN' => [$relationType2, $relationType3]],
916 ];
6a488035 917
5c49fee0 918 $description = "Demonstrates use of IN filter.";
6a488035
TO
919 $subfile = 'INRelationshipType';
920
26dcc9eb 921 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 922 $this->assertEquals($result['count'], 2);
9099cab3 923 $this->AssertEquals([$rel2['id'], $rel3['id']], array_keys($result['values']));
6a488035 924
5c49fee0 925 $description = "Demonstrates use of NOT IN filter.";
6a488035 926 $subfile = 'NotInRelationshipType';
9099cab3
CW
927 $getParams = [
928 'relationship_type_id' => ['NOT IN' => [$relationType2, $relationType3]],
929 ];
26dcc9eb 930 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 931 $this->assertEquals($result['count'], 2);
58bcd8be 932 $this->assertEquals([$rel1['id'], $rel4['id']], array_keys($result['values']));
6a488035 933
58bcd8be 934 $description = 'Demonstrates use of BETWEEN filter.';
6a488035 935 $subfile = 'BetweenRelationshipType';
9099cab3
CW
936 $getParams = [
937 'relationship_type_id' => ['BETWEEN' => [$relationType2, $relationType4]],
938 ];
26dcc9eb 939 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 940 $this->assertEquals($result['count'], 3);
9099cab3 941 $this->AssertEquals([$rel2['id'], $rel3['id'], $rel4['id']], array_keys($result['values']));
6a488035 942
58bcd8be 943 $description = 'Demonstrates use of Not BETWEEN filter.';
6a488035 944 $subfile = 'NotBetweenRelationshipType';
9099cab3
CW
945 $getParams = [
946 'relationship_type_id' => ['NOT BETWEEN' => [$relationType2, $relationType4]],
947 ];
26dcc9eb 948 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 949 $this->assertEquals($result['count'], 1);
58bcd8be 950 $this->assertEquals([$rel1['id']], array_keys($result['values']));
6a488035 951
2d932085
CW
952 foreach ([$relationType2, $relationType3, $relationType4] as $id) {
953 $this->callAPISuccess('RelationshipType', 'delete', ['id' => $id]);
954 }
6a488035 955 }
5896d037 956
6a488035 957 /**
eceb18cc 958 * Check with invalid relationshipType Id.
6a488035 959 */
00be9182 960 public function testRelationshipTypeAddInvalidId() {
9099cab3 961 $relTypeParams = [
6a488035
TO
962 'id' => 'invalid',
963 'name_a_b' => 'Relation 1 for delete',
964 'name_b_a' => 'Relation 2 for delete',
965 'contact_type_a' => 'Individual',
966 'contact_type_b' => 'Organization',
9099cab3 967 ];
b85df283 968 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
75638074 969 'id is not a valid integer');
6a488035
TO
970 }
971
6a488035 972 /**
fe482240 973 * Check with valid data with contact_b.
6a488035 974 */
00be9182 975 public function testGetRelationshipWithContactB() {
9099cab3 976 $relParams = [
6a488035
TO
977 'contact_id_a' => $this->_cId_a,
978 'contact_id_b' => $this->_cId_b,
979 'relationship_type_id' => $this->_relTypeID,
980 'start_date' => '2011-01-01',
981 'end_date' => '2013-01-01',
982 'is_active' => 1,
9099cab3 983 ];
6a488035 984
26dcc9eb 985 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 986
9099cab3 987 $contacts = [
6a488035 988 'contact_id' => $this->_cId_a,
9099cab3 989 ];
6a488035 990
26dcc9eb 991 $result = $this->callAPISuccess('relationship', 'get', $contacts);
75638074 992 $this->assertGreaterThan(0, $result['count']);
9099cab3 993 $params = [
6a488035 994 'id' => $relationship['id'],
9099cab3 995 ];
7c8ae32a 996 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
997 $this->relationshipTypeDelete($this->_relTypeID);
998 }
999
1000 /**
eceb18cc 1001 * Check with valid data with relationshipTypes.
6a488035 1002 */
00be9182 1003 public function testGetRelationshipWithRelTypes() {
9099cab3 1004 $relParams = [
6a488035
TO
1005 'contact_id_a' => $this->_cId_a,
1006 'contact_id_b' => $this->_cId_b,
1007 'relationship_type_id' => $this->_relTypeID,
1008 'start_date' => '2011-01-01',
1009 'end_date' => '2013-01-01',
1010 'is_active' => 1,
9099cab3 1011 ];
6a488035 1012
26dcc9eb 1013 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
6a488035 1014
9099cab3 1015 $contact_a = [
6a488035 1016 'contact_id' => $this->_cId_a,
9099cab3 1017 ];
b85df283 1018 $this->callAPISuccess('relationship', 'get', $contact_a);
6a488035 1019
9099cab3 1020 $params = [
6a488035 1021 'id' => $relationship['id'],
9099cab3 1022 ];
b85df283 1023 $this->callAPISuccess('relationship', 'delete', $params);
6a488035
TO
1024 $this->relationshipTypeDelete($this->_relTypeID);
1025 }
75638074 1026
1027 /**
1028 * Checks that passing in 'contact_id' + a relationship type
1029 * will filter by relationship type (relationships go in both directions)
1030 * as relationship api does a reciprocal check if contact_id provided
1031 *
1032 * We should get 1 result without or with correct relationship type id & 0 with
1033 * an incorrect one
1034 */
00be9182 1035 public function testGetRelationshipByTypeReciprocal() {
8ba5884d 1036 $created = $this->callAPISuccess($this->entity, 'create', $this->_params);
9099cab3 1037 $result = $this->callAPISuccess($this->entity, 'get', [
75638074 1038 'contact_id' => $this->_cId_a,
1039 'relationship_type_id' => $this->_relTypeID,
9099cab3 1040 ]);
75638074 1041 $this->assertEquals(1, $result['count']);
9099cab3 1042 $result = $this->callAPISuccess($this->entity, 'get', [
75638074 1043 'contact_id' => $this->_cId_a,
1044 'relationship_type_id' => $this->_relTypeID + 1,
9099cab3 1045 ]);
75638074 1046 $this->assertEquals(0, $result['count']);
9099cab3 1047 $this->callAPISuccess($this->entity, 'delete', ['id' => $created['id']]);
75638074 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
2d932085
CW
1056 * @param int $version
1057 * @dataProvider versionThreeAndFour
75638074 1058 */
2d932085
CW
1059 public function testGetRelationshipByTypeDAO($version) {
1060 $this->_apiversion = $version;
9099cab3 1061 $this->_ids['relationship'] = $this->callAPISuccess($this->entity, 'create', ['format.only_id' => TRUE] +
b85df283 1062 $this->_params);
9099cab3 1063 $this->callAPISuccess($this->entity, 'getcount', [
39b959db 1064 'contact_id_a' => $this->_cId_a,
9099cab3
CW
1065 ], 1);
1066 $result = $this->callAPISuccess($this->entity, 'get', [
75638074 1067 'contact_id_a' => $this->_cId_a,
1068 'relationship_type_id' => $this->_relTypeID,
9099cab3 1069 ]);
75638074 1070 $this->assertEquals(1, $result['count']);
9099cab3 1071 $result = $this->callAPISuccess($this->entity, 'get', [
75638074 1072 'contact_id_a' => $this->_cId_a,
1073 'relationship_type_id' => $this->_relTypeID + 1,
9099cab3 1074 ]);
75638074 1075 $this->assertEquals(0, $result['count']);
1076 }
1077
1078 /**
1079 * Checks that passing in 'contact_id_b' + a relationship type
1080 * will filter by relationship type for contact b
1081 *
1082 * We should get 1 result without or with correct relationship type id & 0 with
1083 * an incorrect one
2d932085
CW
1084 * @param int $version
1085 * @dataProvider versionThreeAndFour
75638074 1086 */
2d932085
CW
1087 public function testGetRelationshipByTypeArrayDAO($version) {
1088 $this->_apiversion = $version;
8ba5884d 1089 $this->callAPISuccess($this->entity, 'create', $this->_params);
75638074 1090 $org3 = $this->organizationCreate();
39b959db
SL
1091 // lets just assume built in ones aren't being messed with!
1092 $relType2 = 5;
1093 // lets just assume built in ones aren't being messed with!
1094 $relType3 = 6;
75638074 1095
b85df283 1096 // Relationship 2.
8ba5884d 1097 $this->callAPISuccess($this->entity, 'create',
9099cab3 1098 array_merge($this->_params, [
75638074 1099 'relationship_type_id' => $relType2,
21dfd5f5 1100 'contact_id_b' => $this->_cId_b2,
9099cab3 1101 ])
75638074 1102 );
1103
b85df283 1104 // Relationship 3.
8ba5884d 1105 $this->callAPISuccess($this->entity, 'create',
9099cab3 1106 array_merge($this->_params, [
75638074 1107 'relationship_type_id' => $relType3,
21dfd5f5 1108 'contact_id_b' => $org3,
9099cab3 1109 ])
75638074 1110 );
1111
9099cab3 1112 $result = $this->callAPISuccess($this->entity, 'get', [
75638074 1113 'contact_id_a' => $this->_cId_a,
9099cab3
CW
1114 'relationship_type_id' => ['IN' => [$this->_relTypeID, $relType3]],
1115 ]);
75638074 1116
1117 $this->assertEquals(2, $result['count']);
1118 foreach ($result['values'] as $key => $value) {
9099cab3 1119 $this->assertTrue(in_array($value['relationship_type_id'], [$this->_relTypeID, $relType3]));
75638074 1120 }
1121 }
ac8aaa49 1122
1123 /**
1124 * Checks that passing in 'contact_id_b' + a relationship type
1125 * will filter by relationship type for contact b
1126 *
1127 * We should get 1 result without or with correct relationship type id & 0 with
1128 * an incorrect one
1129 */
00be9182 1130 public function testGetRelationshipByTypeArrayReciprocal() {
8ba5884d 1131 $this->callAPISuccess($this->entity, 'create', $this->_params);
ac8aaa49 1132 $org3 = $this->organizationCreate();
b85df283
EM
1133 // lets just assume built in ones aren't being messed with!
1134 $relType2 = 5;
1135 $relType3 = 6;
ac8aaa49 1136
b85df283 1137 // Relationship 2.
8ba5884d 1138 $this->callAPISuccess($this->entity, 'create',
9099cab3 1139 array_merge($this->_params, [
ac8aaa49 1140 'relationship_type_id' => $relType2,
21dfd5f5 1141 'contact_id_b' => $this->_cId_b2,
9099cab3 1142 ])
ac8aaa49 1143 );
1144
b85df283 1145 // Relationship 3.
8ba5884d 1146 $this->callAPISuccess($this->entity, 'create',
9099cab3 1147 array_merge($this->_params, [
ac8aaa49 1148 'relationship_type_id' => $relType3,
21dfd5f5 1149 'contact_id_b' => $org3,
9099cab3 1150 ])
ac8aaa49 1151 );
1152
9099cab3 1153 $result = $this->callAPISuccess($this->entity, 'get', [
ac8aaa49 1154 'contact_id' => $this->_cId_a,
9099cab3
CW
1155 'relationship_type_id' => ['IN' => [$this->_relTypeID, $relType3]],
1156 ]);
ac8aaa49 1157
1158 $this->assertEquals(2, $result['count']);
1159 foreach ($result['values'] as $key => $value) {
9099cab3 1160 $this->assertTrue(in_array($value['relationship_type_id'], [$this->_relTypeID, $relType3]));
ac8aaa49 1161 }
1162 }
6a488035 1163
c9c41397 1164 /**
fe482240
EM
1165 * Test relationship get by membership type.
1166 *
c9c41397 1167 * Checks that passing in 'contact_id_b' + a relationship type
1168 * will filter by relationship type for contact b
1169 *
1170 * We should get 1 result without or with correct relationship type id & 0 with
1171 * an incorrect one
58bcd8be 1172 *
2d932085 1173 * @param int $version
58bcd8be 1174 *
2d932085 1175 * @dataProvider versionThreeAndFour
58bcd8be 1176 * @throws \CRM_Core_Exception
c9c41397 1177 */
2d932085
CW
1178 public function testGetRelationshipByMembershipTypeDAO($version) {
1179 $this->_apiversion = $version;
8ba5884d 1180 $this->callAPISuccess($this->entity, 'create', $this->_params);
c9c41397 1181 $org3 = $this->organizationCreate();
1182
39b959db
SL
1183 // lets just assume built in ones aren't being messed with!
1184 $relType2 = 5;
1185 // lets just assume built in ones aren't being messed with!
1186 $relType3 = 6;
c9c41397 1187 $relType1 = 1;
9099cab3 1188 $memberType = $this->membershipTypeCreate([
e306f08e
CW
1189 'relationship_type_id' => [$relType1, $relType3],
1190 'relationship_direction' => ['a_b', 'b_a'],
9099cab3 1191 ]);
c9c41397 1192
b85df283 1193 // Relationship 2.
8ba5884d 1194 $this->callAPISuccess($this->entity, 'create',
9099cab3 1195 array_merge($this->_params, [
c9c41397 1196 'relationship_type_id' => $relType2,
21dfd5f5 1197 'contact_id_b' => $this->_cId_b2,
9099cab3 1198 ])
c9c41397 1199 );
1200
b85df283 1201 // Relationship 3.
8ba5884d 1202 $this->callAPISuccess($this->entity, 'create',
9099cab3 1203 array_merge($this->_params, [
c9c41397 1204 'relationship_type_id' => $relType3,
21dfd5f5 1205 'contact_id_b' => $org3,
9099cab3 1206 ])
c9c41397 1207 );
1208
b85df283 1209 // Relationship 4 with reversal.
8ba5884d 1210 $this->callAPISuccess($this->entity, 'create',
9099cab3 1211 array_merge($this->_params, [
c9c41397 1212 'relationship_type_id' => $relType1,
1213 'contact_id_a' => $this->_cId_a,
21dfd5f5 1214 'contact_id_b' => $this->_cId_a_2,
9099cab3 1215 ])
c9c41397 1216 );
1217
9099cab3 1218 $result = $this->callAPISuccess($this->entity, 'get', [
c9c41397 1219 'contact_id_a' => $this->_cId_a,
1220 'membership_type_id' => $memberType,
58bcd8be 1221 // Pass version here as there is no intention to replicate support for passing in membership_type_id
1222 'version' => 3,
9099cab3 1223 ]);
c9c41397 1224 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1225 $this->assertEquals(1, $result['count']);
1226 foreach ($result['values'] as $key => $value) {
9099cab3 1227 $this->assertTrue(in_array($value['relationship_type_id'], [$relType1]));
c9c41397 1228 }
1229 }
1230
1231 /**
1232 * Checks that passing in 'contact_id_b' + a relationship type
1233 * will filter by relationship type for contact b
1234 *
1235 * We should get 1 result without or with correct relationship type id & 0 with
1236 * an incorrect one
58bcd8be 1237 *
2d932085 1238 * @param int $version
58bcd8be 1239 *
2d932085 1240 * @dataProvider versionThreeAndFour
58bcd8be 1241 * @throws \CRM_Core_Exception
c9c41397 1242 */
2d932085
CW
1243 public function testGetRelationshipByMembershipTypeReciprocal($version) {
1244 $this->_apiversion = $version;
8ba5884d 1245 $this->callAPISuccess($this->entity, 'create', $this->_params);
c9c41397 1246 $org3 = $this->organizationCreate();
1247
b85df283
EM
1248 // Let's just assume built in ones aren't being messed with!
1249 $relType2 = 5;
1250 $relType3 = 6;
c9c41397 1251 $relType1 = 1;
9099cab3 1252 $memberType = $this->membershipTypeCreate([
e306f08e
CW
1253 'relationship_type_id' => [$relType1, $relType3],
1254 'relationship_direction' => ['a_b', 'b_a'],
9099cab3 1255 ]);
c9c41397 1256
b85df283 1257 // Relationship 2.
8ba5884d 1258 $this->callAPISuccess($this->entity, 'create',
9099cab3 1259 array_merge($this->_params, [
c9c41397 1260 'relationship_type_id' => $relType2,
21dfd5f5 1261 'contact_id_b' => $this->_cId_b2,
9099cab3 1262 ])
c9c41397 1263 );
1264
b85df283 1265 // Relationship 4.
8ba5884d 1266 $this->callAPISuccess($this->entity, 'create',
9099cab3 1267 array_merge($this->_params, [
c9c41397 1268 'relationship_type_id' => $relType3,
21dfd5f5 1269 'contact_id_b' => $org3,
9099cab3 1270 ])
c9c41397 1271 );
1272
b85df283 1273 // Relationship 4 with reversal.
8ba5884d 1274 $this->callAPISuccess($this->entity, 'create',
9099cab3 1275 array_merge($this->_params, [
c9c41397 1276 'relationship_type_id' => $relType1,
1277 'contact_id_a' => $this->_cId_a,
21dfd5f5 1278 'contact_id_b' => $this->_cId_a_2,
9099cab3 1279 ])
c9c41397 1280 );
1281
9099cab3 1282 $result = $this->callAPISuccess($this->entity, 'get', [
c9c41397 1283 'contact_id' => $this->_cId_a,
1284 'membership_type_id' => $memberType,
58bcd8be 1285 // There is no intention to replicate support for passing in membership_type_id
1286 // in apiv4 so pass version as 3
1287 'version' => 3,
9099cab3 1288 ]);
b85df283 1289 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
c9c41397 1290 $this->assertEquals(2, $result['count']);
1291
1292 foreach ($result['values'] as $key => $value) {
9099cab3 1293 $this->assertTrue(in_array($value['relationship_type_id'], [$relType1, $relType3]));
c9c41397 1294 }
1295 }
a7361ba3
EM
1296
1297 /**
b85df283 1298 * Check for e-notices on enable & disable as reported in CRM-14350
310c2031 1299 *
2d932085 1300 * @param int $version
310c2031 1301 *
2d932085 1302 * @dataProvider versionThreeAndFour
310c2031 1303 *
1304 * @throws \CRM_Core_Exception
a7361ba3 1305 */
2d932085
CW
1306 public function testSetActive($version) {
1307 $this->_apiversion = $version;
8ba5884d 1308 $relationship = $this->callAPISuccess($this->entity, 'create', $this->_params);
9099cab3
CW
1309 $this->callAPISuccess($this->entity, 'create', ['id' => $relationship['id'], 'is_active' => 0]);
1310 $this->callAPISuccess($this->entity, 'create', ['id' => $relationship['id'], 'is_active' => 1]);
a7361ba3 1311 }
96025800 1312
7a44a255
EM
1313 /**
1314 * Test creating related memberships.
310c2031 1315 *
2d932085 1316 * @param int $version
310c2031 1317 *
2d932085 1318 * @dataProvider versionThreeAndFour
310c2031 1319 *
1320 * @throws \CRM_Core_Exception
7a44a255 1321 */
2d932085
CW
1322 public function testCreateRelatedMembership($version) {
1323 $this->_apiversion = $version;
9099cab3 1324 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', [
7a44a255
EM
1325 'name' => 'Membership with Related',
1326 'member_of_contact_id' => 1,
1327 'financial_type_id' => 1,
1328 'minimum_fee' => 0.00,
1329 'duration_unit' => 'year',
1330 'duration_interval' => 1,
1331 'period_type' => 'rolling',
1332 'relationship_type_id' => $this->_relTypeID,
1333 'relationship_direction' => 'b_a',
1334 'visibility' => 'Public',
1335 'auto_renew' => 0,
1336 'is_active' => 1,
1337 'domain_id' => CRM_Core_Config::domainID(),
9099cab3
CW
1338 ]);
1339 $originalMembership = $this->callAPISuccess('Membership', 'create', [
7a44a255
EM
1340 'membership_type_id' => $relatedMembershipType['id'],
1341 'contact_id' => $this->_cId_b,
9099cab3
CW
1342 ]);
1343 $this->callAPISuccess('Relationship', 'create', [
7a44a255
EM
1344 'relationship_type_id' => $this->_relTypeID,
1345 'contact_id_a' => $this->_cId_a,
1346 'contact_id_b' => $this->_cId_b,
9099cab3
CW
1347 ]);
1348 $contactAMembership = $this->callAPISuccessGetSingle('membership', ['contact_id' => $this->_cId_a]);
7a44a255 1349 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
45089d88
CW
1350
1351 // Adding a relationship with a future start date should NOT create a membership
9099cab3 1352 $this->callAPISuccess('Relationship', 'create', [
45089d88
CW
1353 'relationship_type_id' => $this->_relTypeID,
1354 'contact_id_a' => $this->_cId_a_2,
1355 'contact_id_b' => $this->_cId_b,
1356 'start_date' => 'now + 1 week',
9099cab3
CW
1357 ]);
1358 $this->callAPISuccessGetCount('membership', ['contact_id' => $this->_cId_a_2], 0);
45089d88 1359
62118c6d 1360 // Deleting the organization should cause the related membership to be deleted.
9099cab3
CW
1361 $this->callAPISuccess('contact', 'delete', ['id' => $this->_cId_b]);
1362 $this->callAPISuccessGetCount('membership', ['contact_id' => $this->_cId_a], 0);
7a44a255
EM
1363 }
1364
f42bcfb1
MD
1365 /**
1366 * Test api respects is_current_employer.
1367 *
1368 * @throws \CRM_Core_Exception
1369 */
1370 public function testRelationshipCreateWithEmployerData() {
1371 // CASE A: Create a current employee relationship without setting end date, ensure that employer field is set
1372 $params = [
1373 'relationship_type_id' => '5_a_b',
1374 'related_contact_id' => $this->_cId_b,
1375 'start_date' => '2008-12-20',
1376 'end_date' => NULL,
1377 'is_active' => 1,
1378 'is_current_employer' => 1,
1379 'is_permission_a_b' => 0,
1380 'is_permission_b_a' => 0,
1381 ];
1382 $reln = new CRM_Contact_Form_Relationship();
1383 $reln->_action = CRM_Core_Action::ADD;
1384 $reln->_contactId = $this->_cId_a;
1385 list ($params, $relationshipIds) = $reln->submit($params);
1386 $this->assertEquals(
1387 $this->_cId_b,
1388 $this->callAPISuccess('Contact', 'getvalue', [
1389 'id' => $this->_cId_a,
1390 'return' => 'current_employer_id',
1391 ]));
1392 // CASE B: Create a past employee relationship by setting end date of past, ensure that employer field is cleared
1393 $params = [
1394 'relationship_type_id' => '5_a_b',
1395 'related_contact_id' => $this->_cId_b,
1396 // set date to past date
1397 'end_date' => '2010-12-20',
1398 ];
1399 $reln->_action = CRM_Core_Action::UPDATE;
1400 $reln->_relationshipId = $relationshipIds[0];
1401 list ($params, $relationshipIds) = $reln->submit($params);
1402 $this->assertEmpty($this->callAPISuccess('Contact', 'getvalue', [
1403 'id' => $this->_cId_a,
1404 'return' => 'current_employer_id',
1405 ]));
1406 $this->callAPISuccess('relationship', 'delete', ['id' => $relationshipIds[0]]);
1407 }
1408
0c578c02
MD
1409 /**
1410 * Test disabling an expired relationship does not incorrectly clear employer_id.
1411 *
1412 * See https://lab.civicrm.org/dev/core/issues/470
1413 *
1414 * @throws \CRM_Core_Exception
1415 * @throws \CiviCRM_API3_Exception
1416 */
1417 public function testDisableExpiredRelationships() {
1418 // Step 1: Create a current employer relationship with Org A
1419 $params = [
1420 'relationship_type_id' => '5',
1421 'contact_id_a' => $this->_cId_a,
1422 'contact_id_b' => $this->_cId_b,
1423 'start_date' => '2008-12-20',
1424 'end_date' => NULL,
1425 'is_active' => 1,
1426 'is_current_employer' => 1,
1427 'is_permission_a_b' => 0,
1428 'is_permission_b_a' => 0,
1429 ];
1430 $this->callAPISuccess('Relationship', 'create', $params);
1431
1432 // ensure that the employer_id field is sucessfully set
1433 $this->assertEquals(
1434 $this->_cId_b,
1435 $this->callAPISuccess('Contact', 'getvalue', [
1436 'id' => $this->_cId_a,
1437 'return' => 'current_employer_id',
1438 ]));
1439 // Step 2: Create a PAST employer relationship with Org B, and setting is_current_employer = FALSE
1440 $orgID2 = $this->organizationCreate();
1441 $params = [
1442 'relationship_type_id' => '5',
1443 'contact_id_a' => $this->_cId_a,
1444 'contact_id_b' => $orgID2,
1445 'start_date' => '2008-12-20',
1446 'end_date' => '2008-12-22',
1447 'is_active' => 1,
1448 'is_current_employer' => 0,
1449 'is_permission_a_b' => 0,
1450 'is_permission_b_a' => 0,
1451 ];
1452
1453 $relationshipB = $this->callAPISuccess('Relationship', 'create', $params);
1454 // ensure that the employer_id field is still set to contact b
1455 $this->assertEquals(
1456 $this->_cId_b,
1457 $this->callAPISuccess('Contact', 'getvalue', [
1458 'id' => $this->_cId_a,
1459 'return' => 'current_employer_id',
1460 ]));
1461
1462 // Step 3: Call schedule job disable_expired_relationships
1463 CRM_Contact_BAO_Relationship::disableExpiredRelationships();
1464
1465 // Result A: Ensure that employer field is not cleared
1466 $this->assertEquals(
1467 $this->_cId_b,
1468 $this->callAPISuccess('Contact', 'getvalue', [
1469 'id' => $this->_cId_a,
1470 'return' => 'current_employer_id',
1471 ]));
1472 // Result B: Ensure that the previous employer relationship with Org B is successfully disabled
1473 $this->assertEquals(
1474 FALSE,
1475 (bool) $this->callAPISuccess('Relationship', 'getvalue', [
1476 'id' => $relationshipB['id'],
1477 'return' => 'is_active',
1478 ]));
1479 }
1480
c9c41397 1481}