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
22 namespace api\v
4\Action
;
24 use Civi\Api4\CustomField
;
25 use Civi\Api4\CustomGroup
;
26 use Civi\Api4\CustomValue
;
27 use Civi\Api4\Contact
;
32 class CustomValueTest
extends BaseCustomValueTest
{
37 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
39 public function testCRUD() {
40 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
42 $group = uniqid('groupc');
43 $colorField = uniqid('colorc');
44 $multiField = uniqid('chkbx');
45 $textField = uniqid('txt');
47 $customGroup = CustomGroup
::create()
48 ->setCheckPermissions(FALSE)
49 ->addValue('name', $group)
50 ->addValue('extends', 'Contact')
51 ->addValue('is_multiple', TRUE)
56 ->setCheckPermissions(FALSE)
57 ->addValue('label', $colorField)
58 ->addValue('option_values', $optionValues)
59 ->addValue('custom_group_id', $customGroup['id'])
60 ->addValue('html_type', 'Select')
61 ->addValue('data_type', 'String')
65 ->setCheckPermissions(FALSE)
66 ->addValue('label', $multiField)
67 ->addValue('option_values', $optionValues)
68 ->addValue('custom_group_id', $customGroup['id'])
69 ->addValue('html_type', 'CheckBox')
70 ->addValue('data_type', 'String')
74 ->setCheckPermissions(FALSE)
75 ->addValue('label', $textField)
76 ->addValue('custom_group_id', $customGroup['id'])
77 ->addValue('html_type', 'Text')
78 ->addValue('data_type', 'String')
81 $this->contactID
= Contact
::create()
82 ->setCheckPermissions(FALSE)
83 ->addValue('first_name', 'Johann')
84 ->addValue('last_name', 'Tester')
85 ->addValue('contact_type', 'Individual')
89 // Retrieve and check the fields of CustomValue = Custom_$group
90 $fields = CustomValue
::getFields($group)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute();
93 'custom_group' => $group,
94 'name' => $colorField,
95 'title' => $colorField,
96 'entity' => "Custom_$group",
97 'data_type' => 'String',
100 'options' => $optionValues,
103 'custom_group' => $group,
104 'name' => $multiField,
105 'title' => $multiField,
106 'entity' => "Custom_$group",
107 'data_type' => 'String',
110 'options' => $optionValues,
113 'custom_group' => $group,
114 'name' => $textField,
115 'title' => $textField,
116 'entity' => "Custom_$group",
117 'data_type' => 'String',
123 'title' => ts('Custom Value ID'),
124 'entity' => "Custom_$group",
125 'data_type' => 'Integer',
129 'name' => 'entity_id',
130 'title' => ts('Entity ID'),
131 'entity' => "Custom_$group",
132 'data_type' => 'Integer',
133 'fk_entity' => 'Contact',
137 foreach ($expectedResult as $key => $field) {
138 foreach ($field as $attr => $value) {
139 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]);
143 // CASE 1: Test CustomValue::create
144 // Create two records for a single contact and using CustomValue::get ensure that two records are created
145 CustomValue
::create($group)
146 ->addValue($colorField, 'g')
147 ->addValue("entity_id", $this->contactID
)
149 CustomValue
::create($group)
150 ->addValue($colorField, 'r')
151 ->addValue("entity_id", $this->contactID
)
153 // fetch custom values using API4 CustomValue::get
154 $result = CustomValue
::get($group)
155 ->addSelect('id', 'entity_id', $colorField, $colorField . ':label')
156 ->addOrderBy($colorField, 'DESC')
159 // check if two custom values are created
160 $this->assertEquals(2, count($result));
165 $colorField . ':label' => 'Red',
166 'entity_id' => $this->contactID
,
171 $colorField . ':label' => 'Green',
172 'entity_id' => $this->contactID
,
176 foreach ($expectedResult as $key => $field) {
177 foreach ($field as $attr => $value) {
178 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
182 // CASE 2: Test CustomValue::update
183 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
184 CustomValue
::update($group)
185 ->addWhere("id", "=", 1)
186 ->addValue($colorField . ':label', 'Blue')
189 // ensure that the value is changed for id = 1
190 $color = CustomValue
::get($group)
191 ->addWhere("id", "=", 1)
193 ->first()[$colorField];
194 $this->assertEquals('b', $color);
196 // CASE 3: Test CustomValue::replace
197 // create a second contact which will be used to replace the custom values, created earlier
198 $secondContactID = Contact
::create()
199 ->setCheckPermissions(FALSE)
200 ->addValue('first_name', 'Adam')
201 ->addValue('last_name', 'Tester')
202 ->addValue('contact_type', 'Individual')
205 // Replace all the records which was created earlier with entity_id = first contact
206 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
207 CustomValue
::replace($group)
208 ->setRecords([[$colorField => 'g', $multiField . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
209 ->addWhere('entity_id', '=', $this->contactID
)
212 // Check the two records created earlier is replaced by new contact
213 $result = CustomValue
::get($group)
214 ->addSelect('id', 'entity_id', $colorField, $colorField . ':label', $multiField, $multiField . ':label')
216 $this->assertEquals(1, count($result));
222 $colorField . ':label' => 'Green',
223 $multiField => ['r', 'g'],
224 $multiField . ':label' => ['Red', 'Green'],
225 'entity_id' => $secondContactID,
228 foreach ($expectedResult as $key => $field) {
229 foreach ($field as $attr => $value) {
230 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
234 // CASE 4: Test CustomValue::delete
235 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
236 CustomValue
::delete($group)->addWhere("id", "=", 3)->execute();
237 $result = CustomValue
::get($group)->execute();
238 // check that there are no custom values present
239 $this->assertEquals(0, count($result));