Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / tests / phpunit / api / v4 / Custom / CustomFieldGetFieldsTest.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\ContactType;
24 use Civi\Api4\CustomField;
25 use Civi\Api4\CustomGroup;
26 use Civi\Api4\Event;
27 use Civi\Api4\Participant;
28
29 /**
30 * @group headless
31 */
32 class CustomFieldGetFieldsTest extends CustomTestBase {
33
34 private $subTypeName = 'Sub_Tester';
35
36 public function tearDown(): void {
37 parent::tearDown();
38 Contact::delete(FALSE)
39 ->addWhere('id', '>', 0)
40 ->execute();
41 Participant::delete(FALSE)
42 ->addWhere('id', '>', 0)
43 ->execute();
44 Event::delete(FALSE)
45 ->addWhere('id', '>', 0)
46 ->execute();
47 ContactType::delete(FALSE)
48 ->addWhere('name', '=', $this->subTypeName)
49 ->execute();
50 }
51
52 public function testCustomGetFieldsWithContactSubType() {
53 ContactType::create(FALSE)
54 ->addValue('name', $this->subTypeName)
55 ->addValue('label', $this->subTypeName)
56 ->addValue('parent_id:name', 'Individual')
57 ->execute();
58
59 $contact1 = Contact::create(FALSE)
60 ->execute()->first();
61 $contact2 = Contact::create(FALSE)->addValue('contact_sub_type', [$this->subTypeName])
62 ->execute()->first();
63 $org = Contact::create(FALSE)->addValue('contact_type', 'Organization')
64 ->execute()->first();
65
66 // Individual sub-type custom group
67 CustomGroup::create(FALSE)
68 ->addValue('extends', 'Individual')
69 ->addValue('extends_entity_column_value', [$this->subTypeName])
70 ->addValue('title', 'contact_sub')
71 ->execute();
72 CustomField::create(FALSE)
73 ->addValue('custom_group_id.name', 'contact_sub')
74 ->addValue('label', 'sub_field')
75 ->addValue('html_type', 'Text')
76 ->execute();
77
78 // Organization custom group
79 CustomGroup::create(FALSE)
80 ->addValue('extends', 'Organization')
81 ->addValue('title', 'org_group')
82 ->execute();
83 CustomField::create(FALSE)
84 ->addValue('custom_group_id.name', 'org_group')
85 ->addValue('label', 'sub_field')
86 ->addValue('html_type', 'Text')
87 ->execute();
88
89 // Unconditional Contact CustomGroup
90 CustomGroup::create(FALSE)
91 ->addValue('extends', 'Contact')
92 ->addValue('title', 'always')
93 ->addChain('field', CustomField::create()
94 ->addValue('custom_group_id', '$id')
95 ->addValue('label', 'on')
96 ->addValue('html_type', 'Text')
97 )->execute();
98
99 $allFields = Contact::getFields(FALSE)
100 ->execute()->indexBy('name');
101 $this->assertArrayHasKey('contact_sub.sub_field', $allFields);
102 $this->assertArrayHasKey('org_group.sub_field', $allFields);
103 $this->assertArrayHasKey('always.on', $allFields);
104
105 $fieldsWithSubtype = Contact::getFields(FALSE)
106 ->addValue('id', $contact2['id'])
107 ->execute()->indexBy('name');
108 $this->assertArrayHasKey('contact_sub.sub_field', $fieldsWithSubtype);
109 $this->assertArrayNotHasKey('org_group.sub_field', $fieldsWithSubtype);
110 $this->assertArrayHasKey('always.on', $fieldsWithSubtype);
111
112 $fieldsNoSubtype = Contact::getFields(FALSE)
113 ->addValue('id', $contact1['id'])
114 ->execute()->indexBy('name');
115 $this->assertArrayNotHasKey('contact_sub.sub_field', $fieldsNoSubtype);
116 $this->assertArrayNotHasKey('org_group.sub_field', $fieldsNoSubtype);
117 $this->assertArrayHasKey('always.on', $fieldsNoSubtype);
118
119 $groupFields = Contact::getFields(FALSE)
120 ->addValue('id', $org['id'])
121 ->execute()->indexBy('name');
122 $this->assertArrayNotHasKey('contact_sub.sub_field', $groupFields);
123 $this->assertArrayHasKey('org_group.sub_field', $groupFields);
124 $this->assertArrayHasKey('always.on', $groupFields);
125 }
126
127 public function testCustomGetFieldsForParticipantSubTypes() {
128 $event1 = Event::create(FALSE)
129 ->addValue('title', 'Test1')
130 ->addValue('event_type_id:name', 'Meeting')
131 ->addValue('start_date', 'now')
132 ->execute()->first();
133 $event2 = Event::create(FALSE)
134 ->addValue('title', 'Test2')
135 ->addValue('event_type_id:name', 'Meeting')
136 ->addValue('start_date', 'now')
137 ->execute()->first();
138 $event3 = Event::create(FALSE)
139 ->addValue('title', 'Test3')
140 ->addValue('event_type_id:name', 'Conference')
141 ->addValue('start_date', 'now')
142 ->execute()->first();
143 $event4 = Event::create(FALSE)
144 ->addValue('title', 'Test4')
145 ->addValue('event_type_id:name', 'Fundraiser')
146 ->addValue('start_date', 'now')
147 ->execute()->first();
148
149 $cid = Contact::create(FALSE)->execute()->single()['id'];
150
151 $sampleData = [
152 ['event_id' => $event1['id'], 'role_id:name' => ['Attendee']],
153 ['event_id' => $event2['id'], 'role_id:name' => ['Attendee', 'Volunteer']],
154 ['event_id' => $event3['id'], 'role_id:name' => ['Attendee']],
155 ['event_id' => $event4['id'], 'role_id:name' => ['Host']],
156 ];
157 $participants = Participant::save(FALSE)
158 ->addDefault('contact_id', $cid)
159 ->addDefault('status_id:name', 'Registered')
160 ->setRecords($sampleData)
161 ->execute();
162
163 // CustomGroup based on Event Type
164 CustomGroup::create(FALSE)
165 ->addValue('extends', 'Participant')
166 ->addValue('extends_entity_column_id:name', 'ParticipantEventType')
167 ->addValue('extends_entity_column_value:name', ['Meeting', 'Conference'])
168 ->addValue('title', 'meeting_conference')
169 ->addChain('field', CustomField::create()
170 ->addValue('custom_group_id', '$id')
171 ->addValue('label', 'sub_field')
172 ->addValue('html_type', 'Text')
173 )
174 ->execute();
175
176 // CustomGroup based on Participant Status
177 CustomGroup::create(FALSE)
178 ->addValue('extends', 'Participant')
179 ->addValue('extends_entity_column_id:name', 'ParticipantRole')
180 ->addValue('extends_entity_column_value:name', ['Volunteer', 'Host'])
181 ->addValue('title', 'volunteer_host')
182 ->addChain('field', CustomField::create()
183 ->addValue('custom_group_id', '$id')
184 ->addValue('label', 'sub_field')
185 ->addValue('html_type', 'Text')
186 )
187 ->execute();
188
189 // CustomGroup based on Specific Events
190 CustomGroup::create(FALSE)
191 ->addValue('extends', 'Participant')
192 ->addValue('extends_entity_column_id:name', 'ParticipantEventName')
193 ->addValue('extends_entity_column_value', [$event2['id'], $event3['id']])
194 ->addValue('title', 'event_2_and_3')
195 ->addChain('field', CustomField::create()
196 ->addValue('custom_group_id', '$id')
197 ->addValue('label', 'sub_field')
198 ->addValue('html_type', 'Text')
199 )
200 ->execute();
201
202 // Unconditional Participant CustomGroup
203 CustomGroup::create(FALSE)
204 ->addValue('extends', 'Participant')
205 ->addValue('title', 'always')
206 ->addChain('field', CustomField::create()
207 ->addValue('custom_group_id', '$id')
208 ->addValue('label', 'on')
209 ->addValue('html_type', 'Text')
210 )
211 ->execute();
212
213 $allFields = Participant::getFields(FALSE)->execute()->indexBy('name');
214 $this->assertArrayHasKey('meeting_conference.sub_field', $allFields);
215 $this->assertArrayHasKey('volunteer_host.sub_field', $allFields);
216 $this->assertArrayHasKey('event_2_and_3.sub_field', $allFields);
217 $this->assertArrayHasKey('always.on', $allFields);
218
219 $participant0Fields = Participant::getFields(FALSE)
220 ->addValue('id', $participants[0]['id'])
221 ->execute()->indexBy('name');
222 $this->assertArrayHasKey('meeting_conference.sub_field', $participant0Fields);
223 $this->assertArrayNotHasKey('volunteer_host.sub_field', $participant0Fields);
224 $this->assertArrayNotHasKey('event_2_and_3.sub_field', $participant0Fields);
225 $this->assertArrayHasKey('always.on', $participant0Fields);
226
227 $participant1Fields = Participant::getFields(FALSE)
228 ->addValue('id', $participants[1]['id'])
229 ->execute()->indexBy('name');
230 $this->assertArrayHasKey('meeting_conference.sub_field', $participant1Fields);
231 $this->assertArrayHasKey('volunteer_host.sub_field', $participant1Fields);
232 $this->assertArrayHasKey('event_2_and_3.sub_field', $participant1Fields);
233 $this->assertArrayHasKey('always.on', $participant1Fields);
234
235 $participant2Fields = Participant::getFields(FALSE)
236 ->addValue('id', $participants[2]['id'])
237 ->execute()->indexBy('name');
238 $this->assertArrayHasKey('meeting_conference.sub_field', $participant2Fields);
239 $this->assertArrayNotHasKey('volunteer_host.sub_field', $participant2Fields);
240 $this->assertArrayHasKey('event_2_and_3.sub_field', $participant2Fields);
241
242 $participant3Fields = Participant::getFields(FALSE)
243 ->addValue('id', $participants[3]['id'])
244 ->execute()->indexBy('name');
245 $this->assertArrayNotHasKey('meeting_conference.sub_field', $participant3Fields);
246 $this->assertArrayHasKey('volunteer_host.sub_field', $participant3Fields);
247 $this->assertArrayNotHasKey('event_3_and_3.sub_field', $participant3Fields);
248 $this->assertArrayHasKey('always.on', $participant3Fields);
249 }
250
251 }