Merge pull request #16670 from demeritcowboy/audit-tpl-3
[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('options', $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('options', $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)->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 ],
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,
109 ],
110 [
111 'custom_group' => $group,
112 'name' => $textField,
113 'title' => $textField,
114 'entity' => "Custom_$group",
115 'data_type' => 'String',
116 'fk_entity' => NULL,
117 'serialize' => NULL,
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)
144 ->addValue($colorField, 'g')
145 ->addValue("entity_id", $this->contactID)
146 ->execute();
147 CustomValue::create($group)
148 ->addValue($colorField, 'r')
149 ->addValue("entity_id", $this->contactID)
150 ->execute();
151 // fetch custom values using API4 CustomValue::get
152 $result = CustomValue::get($group)
153 ->addOrderBy($colorField, 'DESC')
154 ->execute();
155
156 // check if two custom values are created
157 $this->assertEquals(2, count($result));
158 $expectedResult = [
159 [
160 'id' => 2,
161 $colorField => 'r',
162 'entity_id' => $this->contactID,
163 ],
164 [
165 'id' => 1,
166 $colorField => 'g',
167 'entity_id' => $this->contactID,
168 ],
169 ];
170 // match the data
171 foreach ($expectedResult as $key => $field) {
172 foreach ($field as $attr => $value) {
173 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
174 }
175 }
176
177 // CASE 2: Test CustomValue::update
178 // Update a records whose id is 1 and change the custom field (name = Color) value to 'Blue' from 'Green'
179 CustomValue::update($group)
180 ->addWhere("id", "=", 1)
181 ->addValue($colorField, 'b')
182 ->execute();
183
184 // ensure that the value is changed for id = 1
185 $color = CustomValue::get($group)
186 ->addWhere("id", "=", 1)
187 ->execute()
188 ->first()[$colorField];
189 $this->assertEquals('b', $color);
190
191 // CASE 3: Test CustomValue::replace
192 // create a second contact which will be used to replace the custom values, created earlier
193 $secondContactID = Contact::create()
194 ->setCheckPermissions(FALSE)
195 ->addValue('first_name', 'Adam')
196 ->addValue('last_name', 'Tester')
197 ->addValue('contact_type', 'Individual')
198 ->execute()
199 ->first()['id'];
200 // Replace all the records which was created earlier with entity_id = first contact
201 // with custom record [$colorField => 'g', 'entity_id' => $secondContactID]
202 CustomValue::replace($group)
203 ->setRecords([[$colorField => 'g', $multiField => ['r', 'g'], 'entity_id' => $secondContactID]])
204 ->addWhere('entity_id', '=', $this->contactID)
205 ->execute();
206
207 // Check the two records created earlier is replaced by new contact
208 $result = CustomValue::get($group)->execute();
209 $this->assertEquals(1, count($result));
210
211 $expectedResult = [
212 [
213 'id' => 3,
214 $colorField => 'g',
215 $multiField => ['r', 'g'],
216 'entity_id' => $secondContactID,
217 ],
218 ];
219 foreach ($expectedResult as $key => $field) {
220 foreach ($field as $attr => $value) {
221 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
222 }
223 }
224
225 // CASE 4: Test CustomValue::delete
226 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
227 CustomValue::delete($group)->addWhere("id", "=", 3)->execute();
228 $result = CustomValue::get($group)->execute();
229 // check that there are no custom values present
230 $this->assertEquals(0, count($result));
231 }
232
233 }