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('options', $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('options', $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)->execute();
93 'custom_group' => $group,
94 'name' => $colorField,
95 'title' => $colorField,
96 'entity' => "Custom_$group",
97 'data_type' => 'String',
102 'custom_group' => $group,
103 'name' => $multiField,
104 'title' => $multiField,
105 'entity' => "Custom_$group",
106 'data_type' => 'String',
111 'custom_group' => $group,
112 'name' => $textField,
113 'title' => $textField,
114 'entity' => "Custom_$group",
115 'data_type' => 'String',
121 'title' => ts('Custom Value ID'),
122 'entity' => "Custom_$group",
123 'data_type' => 'Integer',
127 'name' => 'entity_id',
128 'title' => ts('Entity ID'),
129 'entity' => "Custom_$group",
130 'data_type' => 'Integer',
131 'fk_entity' => 'Contact',
135 foreach ($expectedResult as $key => $field) {
136 foreach ($field as $attr => $value) {
137 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]);
141 // CASE 1: Test CustomValue::create
142 // Create two records for a single contact and using CustomValue::get ensure that two records are created
143 CustomValue
::create($group)
144 ->addValue($colorField, 'g')
145 ->addValue("entity_id", $this->contactID
)
147 CustomValue
::create($group)
148 ->addValue($colorField, 'r')
149 ->addValue("entity_id", $this->contactID
)
151 // fetch custom values using API4 CustomValue::get
152 $result = CustomValue
::get($group)->execute();
154 // check if two custom values are created
155 $this->assertEquals(2, count($result));
160 'entity_id' => $this->contactID
,
165 'entity_id' => $this->contactID
,
169 foreach ($expectedResult as $key => $field) {
170 foreach ($field as $attr => $value) {
171 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
175 // CASE 2: Test CustomValue::update
176 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
177 CustomValue
::update($group)
178 ->addWhere("id", "=", 1)
179 ->addValue($colorField, 'b')
182 // ensure that the value is changed for id = 1
183 $color = CustomValue
::get($group)
184 ->addWhere("id", "=", 1)
186 ->first()[$colorField];
187 $this->assertEquals('b', $color);
189 // CASE 3: Test CustomValue::replace
190 // create a second contact which will be used to replace the custom values, created earlier
191 $secondContactID = Contact
::create()
192 ->setCheckPermissions(FALSE)
193 ->addValue('first_name', 'Adam')
194 ->addValue('last_name', 'Tester')
195 ->addValue('contact_type', 'Individual')
198 // Replace all the records which was created earlier with entity_id = first contact
199 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
200 CustomValue
::replace($group)
201 ->setRecords([[$colorField => 'g', $multiField => ['r', 'g'], 'entity_id' => $secondContactID]])
202 ->addWhere('entity_id', '=', $this->contactID
)
205 // Check the two records created earlier is replaced by new contact
206 $result = CustomValue
::get($group)->execute();
207 $this->assertEquals(1, count($result));
213 $multiField => ['r', 'g'],
214 'entity_id' => $secondContactID,
217 foreach ($expectedResult as $key => $field) {
218 foreach ($field as $attr => $value) {
219 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
223 // CASE 4: Test CustomValue::delete
224 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
225 CustomValue
::delete($group)->addWhere("id", "=", 3)->execute();
226 $result = CustomValue
::get($group)->execute();
227 // check that there are no custom values present
228 $this->assertEquals(0, count($result));