Merge pull request #23309 from civicrm/5.49
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / RelationshipTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 namespace api\v4\Entity;
20
21 use Civi\Api4\Contact;
22 use api\v4\UnitTestCase;
23 use Civi\Api4\Relationship;
24 use Civi\Api4\RelationshipCache;
25 use Civi\Test\TransactionalInterface;
26
27 /**
28 * Assert that interchanging data between APIv3 and APIv4 yields consistent
29 * encodings.
30 *
31 * @group headless
32 */
33 class RelationshipTest extends UnitTestCase implements TransactionalInterface {
34
35 public function testRelCacheCount() {
36 $c1 = Contact::create(FALSE)->addValue('first_name', '1')->execute()->first()['id'];
37 $c2 = Contact::create(FALSE)->addValue('first_name', '2')->execute()->first()['id'];
38 Relationship::create(FALSE)
39 ->setValues([
40 'contact_id_a' => $c1,
41 'contact_id_b' => $c2,
42 'relationship_type_id' => 1,
43 ])->execute();
44 $cacheRecords = RelationshipCache::get(FALSE)
45 ->addClause('OR', ['near_contact_id', '=', $c1], ['far_contact_id', '=', $c1])
46 ->execute();
47 $this->assertCount(2, $cacheRecords);
48 }
49
50 public function testRelCacheCalcFields() {
51 $c1 = Contact::create(FALSE)->addValue('first_name', '1')->execute()->first()['id'];
52 $c2 = Contact::create(FALSE)->addValue('first_name', '2')->execute()->first()['id'];
53 $relationship = Relationship::create(FALSE)
54 ->setValues([
55 'contact_id_a' => $c1,
56 'contact_id_b' => $c2,
57 'relationship_type_id' => 1,
58 'description' => "Wow, we're related!",
59 'is_permission_a_b' => 1,
60 'is_permission_b_a' => 2,
61 ])->execute()->first();
62 $relationship = Relationship::get(FALSE)
63 ->addWhere('id', '=', $relationship['id'])
64 ->execute()->first();
65 $cacheRecords = RelationshipCache::get(FALSE)
66 ->addWhere('near_contact_id', 'IN', [$c1, $c2])
67 ->addSelect('near_contact_id', 'orientation', 'description', 'relationship_created_date', 'relationship_modified_date', 'permission_near_to_far', 'permission_far_to_near')
68 ->execute()->indexBy('near_contact_id');
69 $this->assertCount(2, $cacheRecords);
70 $this->assertEquals("Wow, we're related!", $cacheRecords[$c1]['description']);
71 $this->assertEquals("Wow, we're related!", $cacheRecords[$c2]['description']);
72 $this->assertEquals(1, $cacheRecords[$c1]['permission_near_to_far']);
73 $this->assertEquals(2, $cacheRecords[$c2]['permission_near_to_far']);
74 $this->assertEquals(2, $cacheRecords[$c1]['permission_far_to_near']);
75 $this->assertEquals(1, $cacheRecords[$c2]['permission_far_to_near']);
76 $this->assertEquals($relationship['created_date'], $cacheRecords[$c1]['relationship_created_date']);
77 $this->assertEquals($relationship['modified_date'], $cacheRecords[$c2]['relationship_modified_date']);
78 }
79
80 }