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