3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_RelationshipCache
18 class CRM_Contact_BAO_RelationshipCacheTest
extends CiviUnitTestCase
{
20 protected function setUp(): void
{
21 $this->useTransaction(TRUE);
26 * Whenever one `Relationship` is created, there should be two corresponding
27 * `RelationshipCache` records.
29 public function testRelationshipCache() {
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',
39 $contact_id_1 = $this->individualCreate();
40 $contact_id_2 = $this->individualCreate([], 1);
42 // create new relationship (using BAO)
44 'relationship_type_id' => $relationship_type_id_1,
45 'contact_id_a' => $contact_id_1,
46 'contact_id_b' => $contact_id_2,
48 $relationshipObj = CRM_Contact_BAO_Relationship
::add($params);
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', [
52 1 => [$relationshipObj->id
, 'Positive'],
55 // There should be two records - the a_b record and the b_a record.
56 $this->assertCount(2, $caches);
57 $idx = CRM_Utils_Array
::index(['orientation'], $caches);
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']);
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']);