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\Action
;
22 use Civi\Api4\Contact
;
23 use Civi\Api4\CustomField
;
24 use Civi\Api4\CustomGroup
;
29 class CustomContactRefTest
extends BaseCustomValueTest
{
31 public function testGetWithJoin() {
32 $firstName = uniqid('fav');
34 $customGroup = CustomGroup
::create(FALSE)
35 ->addValue('name', '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 = Contact
::create(FALSE)
56 ->addValue('first_name', $firstName)
57 ->addValue('last_name', 'Person')
58 ->addValue('contact_type', 'Individual')
62 $favPeopleId1 = Contact
::create(FALSE)
63 ->addValue('first_name', 'FirstFav')
64 ->addValue('last_name', 'People1')
65 ->addValue('contact_type', 'Individual')
69 $favPeopleId2 = Contact
::create(FALSE)
70 ->addValue('first_name', 'SecondFav')
71 ->addValue('last_name', 'People2')
72 ->addValue('contact_type', 'Individual')
76 $contactId1 = Contact
::create(FALSE)
77 ->addValue('first_name', 'Mya')
78 ->addValue('last_name', 'Tester')
79 ->addValue('contact_type', 'Individual')
80 ->addValue('MyContactRef.FavPerson', $favPersonId)
81 ->addValue('MyContactRef.FavPeople', [$favPeopleId2, $favPeopleId1])
85 $contactId2 = Contact
::create(FALSE)
86 ->addValue('first_name', 'Bea')
87 ->addValue('last_name', 'Tester')
88 ->addValue('contact_type', 'Individual')
89 ->addValue('MyContactRef.FavPeople', [$favPeopleId2])
93 $result = Contact
::get(FALSE)
94 ->addSelect('display_name')
95 ->addSelect('MyContactRef.FavPerson.first_name')
96 ->addSelect('MyContactRef.FavPerson.last_name')
97 ->addSelect('MyContactRef.FavPeople')
98 ->addSelect('MyContactRef.FavPeople.last_name')
99 ->addWhere('MyContactRef.FavPerson.first_name', '=', $firstName)
103 $this->assertEquals($firstName, $result['MyContactRef.FavPerson.first_name']);
104 $this->assertEquals('Person', $result['MyContactRef.FavPerson.last_name']);
105 // Ensure serialized values are returned in order
106 $this->assertEquals([$favPeopleId2, $favPeopleId1], $result['MyContactRef.FavPeople']);
107 // Values returned from virtual join should be in the same order
108 $this->assertEquals(['People2', 'People1'], $result['MyContactRef.FavPeople.last_name']);
110 $result = Contact
::get(FALSE)
112 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'First')
116 $this->assertEquals($contactId1, $result['id']);
118 $result = Contact
::get(FALSE)
120 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'Second')
123 $this->assertCount(2, $result);
126 public function testCurrentUser() {
127 $currentUser = $this->createLoggedInUser();
129 $customGroup = CustomGroup
::create(FALSE)
130 ->addValue('name', 'MyContactRef')
131 ->addValue('extends', 'Individual')
135 CustomField
::create(FALSE)
136 ->addValue('label', 'FavPerson')
137 ->addValue('custom_group_id', $customGroup['id'])
138 ->addValue('html_type', 'Autocomplete-Select')
139 ->addValue('data_type', 'ContactReference')
142 $contactId = Contact
::create(FALSE)
143 ->addValue('first_name', 'Mya')
144 ->addValue('last_name', 'Tester')
145 ->addValue('contact_type', 'Individual')
146 ->addValue('MyContactRef.FavPerson', 'user_contact_id')
150 $contact = Contact
::get(FALSE)
151 ->addSelect('display_name')
152 ->addSelect('MyContactRef.FavPerson')
153 ->addWhere('id', '=', $contactId)
157 $this->assertEquals($currentUser, $contact['MyContactRef.FavPerson']);