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\Custom
;
22 use Civi\Api4\Contact
;
23 use Civi\Api4\ContactType
;
24 use Civi\Api4\CustomField
;
25 use Civi\Api4\CustomGroup
;
27 use Civi\Api4\Participant
;
32 class CustomFieldGetFieldsTest
extends CustomTestBase
{
34 private $subTypeName = 'Sub_Tester';
36 public function tearDown(): void
{
38 Contact
::delete(FALSE)
39 ->addWhere('id', '>', 0)
41 Participant
::delete(FALSE)
42 ->addWhere('id', '>', 0)
45 ->addWhere('id', '>', 0)
47 ContactType
::delete(FALSE)
48 ->addWhere('name', '=', $this->subTypeName
)
52 public function testCustomGetFieldsWithContactSubType() {
53 ContactType
::create(FALSE)
54 ->addValue('name', $this->subTypeName
)
55 ->addValue('label', $this->subTypeName
)
56 ->addValue('parent_id:name', 'Individual')
59 $contact1 = Contact
::create(FALSE)
61 $contact2 = Contact
::create(FALSE)->addValue('contact_sub_type', [$this->subTypeName
])
63 $org = Contact
::create(FALSE)->addValue('contact_type', 'Organization')
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')
72 CustomField
::create(FALSE)
73 ->addValue('custom_group_id.name', 'contact_sub')
74 ->addValue('label', 'sub_field')
75 ->addValue('html_type', 'Text')
78 // Organization custom group
79 CustomGroup
::create(FALSE)
80 ->addValue('extends', 'Organization')
81 ->addValue('title', 'org_group')
83 CustomField
::create(FALSE)
84 ->addValue('custom_group_id.name', 'org_group')
85 ->addValue('label', 'sub_field')
86 ->addValue('html_type', 'Text')
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')
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);
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);
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);
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);
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();
149 $cid = Contact
::create(FALSE)->execute()->single()['id'];
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']],
157 $participants = Participant
::save(FALSE)
158 ->addDefault('contact_id', $cid)
159 ->addDefault('status_id:name', 'Registered')
160 ->setRecords($sampleData)
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')
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')
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')
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')
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);
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);
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);
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);
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);