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