Use new checkPermissions shorthand in api calls
[civicrm-core.git] / tests / phpunit / api / v4 / Action / BasicCustomFieldTest.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 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Action;
23
24 use Civi\Api4\Contact;
25 use Civi\Api4\CustomField;
26 use Civi\Api4\CustomGroup;
27
28 /**
29 * @group headless
30 */
31 class BasicCustomFieldTest extends BaseCustomValueTest {
32
33 public function testWithSingleField() {
34
35 $customGroup = CustomGroup::create(FALSE)
36 ->addValue('name', 'MyIndividualFields')
37 ->addValue('extends', 'Individual')
38 ->execute()
39 ->first();
40
41 CustomField::create(FALSE)
42 ->addValue('label', 'FavColor')
43 ->addValue('custom_group_id', $customGroup['id'])
44 ->addValue('html_type', 'Text')
45 ->addValue('data_type', 'String')
46 ->execute();
47
48 // Individual fields should show up when contact_type = null|Individual but not other contact types
49 $getFields = Contact::getFields(FALSE);
50 $this->assertContains('MyIndividualFields.FavColor', $getFields->execute()->column('name'));
51 $this->assertContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name'));
52 $this->assertNotContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Household'])->execute()->column('name'));
53
54 $contactId = Contact::create(FALSE)
55 ->addValue('first_name', 'Johann')
56 ->addValue('last_name', 'Tester')
57 ->addValue('contact_type', 'Individual')
58 ->addValue('MyIndividualFields.FavColor', 'Red')
59 ->execute()
60 ->first()['id'];
61
62 $contact = Contact::get(FALSE)
63 ->addSelect('first_name')
64 ->addSelect('MyIndividualFields.FavColor')
65 ->addWhere('id', '=', $contactId)
66 ->addWhere('MyIndividualFields.FavColor', '=', 'Red')
67 ->execute()
68 ->first();
69
70 $this->assertEquals('Red', $contact['MyIndividualFields.FavColor']);
71
72 Contact::update()
73 ->addWhere('id', '=', $contactId)
74 ->addValue('MyIndividualFields.FavColor', 'Blue')
75 ->execute();
76
77 $contact = Contact::get(FALSE)
78 ->addSelect('MyIndividualFields.FavColor')
79 ->addWhere('id', '=', $contactId)
80 ->execute()
81 ->first();
82
83 $this->assertEquals('Blue', $contact['MyIndividualFields.FavColor']);
84 }
85
86 public function testWithTwoFields() {
87
88 // First custom set
89 CustomGroup::create(FALSE)
90 ->addValue('name', 'MyContactFields')
91 ->addValue('extends', 'Contact')
92 ->addChain('field1', CustomField::create()
93 ->addValue('label', 'FavColor')
94 ->addValue('custom_group_id', '$id')
95 ->addValue('html_type', 'Text')
96 ->addValue('data_type', 'String'))
97 ->addChain('field2', CustomField::create()
98 ->addValue('label', 'FavFood')
99 ->addValue('custom_group_id', '$id')
100 ->addValue('html_type', 'Text')
101 ->addValue('data_type', 'String'))
102 ->execute();
103
104 // Second custom set
105 CustomGroup::create(FALSE)
106 ->addValue('name', 'MyContactFields2')
107 ->addValue('extends', 'Contact')
108 ->addChain('field1', CustomField::create()
109 ->addValue('label', 'FavColor')
110 ->addValue('custom_group_id', '$id')
111 ->addValue('html_type', 'Text')
112 ->addValue('data_type', 'String'))
113 ->addChain('field2', CustomField::create()
114 ->addValue('label', 'FavFood')
115 ->addValue('custom_group_id', '$id')
116 ->addValue('html_type', 'Text')
117 ->addValue('data_type', 'String'))
118 ->execute();
119
120 $contactId1 = Contact::create(FALSE)
121 ->addValue('first_name', 'Johann')
122 ->addValue('last_name', 'Tester')
123 ->addValue('MyContactFields.FavColor', 'Red')
124 ->addValue('MyContactFields.FavFood', 'Cherry')
125 ->execute()
126 ->first()['id'];
127
128 $contactId2 = Contact::create(FALSE)
129 ->addValue('first_name', 'MaryLou')
130 ->addValue('last_name', 'Tester')
131 ->addValue('MyContactFields.FavColor', 'Purple')
132 ->addValue('MyContactFields.FavFood', 'Grapes')
133 ->execute()
134 ->first()['id'];
135
136 $contact = Contact::get(FALSE)
137 ->addSelect('first_name')
138 ->addSelect('MyContactFields.FavColor')
139 ->addSelect('MyContactFields.FavFood')
140 ->addWhere('id', '=', $contactId1)
141 ->addWhere('MyContactFields.FavColor', '=', 'Red')
142 ->addWhere('MyContactFields.FavFood', '=', 'Cherry')
143 ->execute()
144 ->first();
145
146 $this->assertArrayHasKey('MyContactFields.FavColor', $contact);
147 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
148
149 // Update 2nd set and ensure 1st hasn't changed
150 Contact::update()
151 ->addWhere('id', '=', $contactId1)
152 ->addValue('MyContactFields2.FavColor', 'Orange')
153 ->addValue('MyContactFields2.FavFood', 'Tangerine')
154 ->execute();
155 $contact = Contact::get(FALSE)
156 ->addSelect('MyContactFields.FavColor', 'MyContactFields2.FavColor', 'MyContactFields.FavFood', 'MyContactFields2.FavFood')
157 ->addWhere('id', '=', $contactId1)
158 ->execute()
159 ->first();
160 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
161 $this->assertEquals('Orange', $contact['MyContactFields2.FavColor']);
162 $this->assertEquals('Cherry', $contact['MyContactFields.FavFood']);
163 $this->assertEquals('Tangerine', $contact['MyContactFields2.FavFood']);
164
165 // Update 1st set and ensure 2st hasn't changed
166 Contact::update()
167 ->addWhere('id', '=', $contactId1)
168 ->addValue('MyContactFields.FavColor', 'Blue')
169 ->execute();
170 $contact = Contact::get(FALSE)
171 ->addSelect('MyContactFields.FavColor', 'MyContactFields2.FavColor', 'MyContactFields.FavFood', 'MyContactFields2.FavFood')
172 ->addWhere('id', '=', $contactId1)
173 ->execute()
174 ->first();
175 $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
176 $this->assertEquals('Orange', $contact['MyContactFields2.FavColor']);
177 $this->assertEquals('Cherry', $contact['MyContactFields.FavFood']);
178 $this->assertEquals('Tangerine', $contact['MyContactFields2.FavFood']);
179
180 $search = Contact::get(FALSE)
181 ->addClause('OR', ['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes'])
182 ->addSelect('id')
183 ->addOrderBy('id')
184 ->execute()
185 ->indexBy('id');
186
187 $this->assertEquals([$contactId1, $contactId2], array_keys((array) $search));
188
189 $search = Contact::get(FALSE)
190 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
191 ->addSelect('id')
192 ->addOrderBy('id')
193 ->execute()
194 ->indexBy('id');
195
196 $this->assertNotContains($contactId2, array_keys((array) $search));
197
198 $search = Contact::get(FALSE)
199 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
200 ->addSelect('id')
201 ->addOrderBy('id')
202 ->execute()
203 ->indexBy('id');
204
205 $this->assertContains($contactId1, array_keys((array) $search));
206 $this->assertNotContains($contactId2, array_keys((array) $search));
207
208 $search = Contact::get(FALSE)
209 ->setWhere([['NOT', ['OR', [['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes']]]]])
210 ->addSelect('id')
211 ->addOrderBy('id')
212 ->execute()
213 ->indexBy('id');
214
215 $this->assertNotContains($contactId1, array_keys((array) $search));
216 $this->assertNotContains($contactId2, array_keys((array) $search));
217 }
218
219 }