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'); | |
44 | $textField = uniqid('txt'); | |
45 | ||
46 | $customGroup = CustomGroup::create() | |
47 | ->setCheckPermissions(FALSE) | |
48 | ->addValue('name', $group) | |
49 | ->addValue('extends', 'Contact') | |
50 | ->addValue('is_multiple', TRUE) | |
51 | ->execute() | |
52 | ->first(); | |
53 | ||
54 | CustomField::create() | |
55 | ->setCheckPermissions(FALSE) | |
56 | ->addValue('label', $colorField) | |
57 | ->addValue('options', $optionValues) | |
58 | ->addValue('custom_group_id', $customGroup['id']) | |
59 | ->addValue('html_type', 'Select') | |
60 | ->addValue('data_type', 'String') | |
61 | ->execute(); | |
62 | ||
63 | CustomField::create() | |
64 | ->setCheckPermissions(FALSE) | |
65 | ->addValue('label', $textField) | |
66 | ->addValue('custom_group_id', $customGroup['id']) | |
67 | ->addValue('html_type', 'Text') | |
68 | ->addValue('data_type', 'String') | |
69 | ->execute(); | |
70 | ||
71 | $this->contactID = Contact::create() | |
72 | ->setCheckPermissions(FALSE) | |
73 | ->addValue('first_name', 'Johann') | |
74 | ->addValue('last_name', 'Tester') | |
75 | ->addValue('contact_type', 'Individual') | |
76 | ->execute() | |
77 | ->first()['id']; | |
78 | ||
79 | // Retrieve and check the fields of CustomValue = Custom_$group | |
80 | $fields = CustomValue::getFields($group)->execute(); | |
81 | $expectedResult = [ | |
82 | [ | |
83 | 'custom_field_id' => 1, | |
84 | 'custom_group' => $group, | |
85 | 'name' => $colorField, | |
86 | 'title' => $colorField, | |
87 | 'entity' => "Custom_$group", | |
88 | 'data_type' => 'String', | |
89 | 'fk_entity' => NULL, | |
90 | ], | |
91 | [ | |
92 | 'custom_field_id' => 2, | |
93 | 'custom_group' => $group, | |
94 | 'name' => $textField, | |
95 | 'title' => $textField, | |
96 | 'entity' => "Custom_$group", | |
97 | 'data_type' => 'String', | |
98 | 'fk_entity' => NULL, | |
99 | ], | |
100 | [ | |
101 | 'name' => 'id', | |
102 | 'title' => ts('Custom Value ID'), | |
103 | 'entity' => "Custom_$group", | |
104 | 'data_type' => 'Integer', | |
105 | 'fk_entity' => NULL, | |
106 | ], | |
107 | [ | |
108 | 'name' => 'entity_id', | |
109 | 'title' => ts('Entity ID'), | |
110 | 'entity' => "Custom_$group", | |
111 | 'data_type' => 'Integer', | |
112 | 'fk_entity' => 'Contact', | |
113 | ], | |
114 | ]; | |
115 | ||
116 | foreach ($expectedResult as $key => $field) { | |
117 | foreach ($field as $attr => $value) { | |
118 | $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]); | |
119 | } | |
120 | } | |
121 | ||
122 | // CASE 1: Test CustomValue::create | |
123 | // Create two records for a single contact and using CustomValue::get ensure that two records are created | |
124 | CustomValue::create($group) | |
125 | ->addValue($colorField, 'Green') | |
126 | ->addValue("entity_id", $this->contactID) | |
127 | ->execute(); | |
128 | CustomValue::create($group) | |
129 | ->addValue($colorField, 'Red') | |
130 | ->addValue("entity_id", $this->contactID) | |
131 | ->execute(); | |
132 | // fetch custom values using API4 CustomValue::get | |
133 | $result = CustomValue::get($group)->execute(); | |
134 | ||
135 | // check if two custom values are created | |
136 | $this->assertEquals(2, count($result)); | |
137 | $expectedResult = [ | |
138 | [ | |
139 | 'id' => 1, | |
140 | $colorField => 'Green', | |
141 | 'entity_id' => $this->contactID, | |
142 | ], | |
143 | [ | |
144 | 'id' => 2, | |
145 | $colorField => 'Red', | |
146 | 'entity_id' => $this->contactID, | |
147 | ], | |
148 | ]; | |
149 | // match the data | |
150 | foreach ($expectedResult as $key => $field) { | |
151 | foreach ($field as $attr => $value) { | |
152 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
153 | } | |
154 | } | |
155 | ||
156 | // CASE 2: Test CustomValue::update | |
157 | // Update a records whose id is 1 and change the custom field (name = Color) value to 'White' from 'Green' | |
158 | CustomValue::update($group) | |
159 | ->addWhere("id", "=", 1) | |
160 | ->addValue($colorField, 'White') | |
161 | ->execute(); | |
162 | ||
163 | // ensure that the value is changed for id = 1 | |
164 | $color = CustomValue::get($group) | |
165 | ->addWhere("id", "=", 1) | |
166 | ->execute() | |
167 | ->first()[$colorField]; | |
168 | $this->assertEquals('White', $color); | |
169 | ||
170 | // CASE 3: Test CustomValue::replace | |
171 | // create a second contact which will be used to replace the custom values, created earlier | |
172 | $secondContactID = Contact::create() | |
173 | ->setCheckPermissions(FALSE) | |
174 | ->addValue('first_name', 'Adam') | |
175 | ->addValue('last_name', 'Tester') | |
176 | ->addValue('contact_type', 'Individual') | |
177 | ->execute() | |
178 | ->first()['id']; | |
179 | // Replace all the records which was created earlier with entity_id = first contact | |
180 | // with custom record [$colorField => 'Rainbow', 'entity_id' => $secondContactID] | |
181 | CustomValue::replace($group) | |
182 | ->setRecords([[$colorField => 'Rainbow', 'entity_id' => $secondContactID]]) | |
183 | ->addWhere('entity_id', '=', $this->contactID) | |
184 | ->execute(); | |
185 | ||
186 | // Check the two records created earlier is replaced by new contact | |
187 | $result = CustomValue::get($group)->execute(); | |
188 | $this->assertEquals(1, count($result)); | |
189 | ||
190 | $expectedResult = [ | |
191 | [ | |
192 | 'id' => 3, | |
193 | $colorField => 'Rainbow', | |
194 | 'entity_id' => $secondContactID, | |
195 | ], | |
196 | ]; | |
197 | foreach ($expectedResult as $key => $field) { | |
198 | foreach ($field as $attr => $value) { | |
199 | $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]); | |
200 | } | |
201 | } | |
202 | ||
203 | // CASE 4: Test CustomValue::delete | |
204 | // There is only record left whose id = 3, delete that record on basis of criteria id = 3 | |
205 | CustomValue::delete($group)->addWhere("id", "=", 3)->execute(); | |
206 | $result = CustomValue::get($group)->execute(); | |
207 | // check that there are no custom values present | |
208 | $this->assertEquals(0, count($result)); | |
209 | } | |
210 | ||
211 | } |