Merge pull request #20741 from civicrm/5.39
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / RelationshipCacheTest.php
CommitLineData
8416d9e2
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12/**
bcf70e08 13 * Test class for CRM_Contact_BAO_RelationshipCache
8416d9e2
TO
14 *
15 * @package CiviCRM
16 * @group headless
17 */
fd1e4c76 18class CRM_Contact_BAO_RelationshipCacheTest extends CiviUnitTestCase {
8416d9e2 19
5d345864 20 protected function setUp(): void {
8416d9e2
TO
21 $this->useTransaction(TRUE);
22 parent::setUp();
23 }
24
25 /**
26 * Whenever one `Relationship` is created, there should be two corresponding
bcf70e08 27 * `RelationshipCache` records.
8416d9e2 28 */
bcf70e08 29 public function testRelationshipCache() {
8416d9e2
TO
30 // add a new type
31 $relationship_type_id_1 = $this->relationshipTypeCreate([
32 'name_a_b' => 'Praegustator is',
33 'name_b_a' => 'Praegustator for',
34 'contact_type_a' => 'Individual',
35 'contact_type_b' => 'Individual',
36 ]);
37
38 // add some people
39 $contact_id_1 = $this->individualCreate();
40 $contact_id_2 = $this->individualCreate([], 1);
41
42 // create new relationship (using BAO)
43 $params = [
44 'relationship_type_id' => $relationship_type_id_1,
45 'contact_id_a' => $contact_id_1,
46 'contact_id_b' => $contact_id_2,
47 ];
48 $relationshipObj = CRM_Contact_BAO_Relationship::add($params);
49
bcf70e08
TO
50 // Let's make sure the cache records were created!
51 $caches = CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_relationship_cache WHERE relationship_id = %1', [
8416d9e2
TO
52 1 => [$relationshipObj->id, 'Positive'],
53 ])->fetchAll();
54
55 // There should be two records - the a_b record and the b_a record.
bcf70e08
TO
56 $this->assertCount(2, $caches);
57 $idx = CRM_Utils_Array::index(['orientation'], $caches);
8416d9e2
TO
58
59 $this->assertEquals($relationship_type_id_1, $idx['a_b']['relationship_type_id']);
60 $this->assertEquals($contact_id_1, $idx['a_b']['near_contact_id']);
61 $this->assertEquals('Praegustator is', $idx['a_b']['near_relation']);
62 $this->assertEquals($contact_id_2, $idx['a_b']['far_contact_id']);
63 $this->assertEquals('Praegustator for', $idx['a_b']['far_relation']);
64
65 $this->assertEquals($relationship_type_id_1, $idx['b_a']['relationship_type_id']);
66 $this->assertEquals($contact_id_2, $idx['b_a']['near_contact_id']);
67 $this->assertEquals('Praegustator for', $idx['b_a']['near_relation']);
68 $this->assertEquals($contact_id_1, $idx['b_a']['far_contact_id']);
69 $this->assertEquals('Praegustator is', $idx['b_a']['far_relation']);
70 }
71
72}