72d127508a505c6498f1f2265516d095c6142e81
3 namespace api\v
4\Action
;
5 use Civi\Api4\CustomField
;
6 use Civi\Api4\CustomGroup
;
7 use Civi\Api4\CustomValue
;
13 class CustomValueTest
extends BaseCustomValueTest
{
18 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
20 public function testCRUD() {
21 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
23 $group = uniqid('groupc');
24 $colorField = uniqid('colorc');
25 $textField = uniqid('txt');
27 $customGroup = CustomGroup
::create()
28 ->setCheckPermissions(FALSE)
29 ->addValue('name', $group)
30 ->addValue('extends', 'Contact')
31 ->addValue('is_multiple', TRUE)
36 ->setCheckPermissions(FALSE)
37 ->addValue('label', $colorField)
38 ->addValue('options', $optionValues)
39 ->addValue('custom_group_id', $customGroup['id'])
40 ->addValue('html_type', 'Select')
41 ->addValue('data_type', 'String')
45 ->setCheckPermissions(FALSE)
46 ->addValue('label', $textField)
47 ->addValue('custom_group_id', $customGroup['id'])
48 ->addValue('html_type', 'Text')
49 ->addValue('data_type', 'String')
52 $this->contactID
= Contact
::create()
53 ->setCheckPermissions(FALSE)
54 ->addValue('first_name', 'Johann')
55 ->addValue('last_name', 'Tester')
56 ->addValue('contact_type', 'Individual')
60 // Retrieve and check the fields of CustomValue = Custom_$group
61 $fields = CustomValue
::getFields($group)->execute();
64 'custom_field_id' => 1,
65 'custom_group' => $group,
66 'name' => $colorField,
67 'title' => $colorField,
68 'entity' => "Custom_$group",
69 'data_type' => 'String',
73 'custom_field_id' => 2,
74 'custom_group' => $group,
76 'title' => $textField,
77 'entity' => "Custom_$group",
78 'data_type' => 'String',
83 'title' => ts('Custom Value ID'),
84 'entity' => "Custom_$group",
85 'data_type' => 'Integer',
89 'name' => 'entity_id',
90 'title' => ts('Entity ID'),
91 'entity' => "Custom_$group",
92 'data_type' => 'Integer',
93 'fk_entity' => 'Contact',
97 foreach ($expectedResult as $key => $field) {
98 foreach ($field as $attr => $value) {
99 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]);
103 // CASE 1: Test CustomValue::create
104 // Create two records for a single contact and using CustomValue::get ensure that two records are created
105 CustomValue
::create($group)
106 ->addValue($colorField, 'Green')
107 ->addValue("entity_id", $this->contactID
)
109 CustomValue
::create($group)
110 ->addValue($colorField, 'Red')
111 ->addValue("entity_id", $this->contactID
)
113 // fetch custom values using API4 CustomValue::get
114 $result = CustomValue
::get($group)->execute();
116 // check if two custom values are created
117 $this->assertEquals(2, count($result));
121 $colorField => 'Green',
122 'entity_id' => $this->contactID
,
126 $colorField => 'Red',
127 'entity_id' => $this->contactID
,
131 foreach ($expectedResult as $key => $field) {
132 foreach ($field as $attr => $value) {
133 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
137 // CASE 2: Test CustomValue::update
138 // Update a records whose id is 1 and change the custom field (name = Color) value to 'White' from 'Green'
139 CustomValue
::update($group)
140 ->addWhere("id", "=", 1)
141 ->addValue($colorField, 'White')
144 // ensure that the value is changed for id = 1
145 $color = CustomValue
::get($group)
146 ->addWhere("id", "=", 1)
148 ->first()[$colorField];
149 $this->assertEquals('White', $color);
151 // CASE 3: Test CustomValue::replace
152 // create a second contact which will be used to replace the custom values, created earlier
153 $secondContactID = Contact
::create()
154 ->setCheckPermissions(FALSE)
155 ->addValue('first_name', 'Adam')
156 ->addValue('last_name', 'Tester')
157 ->addValue('contact_type', 'Individual')
160 // Replace all the records which was created earlier with entity_id = first contact
161 // with custom record [$colorField => 'Rainbow', 'entity_id' => $secondContactID]
162 CustomValue
::replace($group)
163 ->setRecords([[$colorField => 'Rainbow', 'entity_id' => $secondContactID]])
164 ->addWhere('entity_id', '=', $this->contactID
)
167 // Check the two records created earlier is replaced by new contact
168 $result = CustomValue
::get($group)->execute();
169 $this->assertEquals(1, count($result));
174 $colorField => 'Rainbow',
175 'entity_id' => $secondContactID,
178 foreach ($expectedResult as $key => $field) {
179 foreach ($field as $attr => $value) {
180 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
184 // CASE 4: Test CustomValue::delete
185 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
186 CustomValue
::delete($group)->addWhere("id", "=", 3)->execute();
187 $result = CustomValue
::get($group)->execute();
188 // check that there are no custom values present
189 $this->assertEquals(0, count($result));