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