72d127508a505c6498f1f2265516d095c6142e81
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CustomValueTest.php
1 <?php
2
3 namespace api\v4\Action;
4
5 use Civi\Api4\CustomField;
6 use Civi\Api4\CustomGroup;
7 use Civi\Api4\CustomValue;
8 use Civi\Api4\Contact;
9
10 /**
11 * @group headless
12 */
13 class CustomValueTest extends BaseCustomValueTest {
14
15 protected $contactID;
16
17 /**
18 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
19 */
20 public function testCRUD() {
21 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
22
23 $group = uniqid('groupc');
24 $colorField = uniqid('colorc');
25 $textField = uniqid('txt');
26
27 $customGroup = CustomGroup::create()
28 ->setCheckPermissions(FALSE)
29 ->addValue('name', $group)
30 ->addValue('extends', 'Contact')
31 ->addValue('is_multiple', TRUE)
32 ->execute()
33 ->first();
34
35 CustomField::create()
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')
42 ->execute();
43
44 CustomField::create()
45 ->setCheckPermissions(FALSE)
46 ->addValue('label', $textField)
47 ->addValue('custom_group_id', $customGroup['id'])
48 ->addValue('html_type', 'Text')
49 ->addValue('data_type', 'String')
50 ->execute();
51
52 $this->contactID = Contact::create()
53 ->setCheckPermissions(FALSE)
54 ->addValue('first_name', 'Johann')
55 ->addValue('last_name', 'Tester')
56 ->addValue('contact_type', 'Individual')
57 ->execute()
58 ->first()['id'];
59
60 // Retrieve and check the fields of CustomValue = Custom_$group
61 $fields = CustomValue::getFields($group)->execute();
62 $expectedResult = [
63 [
64 'custom_field_id' => 1,
65 'custom_group' => $group,
66 'name' => $colorField,
67 'title' => $colorField,
68 'entity' => "Custom_$group",
69 'data_type' => 'String',
70 'fk_entity' => NULL,
71 ],
72 [
73 'custom_field_id' => 2,
74 'custom_group' => $group,
75 'name' => $textField,
76 'title' => $textField,
77 'entity' => "Custom_$group",
78 'data_type' => 'String',
79 'fk_entity' => NULL,
80 ],
81 [
82 'name' => 'id',
83 'title' => ts('Custom Value ID'),
84 'entity' => "Custom_$group",
85 'data_type' => 'Integer',
86 'fk_entity' => NULL,
87 ],
88 [
89 'name' => 'entity_id',
90 'title' => ts('Entity ID'),
91 'entity' => "Custom_$group",
92 'data_type' => 'Integer',
93 'fk_entity' => 'Contact',
94 ],
95 ];
96
97 foreach ($expectedResult as $key => $field) {
98 foreach ($field as $attr => $value) {
99 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]);
100 }
101 }
102
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)
108 ->execute();
109 CustomValue::create($group)
110 ->addValue($colorField, 'Red')
111 ->addValue("entity_id", $this->contactID)
112 ->execute();
113 // fetch custom values using API4 CustomValue::get
114 $result = CustomValue::get($group)->execute();
115
116 // check if two custom values are created
117 $this->assertEquals(2, count($result));
118 $expectedResult = [
119 [
120 'id' => 1,
121 $colorField => 'Green',
122 'entity_id' => $this->contactID,
123 ],
124 [
125 'id' => 2,
126 $colorField => 'Red',
127 'entity_id' => $this->contactID,
128 ],
129 ];
130 // match the data
131 foreach ($expectedResult as $key => $field) {
132 foreach ($field as $attr => $value) {
133 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
134 }
135 }
136
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')
142 ->execute();
143
144 // ensure that the value is changed for id = 1
145 $color = CustomValue::get($group)
146 ->addWhere("id", "=", 1)
147 ->execute()
148 ->first()[$colorField];
149 $this->assertEquals('White', $color);
150
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')
158 ->execute()
159 ->first()['id'];
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)
165 ->execute();
166
167 // Check the two records created earlier is replaced by new contact
168 $result = CustomValue::get($group)->execute();
169 $this->assertEquals(1, count($result));
170
171 $expectedResult = [
172 [
173 'id' => 3,
174 $colorField => 'Rainbow',
175 'entity_id' => $secondContactID,
176 ],
177 ];
178 foreach ($expectedResult as $key => $field) {
179 foreach ($field as $attr => $value) {
180 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
181 }
182 }
183
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));
190 }
191
192 }