Merge pull request #17943 from jitendrapurohit/core-1906
[civicrm-core.git] / tests / phpunit / api / v4 / Action / PseudoconstantTest.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 */
18
19
20 namespace api\v4\Action;
21
22 use Civi\Api4\Address;
23 use Civi\Api4\Contact;
24 use Civi\Api4\Activity;
25 use Civi\Api4\CustomField;
26 use Civi\Api4\CustomGroup;
27 use Civi\Api4\Email;
28 use Civi\Api4\EntityTag;
29 use Civi\Api4\OptionValue;
30 use Civi\Api4\Tag;
31
32 /**
33 * @group headless
34 */
35 class PseudoconstantTest extends BaseCustomValueTest {
36
37 public function testOptionValue() {
38 $cid = Contact::create(FALSE)->addValue('first_name', 'bill')->execute()->first()['id'];
39 $subject = uniqid('subject');
40 OptionValue::create()
41 ->addValue('option_group_id:name', 'activity_type')
42 ->addValue('label', 'Fake Type')
43 ->execute();
44
45 $options = Activity::getFields()
46 ->addWhere('name', '=', 'activity_type_id')
47 ->setLoadOptions(['id', 'name', 'label'])
48 ->execute()->first()['options'];
49 $options = array_column($options, NULL, 'name');
50 $this->assertEquals('Fake Type', $options['Fake_Type']['label']);
51
52 Activity::create()
53 ->addValue('activity_type_id:name', 'Meeting')
54 ->addValue('source_contact_id', $cid)
55 ->addValue('subject', $subject)
56 ->execute();
57
58 Activity::create()
59 ->addValue('activity_type_id:name', 'Fake_Type')
60 ->addValue('source_contact_id', $cid)
61 ->addValue('subject', $subject)
62 ->execute();
63
64 $act = Activity::get()
65 ->addWhere('activity_type_id:label', '=', 'Fake Type')
66 ->addWhere('subject', '=', $subject)
67 ->addSelect('activity_type_id:name')
68 ->addSelect('activity_type_id:label')
69 ->addSelect('activity_type_id')
70 ->execute();
71
72 $this->assertCount(1, $act);
73 $this->assertEquals('Fake Type', $act[0]['activity_type_id:label']);
74 $this->assertEquals('Fake_Type', $act[0]['activity_type_id:name']);
75 $this->assertTrue(is_numeric($act[0]['activity_type_id']));
76
77 $act = Activity::get()
78 ->addHaving('activity_type_id:name', '=', 'Fake_Type')
79 ->addHaving('subject', '=', $subject)
80 ->addSelect('activity_type_id:label')
81 ->addSelect('activity_type_id')
82 ->addSelect('subject')
83 ->execute();
84
85 $this->assertCount(1, $act);
86 $this->assertEquals('Fake Type', $act[0]['activity_type_id:label']);
87 $this->assertTrue(is_numeric($act[0]['activity_type_id']));
88
89 $act = Activity::get()
90 ->addHaving('activity_type_id:name', '=', 'Fake_Type')
91 ->addHaving('subject', '=', $subject)
92 ->addSelect('activity_type_id')
93 ->addSelect('subject')
94 ->execute();
95
96 $this->assertCount(1, $act);
97 $this->assertTrue(is_numeric($act[0]['activity_type_id']));
98 }
99
100 public function testAddressOptions() {
101 $cid = Contact::create(FALSE)->addValue('first_name', 'addr')->execute()->first()['id'];
102 Address::save()
103 ->addRecord([
104 'contact_id' => $cid,
105 'state_province_id:abbr' => 'CA',
106 'country_id:label' => 'United States',
107 'street_address' => '1',
108 ])
109 ->addRecord([
110 'contact_id' => $cid,
111 'state_province_id:abbr' => 'CA',
112 'country_id:label' => 'Uruguay',
113 'street_address' => '2',
114 ])
115 ->addRecord([
116 'contact_id' => $cid,
117 'state_province_id:abbr' => 'CA',
118 'country_id:abbr' => 'ES',
119 'street_address' => '3',
120 ])
121 ->execute();
122
123 $addr = Address::get()
124 ->addWhere('contact_id', '=', $cid)
125 ->addSelect('state_province_id:abbr', 'state_province_id:name', 'country_id:label', 'country_id:name')
126 ->addOrderBy('street_address')
127 ->execute();
128
129 $this->assertCount(3, $addr);
130
131 // US - California
132 $this->assertEquals('CA', $addr[0]['state_province_id:abbr']);
133 $this->assertEquals('California', $addr[0]['state_province_id:name']);
134 $this->assertEquals('US', $addr[0]['country_id:name']);
135 $this->assertEquals('United States', $addr[0]['country_id:label']);
136 // Uruguay - Canelones
137 $this->assertEquals('CA', $addr[1]['state_province_id:abbr']);
138 $this->assertEquals('Canelones', $addr[1]['state_province_id:name']);
139 $this->assertEquals('UY', $addr[1]['country_id:name']);
140 $this->assertEquals('Uruguay', $addr[1]['country_id:label']);
141 // Spain - Cádiz
142 $this->assertEquals('CA', $addr[2]['state_province_id:abbr']);
143 $this->assertEquals('Cádiz', $addr[2]['state_province_id:name']);
144 $this->assertEquals('ES', $addr[2]['country_id:name']);
145 $this->assertEquals('Spain', $addr[2]['country_id:label']);
146 }
147
148 public function testCustomOptions() {
149 $technicolor = [
150 ['id' => 'r', 'name' => 'red', 'label' => 'RED', 'color' => '#ff0000', 'description' => 'Red color', 'icon' => 'fa-red'],
151 ['id' => 'g', 'name' => 'green', 'label' => 'GREEN', 'color' => '#00ff00', 'description' => 'Green color', 'icon' => 'fa-green'],
152 ['id' => 'b', 'name' => 'blue', 'label' => 'BLUE', 'color' => '#0000ff', 'description' => 'Blue color', 'icon' => 'fa-blue'],
153 ];
154
155 CustomGroup::create(FALSE)
156 ->addValue('name', 'myPseudoconstantTest')
157 ->addValue('extends', 'Individual')
158 ->addChain('field1', CustomField::create()
159 ->addValue('custom_group_id', '$id')
160 ->addValue('option_values', ['r' => 'red', 'g' => 'green', 'b' => 'blü'])
161 ->addValue('label', 'Color')
162 ->addValue('html_type', 'Select')
163 )->addChain('field2', CustomField::create()
164 ->addValue('custom_group_id', '$id')
165 ->addValue('option_values', $technicolor)
166 ->addValue('label', 'Technicolor')
167 ->addValue('html_type', 'CheckBox')
168 )->execute();
169
170 $fields = Contact::getFields()
171 ->setLoadOptions(array_keys($technicolor[0]))
172 ->setIncludeCustom(TRUE)
173 ->execute()
174 ->indexBy('name');
175
176 foreach ($technicolor as $index => $option) {
177 foreach ($option as $prop => $val) {
178 $this->assertEquals($val, $fields['myPseudoconstantTest.Technicolor']['options'][$index][$prop]);
179 }
180 }
181
182 $cid = Contact::create(FALSE)
183 ->addValue('first_name', 'col')
184 ->addValue('myPseudoconstantTest.Color:label', 'blü')
185 ->execute()->first()['id'];
186
187 $result = Contact::get(FALSE)
188 ->addWhere('id', '=', $cid)
189 ->addSelect('myPseudoconstantTest.Color:name', 'myPseudoconstantTest.Color:label', 'myPseudoconstantTest.Color')
190 ->execute()->first();
191
192 $this->assertEquals('blü', $result['myPseudoconstantTest.Color:label']);
193 $this->assertEquals('bl_', $result['myPseudoconstantTest.Color:name']);
194 $this->assertEquals('b', $result['myPseudoconstantTest.Color']);
195
196 $cid1 = Contact::create(FALSE)
197 ->addValue('first_name', 'two')
198 ->addValue('myPseudoconstantTest.Technicolor:label', 'RED')
199 ->execute()->first()['id'];
200 $cid2 = Contact::create(FALSE)
201 ->addValue('first_name', 'two')
202 ->addValue('myPseudoconstantTest.Technicolor:label', 'GREEN')
203 ->execute()->first()['id'];
204
205 // Test ordering by label
206 $result = Contact::get(FALSE)
207 ->addWhere('id', 'IN', [$cid1, $cid2])
208 ->addSelect('id')
209 ->addOrderBy('myPseudoconstantTest.Technicolor:label')
210 ->execute()->first()['id'];
211 $this->assertEquals($cid2, $result);
212 $result = Contact::get(FALSE)
213 ->addWhere('id', 'IN', [$cid1, $cid2])
214 ->addSelect('id')
215 ->addOrderBy('myPseudoconstantTest.Technicolor:label', 'DESC')
216 ->execute()->first()['id'];
217 $this->assertEquals($cid1, $result);
218 }
219
220 public function testJoinOptions() {
221 $cid1 = Contact::create(FALSE)
222 ->addValue('first_name', 'Tom')
223 ->addValue('gender_id:label', 'Male')
224 ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'tom@example.com', 'location_type_id:name' => 'Work']))
225 ->execute()->first()['id'];
226 $cid2 = Contact::create(FALSE)
227 ->addValue('first_name', 'Sue')
228 ->addValue('gender_id:name', 'Female')
229 ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'sue@example.com', 'location_type_id:name' => 'Home']))
230 ->execute()->first()['id'];
231 $cid3 = Contact::create(FALSE)
232 ->addValue('first_name', 'Pat')
233 ->addChain('email', Email::create()->setValues(['contact_id' => '$id', 'email' => 'pat@example.com', 'location_type_id:name' => 'Home']))
234 ->execute()->first()['id'];
235
236 $emails = Email::get()
237 ->addSelect('location_type_id:name', 'contact.gender_id:label', 'email', 'contact_id')
238 ->addWhere('contact_id', 'IN', [$cid1, $cid2, $cid3])
239 ->addWhere('contact.gender_id:label', 'IN', ['Male', 'Female'])
240 ->execute()->indexBy('contact_id');
241 $this->assertCount(2, $emails);
242 $this->assertEquals('Work', $emails[$cid1]['location_type_id:name']);
243 $this->assertEquals('Home', $emails[$cid2]['location_type_id:name']);
244 $this->assertEquals('Male', $emails[$cid1]['contact.gender_id:label']);
245 $this->assertEquals('Female', $emails[$cid2]['contact.gender_id:label']);
246
247 $emails = Email::get()
248 ->addSelect('location_type_id:name', 'contact.gender_id:label', 'email', 'contact_id')
249 ->addWhere('contact_id', 'IN', [$cid1, $cid2, $cid3])
250 ->addWhere('location_type_id:name', 'IN', ['Home'])
251 ->execute()->indexBy('contact_id');
252 $this->assertCount(2, $emails);
253 $this->assertEquals('Home', $emails[$cid2]['location_type_id:name']);
254 $this->assertEquals('Home', $emails[$cid3]['location_type_id:name']);
255 $this->assertEquals('Female', $emails[$cid2]['contact.gender_id:label']);
256 $this->assertNull($emails[$cid3]['contact.gender_id:label']);
257 }
258
259 public function testTagOptions() {
260 $tag = uniqid('tag');
261 Tag::create(FALSE)
262 ->addValue('name', $tag)
263 ->addValue('description', 'colorful')
264 ->addValue('color', '#aabbcc')
265 ->execute();
266 $options = EntityTag::getFields()
267 ->setLoadOptions(['id', 'name', 'color', 'description', 'label'])
268 ->addWhere('name', '=', 'tag_id')
269 ->execute()->first()['options'];
270 $options = array_column($options, NULL, 'name');
271 $this->assertEquals('colorful', $options[$tag]['description']);
272 $this->assertEquals('#aabbcc', $options[$tag]['color']);
273 $this->assertEquals($tag, $options[$tag]['label']);
274 }
275
276 }