Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace api\v4\Action; |
23 | ||
24 | use Civi\Api4\CustomField; | |
25 | use Civi\Api4\CustomGroup; | |
26 | use Civi\Api4\CustomValue; | |
27 | use Civi\Api4\Contact; | |
28 | ||
29 | /** | |
30 | * @group headless | |
31 | */ | |
32 | class CustomValueTest extends BaseCustomValueTest { | |
33 | ||
34 | protected $contactID; | |
35 | ||
36 | /** | |
37 | * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete | |
38 | */ | |
39 | public function testCRUD() { | |
40 | $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue']; | |
41 | ||
42 | $group = uniqid('groupc'); | |
43 | $colorField = uniqid('colorc'); | |
3766bd36 | 44 | $multiField = uniqid('chkbx'); |
19b53e5b C |
45 | $textField = uniqid('txt'); |
46 | ||
47 | $customGroup = CustomGroup::create() | |
48 | ->setCheckPermissions(FALSE) | |
49 | ->addValue('name', $group) | |
50 | ->addValue('extends', 'Contact') | |
51 | ->addValue('is_multiple', TRUE) | |
52 | ->execute() | |
53 | ->first(); | |
54 | ||
55 | CustomField::create() | |
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') | |
62 | ->execute(); | |
63 | ||
3766bd36 CW |
64 | CustomField::create() |
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') | |
71 | ->execute(); | |
72 | ||
19b53e5b C |
73 | CustomField::create() |
74 | ->setCheckPermissions(FALSE) | |
75 | ->addValue('label', $textField) | |
76 | ->addValue('custom_group_id', $customGroup['id']) | |
77 | ->addValue('html_type', 'Text') | |
78 | ->addValue('data_type', 'String') | |
79 | ->execute(); | |
80 | ||
81 | $this->contactID = Contact::create() | |
82 | ->setCheckPermissions(FALSE) | |
83 | ->addValue('first_name', 'Johann') | |
84 | ->addValue('last_name', 'Tester') | |
85 | ->addValue('contact_type', 'Individual') | |
86 | ->execute() | |
87 | ->first()['id']; | |
88 | ||
89 | // Retrieve and check the fields of CustomValue = Custom_$group | |
90 | $fields = CustomValue::getFields($group)->execute(); | |
91 | $expectedResult = [ | |
92 | [ | |
19b53e5b C |
93 | 'custom_group' => $group, |
94 | 'name' => $colorField, | |
95 | 'title' => $colorField, | |
96 | 'entity' => "Custom_$group", | |
97 | 'data_type' => 'String', | |
98 | 'fk_entity' => NULL, | |
3766bd36 CW |
99 | 'serialize' => NULL, |
100 | ], | |
101 | [ | |
102 | 'custom_group' => $group, | |
103 | 'name' => $multiField, | |
104 | 'title' => $multiField, | |
105 | 'entity' => "Custom_$group", | |
106 | 'data_type' => 'String', | |
107 | 'fk_entity' => NULL, | |
108 | 'serialize' => 1, | |
19b53e5b C |
109 | ], |
110 | [ | |
19b53e5b C |
111 | 'custom_group' => $group, |
112 | 'name' => $textField, | |
113 | 'title' => $textField, | |
114 | 'entity' => "Custom_$group", | |
115 | 'data_type' => 'String', | |
116 | 'fk_entity' => NULL, | |
3766bd36 | 117 | 'serialize' => NULL, |
19b53e5b C |
118 | ], |
119 | [ | |
120 | 'name' => 'id', | |
121 | 'title' => ts('Custom Value ID'), | |
122 | 'entity' => "Custom_$group", | |
123 | 'data_type' => 'Integer', | |
124 | 'fk_entity' => NULL, | |
125 | ], | |
126 | [ | |
127 | 'name' => 'entity_id', | |
128 | 'title' => ts('Entity ID'), | |
129 | 'entity' => "Custom_$group", | |
130 | 'data_type' => 'Integer', | |
131 | 'fk_entity' => 'Contact', | |
132 | ], | |
133 | ]; | |
134 | ||
135 | foreach ($expectedResult as $key => $field) { | |
136 | foreach ($field as $attr => $value) { | |
137 | $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]); | |
138 | } | |
139 | } | |
140 | ||
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) | |
3766bd36 | 144 | ->addValue($colorField, 'g') |
19b53e5b C |
145 | ->addValue("entity_id", $this->contactID) |
146 | ->execute(); | |
147 | CustomValue::create($group) | |
3766bd36 | 148 | ->addValue($colorField, 'r') |
19b53e5b C |
149 | ->addValue("entity_id", $this->contactID) |
150 | ->execute(); | |
151 | // fetch custom values using API4 CustomValue::get | |
152 | $result = CustomValue::get($group)->execute(); | |
153 | ||
154 | // check if two custom values are created | |
155 | $this->assertEquals(2, count($result)); | |
156 | $expectedResult = [ | |
157 | [ | |
158 | 'id' => 1, | |
3766bd36 | 159 | $colorField => 'g', |
19b53e5b C |
160 | 'entity_id' => $this->contactID, |
161 | ], | |
162 | [ | |
163 | 'id' => 2, | |
3766bd36 | 164 | $colorField => 'r', |
19b53e5b C |
165 | 'entity_id' => $this->contactID, |
166 | ], | |
167 | ]; | |
168 | // match the data | |
169 | foreach ($expectedResult as $key => $field) { | |
170 | foreach ($field as $attr => $value) { | |
171 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
172 | } | |
173 | } | |
174 | ||
175 | // CASE 2: Test CustomValue::update | |
3766bd36 | 176 | // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green' |
19b53e5b C |
177 | CustomValue::update($group) |
178 | ->addWhere("id", "=", 1) | |
3766bd36 | 179 | ->addValue($colorField, 'b') |
19b53e5b C |
180 | ->execute(); |
181 | ||
182 | // ensure that the value is changed for id = 1 | |
183 | $color = CustomValue::get($group) | |
184 | ->addWhere("id", "=", 1) | |
185 | ->execute() | |
186 | ->first()[$colorField]; | |
3766bd36 | 187 | $this->assertEquals('b', $color); |
19b53e5b C |
188 | |
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') | |
196 | ->execute() | |
197 | ->first()['id']; | |
198 | // Replace all the records which was created earlier with entity_id = first contact | |
3766bd36 | 199 | // with custom record [$colorField => 'g', 'entity_id' => $secondContactID] |
19b53e5b | 200 | CustomValue::replace($group) |
3766bd36 | 201 | ->setRecords([[$colorField => 'g', $multiField => ['r', 'g'], 'entity_id' => $secondContactID]]) |
19b53e5b C |
202 | ->addWhere('entity_id', '=', $this->contactID) |
203 | ->execute(); | |
204 | ||
205 | // Check the two records created earlier is replaced by new contact | |
206 | $result = CustomValue::get($group)->execute(); | |
207 | $this->assertEquals(1, count($result)); | |
208 | ||
209 | $expectedResult = [ | |
210 | [ | |
211 | 'id' => 3, | |
3766bd36 CW |
212 | $colorField => 'g', |
213 | $multiField => ['r', 'g'], | |
19b53e5b C |
214 | 'entity_id' => $secondContactID, |
215 | ], | |
216 | ]; | |
217 | foreach ($expectedResult as $key => $field) { | |
218 | foreach ($field as $attr => $value) { | |
219 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
220 | } | |
221 | } | |
222 | ||
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)); | |
229 | } | |
230 | ||
231 | } |