APIv4 - Fix saving NULL as custom field value
[civicrm-core.git] / tests / phpunit / api / v4 / Action / 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\Action;
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 BaseCustomValueTest {
30
31 public function testGetWithJoin() {
32 $firstName = uniqid('fav');
33
34 $customGroup = CustomGroup::create(FALSE)
35 ->addValue('name', '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 = Contact::create(FALSE)
56 ->addValue('first_name', $firstName)
57 ->addValue('last_name', 'Person')
58 ->addValue('contact_type', 'Individual')
59 ->execute()
60 ->first()['id'];
61
62 $favPeopleId1 = Contact::create(FALSE)
63 ->addValue('first_name', 'FirstFav')
64 ->addValue('last_name', 'People1')
65 ->addValue('contact_type', 'Individual')
66 ->execute()
67 ->first()['id'];
68
69 $favPeopleId2 = Contact::create(FALSE)
70 ->addValue('first_name', 'SecondFav')
71 ->addValue('last_name', 'People2')
72 ->addValue('contact_type', 'Individual')
73 ->execute()
74 ->first()['id'];
75
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])
82 ->execute()
83 ->first()['id'];
84
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])
90 ->execute()
91 ->first()['id'];
92
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)
100 ->execute()
101 ->single();
102
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']);
109
110 $result = Contact::get(FALSE)
111 ->addSelect('id')
112 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'First')
113 ->execute()
114 ->single();
115
116 $this->assertEquals($contactId1, $result['id']);
117
118 $result = Contact::get(FALSE)
119 ->addSelect('id')
120 ->addWhere('MyContactRef.FavPeople.first_name', 'CONTAINS', 'Second')
121 ->execute();
122
123 $this->assertCount(2, $result);
124 }
125
126 public function testCurrentUser() {
127 $currentUser = $this->createLoggedInUser();
128
129 $customGroup = CustomGroup::create(FALSE)
130 ->addValue('name', 'MyContactRef')
131 ->addValue('extends', 'Individual')
132 ->execute()
133 ->first();
134
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')
140 ->execute();
141
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')
147 ->execute()
148 ->first()['id'];
149
150 $contact = Contact::get(FALSE)
151 ->addSelect('display_name')
152 ->addSelect('MyContactRef.FavPerson')
153 ->addWhere('id', '=', $contactId)
154 ->execute()
155 ->first();
156
157 $this->assertEquals($currentUser, $contact['MyContactRef.FavPerson']);
158 }
159
160 }