4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Custom
;
22 use Civi\Api4\Contact
;
23 use Civi\Api4\CustomField
;
24 use Civi\Api4\CustomGroup
;
29 class CustomContactRefTest
extends CustomTestBase
{
31 public function testGetWithJoin() {
32 $firstName = uniqid('fav');
34 $customGroup = CustomGroup
::create(FALSE)
35 ->addValue('title', 'MyContactRef')
36 ->addValue('extends', 'Individual')
40 CustomField
::create(FALSE)
41 ->addValue('label', 'FavPerson')
42 ->addValue('custom_group_id', $customGroup['id'])
43 ->addValue('html_type', 'Autocomplete-Select')
44 ->addValue('data_type', 'ContactReference')
47 CustomField
::create(FALSE)
48 ->addValue('label', 'FavPeople')
49 ->addValue('custom_group_id', $customGroup['id'])
50 ->addValue('html_type', 'Autocomplete-Select')
51 ->addValue('data_type', 'ContactReference')
52 ->addValue('serialize', 1)
55 $favPersonId = $this->createTestRecord('Contact', [
56 'first_name' => $firstName,
57 'last_name' => 'Person',
58 'contact_type' => 'Individual',
61 $favPeopleId1 = $this->createTestRecord('Contact', [
62 'first_name' => 'FirstFav',
63 'last_name' => 'People1',
64 'contact_type' => 'Individual',
67 $favPeopleId2 = $this->createTestRecord('Contact', [
68 'first_name' => 'SecondFav',
69 'last_name' => 'People2',
70 'contact_type' => 'Individual',
73 $contactId1 = $this->createTestRecord('Contact', [
74 'first_name' => 'Mya',
75 'last_name' => 'Tester',
76 'contact_type' => 'Individual',
77 'MyContactRef.FavPerson' => $favPersonId,
78 'MyContactRef.FavPeople' => [$favPeopleId2, $favPeopleId1],
81 $contactId2 = $this->createTestRecord('Contact', [
82 'first_name' => 'Bea',
83 'last_name' => 'Tester',
84 'contact_type' => 'Individual',
85 'MyContactRef.FavPeople' => [$favPeopleId2],
88 $result = Contact
::get(FALSE)
89 ->addSelect('display_name')
90 ->addSelect('MyContactRef.FavPerson.first_name')
91 ->addSelect('MyContactRef.FavPerson.last_name')
92 ->addSelect('MyContactRef.FavPeople')
93 ->addSelect('MyContactRef.FavPeople.last_name')
94 ->addWhere('MyContactRef.FavPerson.first_name', '=', $firstName)
98 $this->assertEquals($firstName, $result['MyContactRef.FavPerson.first_name']);
99 $this->assertEquals('Person', $result['MyContactRef.FavPerson.last_name']);
100 // Ensure serialized values are returned in order
101 $this->assertEquals([$favPeopleId2, $favPeopleId1], $result['MyContactRef.FavPeople']);
102 // Values returned from virtual join should be in the same order
103 $this->assertEquals(['People2', 'People1'], $result['MyContactRef.FavPeople.last_name']);
105 $result = Contact
::get(FALSE)
107 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'First')
111 $this->assertEquals($contactId1, $result['id']);
113 $result = Contact
::get(FALSE)
115 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'Second')
118 $this->assertCount(2, $result);
121 public function testCurrentUser() {
122 $currentUser = $this->createLoggedInUser();
124 $customGroup = CustomGroup
::create(FALSE)
125 ->addValue('title', 'MyContactRef')
126 ->addValue('extends', 'Individual')
130 CustomField
::create(FALSE)
131 ->addValue('label', 'FavPerson')
132 ->addValue('custom_group_id', $customGroup['id'])
133 ->addValue('html_type', 'Autocomplete-Select')
134 ->addValue('data_type', 'ContactReference')
137 $contactId = $this->createTestRecord('Contact', [
138 'first_name' => 'Mya',
139 'last_name' => 'Tester',
140 'contact_type' => 'Individual',
141 'MyContactRef.FavPerson' => 'user_contact_id',
144 $contact = Contact
::get(FALSE)
145 ->addSelect('display_name')
146 ->addSelect('MyContactRef.FavPerson')
147 ->addWhere('id', '=', $contactId)
151 $this->assertEquals($currentUser, $contact['MyContactRef.FavPerson']);