Merge pull request #16733 from eileenmcnaughton/smarty
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CustomValueTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
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');
44 $multiField = uniqid('chkbx');
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('option_values', $optionValues)
59 ->addValue('custom_group_id', $customGroup['id'])
60 ->addValue('html_type', 'Select')
61 ->addValue('data_type', 'String')
62 ->execute();
63
64 CustomField::create()
65 ->setCheckPermissions(FALSE)
66 ->addValue('label', $multiField)
67 ->addValue('option_values', $optionValues)
68 ->addValue('custom_group_id', $customGroup['id'])
69 ->addValue('html_type', 'CheckBox')
70 ->addValue('data_type', 'String')
71 ->execute();
72
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)->setLoadOptions(TRUE)->setCheckPermissions(FALSE)->execute();
91 $expectedResult = [
92 [
93 'custom_group' => $group,
94 'name' => $colorField,
95 'title' => $colorField,
96 'entity' => "Custom_$group",
97 'data_type' => 'String',
98 'fk_entity' => NULL,
99 'serialize' => NULL,
100 'options' => $optionValues,
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,
110 'options' => $optionValues,
111 ],
112 [
113 'custom_group' => $group,
114 'name' => $textField,
115 'title' => $textField,
116 'entity' => "Custom_$group",
117 'data_type' => 'String',
118 'fk_entity' => NULL,
119 'serialize' => NULL,
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)
146 ->addValue($colorField, 'g')
147 ->addValue("entity_id", $this->contactID)
148 ->execute();
149 CustomValue::create($group)
150 ->addValue($colorField, 'r')
151 ->addValue("entity_id", $this->contactID)
152 ->execute();
153 // fetch custom values using API4 CustomValue::get
154 $result = CustomValue::get($group)
155 ->addSelect('id', 'entity_id', $colorField, $colorField . ':label')
156 ->addOrderBy($colorField, 'DESC')
157 ->execute();
158
159 // check if two custom values are created
160 $this->assertEquals(2, count($result));
161 $expectedResult = [
162 [
163 'id' => 2,
164 $colorField => 'r',
165 $colorField . ':label' => 'Red',
166 'entity_id' => $this->contactID,
167 ],
168 [
169 'id' => 1,
170 $colorField => 'g',
171 $colorField . ':label' => 'Green',
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
183 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
184 CustomValue::update($group)
185 ->addWhere("id", "=", 1)
186 ->addValue($colorField . ':label', 'Blue')
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];
194 $this->assertEquals('b', $color);
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
206 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
207 CustomValue::replace($group)
208 ->setRecords([[$colorField => 'g', $multiField . ':label' => ['Red', 'Green'], 'entity_id' => $secondContactID]])
209 ->addWhere('entity_id', '=', $this->contactID)
210 ->execute();
211
212 // Check the two records created earlier is replaced by new contact
213 $result = CustomValue::get($group)
214 ->addSelect('id', 'entity_id', $colorField, $colorField . ':label', $multiField, $multiField . ':label')
215 ->execute();
216 $this->assertEquals(1, count($result));
217
218 $expectedResult = [
219 [
220 'id' => 3,
221 $colorField => 'g',
222 $colorField . ':label' => 'Green',
223 $multiField => ['r', 'g'],
224 $multiField . ':label' => ['Red', 'Green'],
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 }