Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / tests / phpunit / api / v4 / Custom / CustomContactRefTest.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
20 namespace api\v4\Custom;
21
22 use Civi\Api4\Contact;
23 use Civi\Api4\CustomField;
24 use Civi\Api4\CustomGroup;
25
26 /**
27 * @group headless
28 */
29 class CustomContactRefTest extends CustomTestBase {
30
31 public function testGetWithJoin() {
32 $firstName = uniqid('fav');
33
34 $customGroup = CustomGroup::create(FALSE)
35 ->addValue('title', 'MyContactRef')
36 ->addValue('extends', 'Individual')
37 ->execute()
38 ->first();
39
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')
45 ->execute();
46
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)
53 ->execute();
54
55 $favPersonId = $this->createTestRecord('Contact', [
56 'first_name' => $firstName,
57 'last_name' => 'Person',
58 'contact_type' => 'Individual',
59 ])['id'];
60
61 $favPeopleId1 = $this->createTestRecord('Contact', [
62 'first_name' => 'FirstFav',
63 'last_name' => 'People1',
64 'contact_type' => 'Individual',
65 ])['id'];
66
67 $favPeopleId2 = $this->createTestRecord('Contact', [
68 'first_name' => 'SecondFav',
69 'last_name' => 'People2',
70 'contact_type' => 'Individual',
71 ])['id'];
72
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],
79 ])['id'];
80
81 $contactId2 = $this->createTestRecord('Contact', [
82 'first_name' => 'Bea',
83 'last_name' => 'Tester',
84 'contact_type' => 'Individual',
85 'MyContactRef.FavPeople' => [$favPeopleId2],
86 ])['id'];
87
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)
95 ->execute()
96 ->single();
97
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']);
104
105 $result = Contact::get(FALSE)
106 ->addSelect('id')
107 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'First')
108 ->execute()
109 ->single();
110
111 $this->assertEquals($contactId1, $result['id']);
112
113 $result = Contact::get(FALSE)
114 ->addSelect('id')
115 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'Second')
116 ->execute();
117
118 $this->assertCount(2, $result);
119 }
120
121 public function testCurrentUser() {
122 $currentUser = $this->createLoggedInUser();
123
124 $customGroup = CustomGroup::create(FALSE)
125 ->addValue('title', 'MyContactRef')
126 ->addValue('extends', 'Individual')
127 ->execute()
128 ->first();
129
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')
135 ->execute();
136
137 $contactId = $this->createTestRecord('Contact', [
138 'first_name' => 'Mya',
139 'last_name' => 'Tester',
140 'contact_type' => 'Individual',
141 'MyContactRef.FavPerson' => 'user_contact_id',
142 ])['id'];
143
144 $contact = Contact::get(FALSE)
145 ->addSelect('display_name')
146 ->addSelect('MyContactRef.FavPerson')
147 ->addWhere('id', '=', $contactId)
148 ->execute()
149 ->first();
150
151 $this->assertEquals($currentUser, $contact['MyContactRef.FavPerson']);
152 }
153
154 }