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\Action
;
22 use Civi\Api4\CustomField
;
23 use Civi\Api4\CustomGroup
;
24 use Civi\Api4\CustomValue
;
25 use Civi\Api4\Contact
;
31 class CustomValueTest
extends BaseCustomValueTest
{
36 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
38 public function testCRUD() {
39 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
41 $group = uniqid('groupc');
42 $colorFieldName = uniqid('colorc');
43 $multiFieldName = uniqid('chkbx');
44 $textFieldName = uniqid('txt');
46 $customGroup = CustomGroup
::create(FALSE)
47 ->addValue('title', $group)
48 ->addValue('extends', 'Contact')
49 ->addValue('is_multiple', TRUE)
53 $colorField = CustomField
::create(FALSE)
54 ->addValue('label', $colorFieldName)
55 ->addValue('option_values', $optionValues)
56 ->addValue('custom_group_id', $customGroup['id'])
57 ->addValue('html_type', 'Select')
58 ->addValue('data_type', 'String')
61 $multiField = CustomField
::create(FALSE)
62 ->addValue('label', $multiFieldName)
63 ->addValue('option_values', $optionValues)
64 ->addValue('custom_group_id', $customGroup['id'])
65 ->addValue('html_type', 'CheckBox')
66 ->addValue('data_type', 'String')
69 $textField = CustomField
::create(FALSE)
70 ->addValue('label', $textFieldName)
71 ->addValue('custom_group_id', $customGroup['id'])
72 ->addValue('html_type', 'Text')
73 ->addValue('data_type', 'String')
76 $this->contactID
= Contact
::create(FALSE)
77 ->addValue('first_name', 'Johann')
78 ->addValue('last_name', 'Tester')
79 ->addValue('contact_type', 'Individual')
83 // Ensure virtual api entity has been created
84 $entity = Entity
::get(FALSE)
85 ->addWhere('name', '=', "Custom_$group")
86 ->execute()->single();
87 $this->assertEquals(['CustomValue'], $entity['type']);
88 $this->assertEquals(['id'], $entity['primary_key']);
89 $this->assertEquals($customGroup['table_name'], $entity['table_name']);
90 $this->assertEquals('Civi\Api4\CustomValue', $entity['class']);
91 $this->assertEquals([$group], $entity['class_args']);
92 $this->assertEquals('secondary', $entity['searchable']);
94 // Retrieve and check the fields of CustomValue = Custom_$group
95 $fields = CustomValue
::getFields($group)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute();
98 'custom_group' => $group,
100 'name' => $colorFieldName,
101 'title' => $colorFieldName,
102 'entity' => "Custom_$group",
103 'table_name' => $customGroup['table_name'],
104 'column_name' => $colorField['column_name'],
105 'data_type' => 'String',
108 'options' => $optionValues,
111 'custom_group' => $group,
113 'name' => $multiFieldName,
114 'title' => $multiFieldName,
115 'entity' => "Custom_$group",
116 'table_name' => $customGroup['table_name'],
117 'column_name' => $multiField['column_name'],
118 'data_type' => 'String',
121 'options' => $optionValues,
124 'custom_group' => $group,
126 'name' => $textFieldName,
127 'title' => $textFieldName,
128 'entity' => "Custom_$group",
129 'table_name' => $customGroup['table_name'],
130 'column_name' => $textField['column_name'],
131 'data_type' => 'String',
138 'title' => ts('Custom Value ID'),
139 'entity' => "Custom_$group",
140 'table_name' => $customGroup['table_name'],
141 'column_name' => 'id',
142 'data_type' => 'Integer',
146 'name' => 'entity_id',
148 'title' => ts('Entity ID'),
149 'table_name' => $customGroup['table_name'],
150 'column_name' => 'entity_id',
151 'entity' => "Custom_$group",
152 'data_type' => 'Integer',
153 'fk_entity' => 'Contact',
157 foreach ($expectedResult as $key => $field) {
158 foreach ($field as $attr => $value) {
159 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr], "$key $attr");
163 // CASE 1: Test CustomValue::create
164 // Create two records for a single contact and using CustomValue::get ensure that two records are created
166 CustomValue
::create($group)
167 ->addValue($colorFieldName, 'g')
168 ->addValue("entity_id", $this->contactID
)
169 ->execute()->first(),
170 CustomValue
::create($group)
171 ->addValue($colorFieldName . ':label', 'Red')
172 ->addValue("entity_id", $this->contactID
)
173 ->execute()->first(),
175 // fetch custom values using API4 CustomValue::get
176 $result = CustomValue
::get($group)
177 ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label')
178 ->addOrderBy($colorFieldName, 'ASC')
181 // check if two custom values are created
182 $this->assertEquals(2, count($result));
186 $colorFieldName => 'g',
187 $colorFieldName . ':label' => 'Green',
188 'entity_id' => $this->contactID
,
192 $colorFieldName => 'r',
193 $colorFieldName . ':label' => 'Red',
194 'entity_id' => $this->contactID
,
198 foreach ($expectedResult as $key => $field) {
199 foreach ($field as $attr => $value) {
200 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
201 if (!strpos($attr, ':')) {
202 $this->assertEquals($expectedResult[$key][$attr], $created[$key][$attr]);
207 // CASE 2: Test CustomValue::update
208 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
209 CustomValue
::update($group)
210 ->addWhere("id", "=", 1)
211 ->addValue($colorFieldName . ':label', 'Blue')
214 // ensure that the value is changed for id = 1
215 $color = CustomValue
::get($group)
216 ->addWhere("id", "=", 1)
218 ->first()[$colorFieldName];
219 $this->assertEquals('b', $color);
221 // CASE 3: Test CustomValue::replace
222 // create a second contact which will be used to replace the custom values, created earlier
223 $secondContactID = Contact
::create(FALSE)
224 ->addValue('first_name', 'Adam')
225 ->addValue('last_name', 'Tester')
226 ->addValue('contact_type', 'Individual')
229 // Replace all the records which was created earlier with entity_id = first contact
230 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
231 CustomValue
::replace($group)
232 ->setRecords([[$colorFieldName => 'g', $multiFieldName . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
233 ->addWhere('entity_id', '=', $this->contactID
)
236 // Check the two records created earlier is replaced by new contact
237 $result = CustomValue
::get($group)
238 ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label', $multiFieldName, $multiFieldName . ':label')
240 $this->assertEquals(1, count($result));
245 $colorFieldName => 'g',
246 $colorFieldName . ':label' => 'Green',
247 $multiFieldName => ['r', 'g'],
248 $multiFieldName . ':label' => ['Red', 'Green'],
249 'entity_id' => $secondContactID,
252 foreach ($expectedResult as $key => $field) {
253 foreach ($field as $attr => $value) {
254 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
258 // CASE 4: Test CustomValue::delete
259 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
260 CustomValue
::delete($group)->addWhere("id", "=", 3)->execute();
261 $result = CustomValue
::get($group)->execute();
262 // check that there are no custom values present
263 $this->assertEquals(0, count($result));
267 * Whenever a CustomGroup toggles the `is_multiple` flag, the entity-list should be updated.
269 * @throws \API_Exception
270 * @throws \Civi\API\Exception\UnauthorizedException
272 public function testEntityRefresh() {
273 $groupName = uniqid('groupc');
275 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
277 CustomGroup
::create(FALSE)
278 ->addValue('title', $groupName)
279 ->addValue('extends', 'Contact')
280 ->addValue('is_multiple', FALSE)
283 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
285 CustomGroup
::update(FALSE)
286 ->addWhere('name', '=', $groupName)
287 ->addValue('is_multiple', TRUE)
289 $this->assertContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
291 CustomGroup
::update(FALSE)
292 ->addWhere('name', '=', $groupName)
293 ->addValue('is_multiple', FALSE)
295 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));