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\CustomField
;
23 use Civi\Api4\CustomGroup
;
24 use Civi\Api4\CustomValue
;
30 class CustomValueTest
extends CustomTestBase
{
35 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
37 public function testCRUD() {
38 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
40 $group = uniqid('groupc');
41 $colorFieldName = uniqid('colorc');
42 $multiFieldName = uniqid('chkbx');
43 $textFieldName = uniqid('txt');
45 $customGroup = CustomGroup
::create(FALSE)
46 ->addValue('title', $group)
47 ->addValue('extends', 'Contact')
48 ->addValue('is_multiple', TRUE)
52 $colorField = CustomField
::create(FALSE)
53 ->addValue('label', $colorFieldName)
54 ->addValue('option_values', $optionValues)
55 ->addValue('custom_group_id', $customGroup['id'])
56 ->addValue('html_type', 'Select')
57 ->addValue('data_type', 'String')
60 $multiField = CustomField
::create(FALSE)
61 ->addValue('label', $multiFieldName)
62 ->addValue('option_values', $optionValues)
63 ->addValue('custom_group_id', $customGroup['id'])
64 ->addValue('html_type', 'CheckBox')
65 ->addValue('data_type', 'String')
68 $textField = CustomField
::create(FALSE)
69 ->addValue('label', $textFieldName)
70 ->addValue('custom_group_id', $customGroup['id'])
71 ->addValue('html_type', 'Text')
72 ->addValue('data_type', 'String')
75 $this->contactID
= $this->createTestRecord('Contact', [
76 'first_name' => 'Johann',
77 'last_name' => 'Tester',
78 'contact_type' => 'Individual',
81 // Ensure virtual api entity has been created
82 $entity = Entity
::get(FALSE)
83 ->addWhere('name', '=', "Custom_$group")
84 ->execute()->single();
85 $this->assertEquals(['CustomValue'], $entity['type']);
86 $this->assertEquals(['id'], $entity['primary_key']);
87 $this->assertEquals($customGroup['table_name'], $entity['table_name']);
88 $this->assertEquals('Civi\Api4\CustomValue', $entity['class']);
89 $this->assertEquals([$group], $entity['class_args']);
90 $this->assertEquals('secondary', $entity['searchable']);
92 // Retrieve and check the fields of CustomValue = Custom_$group
93 $fields = CustomValue
::getFields($group, FALSE)->setLoadOptions(TRUE)->execute();
96 'custom_group' => $group,
98 'name' => $colorFieldName,
99 'title' => $colorFieldName,
100 'entity' => "Custom_$group",
101 'table_name' => $customGroup['table_name'],
102 'column_name' => $colorField['column_name'],
103 'data_type' => 'String',
106 'options' => $optionValues,
109 'custom_group' => $group,
111 'name' => $multiFieldName,
112 'title' => $multiFieldName,
113 'entity' => "Custom_$group",
114 'table_name' => $customGroup['table_name'],
115 'column_name' => $multiField['column_name'],
116 'data_type' => 'String',
119 'options' => $optionValues,
122 'custom_group' => $group,
124 'name' => $textFieldName,
125 'title' => $textFieldName,
126 'entity' => "Custom_$group",
127 'table_name' => $customGroup['table_name'],
128 'column_name' => $textField['column_name'],
129 'data_type' => 'String',
136 'title' => ts('Custom Value ID'),
137 'entity' => "Custom_$group",
138 'table_name' => $customGroup['table_name'],
139 'column_name' => 'id',
140 'data_type' => 'Integer',
144 'name' => 'entity_id',
146 'title' => ts('Entity ID'),
147 'table_name' => $customGroup['table_name'],
148 'column_name' => 'entity_id',
149 'entity' => "Custom_$group",
150 'data_type' => 'Integer',
151 'fk_entity' => 'Contact',
155 foreach ($expectedResult as $key => $field) {
156 foreach ($field as $attr => $value) {
157 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr], "$key $attr");
161 // CASE 1: Test CustomValue::create
162 // Create two records for a single contact and using CustomValue::get ensure that two records are created
164 CustomValue
::create($group)
165 ->addValue($colorFieldName, 'g')
166 ->addValue("entity_id", $this->contactID
)
167 ->execute()->first(),
168 CustomValue
::create($group)
169 ->addValue($colorFieldName . ':label', 'Red')
170 ->addValue("entity_id", $this->contactID
)
171 ->execute()->first(),
173 // fetch custom values using API4 CustomValue::get
174 $result = CustomValue
::get($group)
175 ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label')
176 ->addOrderBy($colorFieldName, 'ASC')
179 // check if two custom values are created
180 $this->assertEquals(2, count($result));
184 $colorFieldName => 'g',
185 $colorFieldName . ':label' => 'Green',
186 'entity_id' => $this->contactID
,
190 $colorFieldName => 'r',
191 $colorFieldName . ':label' => 'Red',
192 'entity_id' => $this->contactID
,
196 foreach ($expectedResult as $key => $field) {
197 foreach ($field as $attr => $value) {
198 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
199 if (!strpos($attr, ':')) {
200 $this->assertEquals($expectedResult[$key][$attr], $created[$key][$attr]);
205 // CASE 2: Test CustomValue::update
206 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
207 CustomValue
::update($group)
208 ->addWhere("id", "=", 1)
209 ->addValue($colorFieldName . ':label', 'Blue')
212 // ensure that the value is changed for id = 1
213 $color = CustomValue
::get($group)
214 ->addWhere("id", "=", 1)
216 ->first()[$colorFieldName];
217 $this->assertEquals('b', $color);
219 // CASE 3: Test CustomValue::replace
220 // create a second contact which will be used to replace the custom values, created earlier
221 $secondContactID = $this->createTestRecord('Contact', [
222 'first_name' => 'Adam',
223 'last_name' => 'Tester',
224 'contact_type' => 'Individual',
226 // Replace all the records which was created earlier with entity_id = first contact
227 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
228 CustomValue
::replace($group)
229 ->setRecords([[$colorFieldName => 'g', $multiFieldName . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
230 ->addWhere('entity_id', '=', $this->contactID
)
233 // Check the two records created earlier is replaced by new contact
234 $result = CustomValue
::get($group)
235 ->addSelect('id', 'entity_id', $colorFieldName, $colorFieldName . ':label', $multiFieldName, $multiFieldName . ':label')
237 $this->assertEquals(1, count($result));
242 $colorFieldName => 'g',
243 $colorFieldName . ':label' => 'Green',
244 $multiFieldName => ['r', 'g'],
245 $multiFieldName . ':label' => ['Red', 'Green'],
246 'entity_id' => $secondContactID,
249 foreach ($expectedResult as $key => $field) {
250 foreach ($field as $attr => $value) {
251 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
256 CustomField
::update(FALSE)
257 ->addValue('is_active', FALSE)
258 ->addWhere('id', '=', $multiField['id'])
261 $result = CustomValue
::get($group)->execute()->single();
262 $this->assertArrayHasKey($colorFieldName, $result);
263 $this->assertArrayNotHasKey($multiFieldName, $result);
265 // CASE 4: Test CustomValue::delete
266 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
267 CustomValue
::delete($group)->addWhere("id", "=", 3)->execute();
268 $result = CustomValue
::get($group)->execute();
269 // check that there are no custom values present
270 $this->assertEquals(0, count($result));
274 * Whenever a CustomGroup toggles the `is_multiple` flag, the entity-list should be updated.
276 * @throws \API_Exception
277 * @throws \Civi\API\Exception\UnauthorizedException
279 public function testEntityRefresh() {
280 $groupName = uniqid('groupc');
282 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
284 CustomGroup
::create(FALSE)
285 ->addValue('title', $groupName)
286 ->addValue('extends', 'Contact')
287 ->addValue('is_multiple', FALSE)
290 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
292 CustomGroup
::update(FALSE)
293 ->addWhere('name', '=', $groupName)
294 ->addValue('is_multiple', TRUE)
296 $this->assertContains("Custom_$groupName", Entity
::get()->execute()->column('name'));
298 CustomGroup
::update(FALSE)
299 ->addWhere('name', '=', $groupName)
300 ->addValue('is_multiple', FALSE)
302 $this->assertNotContains("Custom_$groupName", Entity
::get()->execute()->column('name'));