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