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) | |
961e974c | 58 | ->addValue('option_values', $optionValues) |
19b53e5b C |
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) | |
961e974c | 67 | ->addValue('option_values', $optionValues) |
3766bd36 CW |
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 | |
961e974c | 90 | $fields = CustomValue::getFields($group)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute(); |
19b53e5b C |
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 | 99 | 'serialize' => NULL, |
961e974c | 100 | 'options' => $optionValues, |
3766bd36 CW |
101 | ], |
102 | [ | |
103 | 'custom_group' => $group, | |
104 | 'name' => $multiField, | |
105 | 'title' => $multiField, | |
106 | 'entity' => "Custom_$group", | |
107 | 'data_type' => 'String', | |
108 | 'fk_entity' => NULL, | |
109 | 'serialize' => 1, | |
961e974c | 110 | 'options' => $optionValues, |
19b53e5b C |
111 | ], |
112 | [ | |
19b53e5b C |
113 | 'custom_group' => $group, |
114 | 'name' => $textField, | |
115 | 'title' => $textField, | |
116 | 'entity' => "Custom_$group", | |
117 | 'data_type' => 'String', | |
118 | 'fk_entity' => NULL, | |
3766bd36 | 119 | 'serialize' => NULL, |
19b53e5b C |
120 | ], |
121 | [ | |
122 | 'name' => 'id', | |
123 | 'title' => ts('Custom Value ID'), | |
124 | 'entity' => "Custom_$group", | |
125 | 'data_type' => 'Integer', | |
126 | 'fk_entity' => NULL, | |
127 | ], | |
128 | [ | |
129 | 'name' => 'entity_id', | |
130 | 'title' => ts('Entity ID'), | |
131 | 'entity' => "Custom_$group", | |
132 | 'data_type' => 'Integer', | |
133 | 'fk_entity' => 'Contact', | |
134 | ], | |
135 | ]; | |
136 | ||
137 | foreach ($expectedResult as $key => $field) { | |
138 | foreach ($field as $attr => $value) { | |
139 | $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]); | |
140 | } | |
141 | } | |
142 | ||
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) | |
3766bd36 | 146 | ->addValue($colorField, 'g') |
19b53e5b C |
147 | ->addValue("entity_id", $this->contactID) |
148 | ->execute(); | |
149 | CustomValue::create($group) | |
3766bd36 | 150 | ->addValue($colorField, 'r') |
19b53e5b C |
151 | ->addValue("entity_id", $this->contactID) |
152 | ->execute(); | |
153 | // fetch custom values using API4 CustomValue::get | |
822e499f | 154 | $result = CustomValue::get($group) |
961e974c | 155 | ->addSelect('id', 'entity_id', $colorField, $colorField . ':label') |
822e499f CW |
156 | ->addOrderBy($colorField, 'DESC') |
157 | ->execute(); | |
19b53e5b C |
158 | |
159 | // check if two custom values are created | |
160 | $this->assertEquals(2, count($result)); | |
161 | $expectedResult = [ | |
162 | [ | |
822e499f CW |
163 | 'id' => 2, |
164 | $colorField => 'r', | |
961e974c | 165 | $colorField . ':label' => 'Red', |
19b53e5b C |
166 | 'entity_id' => $this->contactID, |
167 | ], | |
168 | [ | |
822e499f CW |
169 | 'id' => 1, |
170 | $colorField => 'g', | |
961e974c | 171 | $colorField . ':label' => 'Green', |
19b53e5b C |
172 | 'entity_id' => $this->contactID, |
173 | ], | |
174 | ]; | |
175 | // match the data | |
176 | foreach ($expectedResult as $key => $field) { | |
177 | foreach ($field as $attr => $value) { | |
178 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
179 | } | |
180 | } | |
181 | ||
182 | // CASE 2: Test CustomValue::update | |
3766bd36 | 183 | // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green' |
19b53e5b C |
184 | CustomValue::update($group) |
185 | ->addWhere("id", "=", 1) | |
961e974c | 186 | ->addValue($colorField . ':label', 'Blue') |
19b53e5b C |
187 | ->execute(); |
188 | ||
189 | // ensure that the value is changed for id = 1 | |
190 | $color = CustomValue::get($group) | |
191 | ->addWhere("id", "=", 1) | |
192 | ->execute() | |
193 | ->first()[$colorField]; | |
3766bd36 | 194 | $this->assertEquals('b', $color); |
19b53e5b C |
195 | |
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') | |
203 | ->execute() | |
204 | ->first()['id']; | |
205 | // Replace all the records which was created earlier with entity_id = first contact | |
3766bd36 | 206 | // with custom record [$colorField => 'g', 'entity_id' => $secondContactID] |
19b53e5b | 207 | CustomValue::replace($group) |
961e974c | 208 | ->setRecords([[$colorField => 'g', $multiField . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]]) |
19b53e5b C |
209 | ->addWhere('entity_id', '=', $this->contactID) |
210 | ->execute(); | |
211 | ||
212 | // Check the two records created earlier is replaced by new contact | |
961e974c CW |
213 | $result = CustomValue::get($group) |
214 | ->addSelect('id', 'entity_id', $colorField, $colorField . ':label', $multiField, $multiField . ':label') | |
215 | ->execute(); | |
19b53e5b C |
216 | $this->assertEquals(1, count($result)); |
217 | ||
218 | $expectedResult = [ | |
219 | [ | |
220 | 'id' => 3, | |
3766bd36 | 221 | $colorField => 'g', |
961e974c | 222 | $colorField . ':label' => 'Green', |
3766bd36 | 223 | $multiField => ['r', 'g'], |
961e974c | 224 | $multiField . ':label' => ['Red', 'Green'], |
19b53e5b C |
225 | 'entity_id' => $secondContactID, |
226 | ], | |
227 | ]; | |
228 | foreach ($expectedResult as $key => $field) { | |
229 | foreach ($field as $attr => $value) { | |
230 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
231 | } | |
232 | } | |
233 | ||
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)); | |
240 | } | |
241 | ||
242 | } |