Merge pull request #17163 from jitendrapurohit/core-1731
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CreateWithOptionGroupTest.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\Contact;
27
28 /**
29 * @group headless
30 */
31 class CreateWithOptionGroupTest extends BaseCustomValueTest {
32
33 /**
34 * Remove the custom tables
35 */
36 public function setUp() {
37 $this->dropByPrefix('civicrm_value_financial');
38 $this->dropByPrefix('civicrm_value_favorite');
39 parent::setUp();
40 }
41
42 public function testGetWithCustomData() {
43 $group = uniqid('fava');
44 $colorField = uniqid('colora');
45 $foodField = uniqid('fooda');
46
47 $customGroupId = CustomGroup::create()
48 ->setCheckPermissions(FALSE)
49 ->addValue('name', $group)
50 ->addValue('extends', 'Contact')
51 ->execute()
52 ->first()['id'];
53
54 CustomField::create()
55 ->setCheckPermissions(FALSE)
56 ->addValue('label', $colorField)
57 ->addValue('name', $colorField)
58 ->addValue('option_values', ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'])
59 ->addValue('custom_group_id', $customGroupId)
60 ->addValue('html_type', 'Select')
61 ->addValue('data_type', 'String')
62 ->execute();
63
64 CustomField::create()
65 ->setCheckPermissions(FALSE)
66 ->addValue('label', $foodField)
67 ->addValue('name', $foodField)
68 ->addValue('option_values', ['1' => 'Corn', '2' => 'Potatoes', '3' => 'Cheese'])
69 ->addValue('custom_group_id', $customGroupId)
70 ->addValue('html_type', 'Select')
71 ->addValue('data_type', 'String')
72 ->execute();
73
74 $customGroupId = CustomGroup::create()
75 ->setCheckPermissions(FALSE)
76 ->addValue('name', 'FinancialStuff')
77 ->addValue('extends', 'Contact')
78 ->execute()
79 ->first()['id'];
80
81 CustomField::create()
82 ->setCheckPermissions(FALSE)
83 ->addValue('label', 'Salary')
84 ->addValue('custom_group_id', $customGroupId)
85 ->addValue('html_type', 'Number')
86 ->addValue('data_type', 'Money')
87 ->execute();
88
89 Contact::create()
90 ->setCheckPermissions(FALSE)
91 ->addValue('first_name', 'Jerome')
92 ->addValue('last_name', 'Tester')
93 ->addValue('contact_type', 'Individual')
94 ->addValue("$group.$colorField", 'r')
95 ->addValue("$group.$foodField", '1')
96 ->addValue('FinancialStuff.Salary', 50000)
97 ->execute();
98
99 $result = Contact::get()
100 ->setCheckPermissions(FALSE)
101 ->addSelect('first_name')
102 ->addSelect("$group.$colorField:label")
103 ->addSelect("$group.$foodField:label")
104 ->addSelect('FinancialStuff.Salary')
105 ->addWhere("$group.$foodField:label", 'IN', ['Corn', 'Potatoes'])
106 ->addWhere('FinancialStuff.Salary', '>', '10000')
107 ->execute()
108 ->first();
109
110 $this->assertEquals('Red', $result["$group.$colorField:label"]);
111 $this->assertEquals('Corn', $result["$group.$foodField:label"]);
112 $this->assertEquals(50000, $result['FinancialStuff.Salary']);
113 }
114
115 public function testWithCustomDataForMultipleContacts() {
116 $group = uniqid('favb');
117 $colorField = uniqid('colorb');
118 $foodField = uniqid('foodb');
119
120 $customGroupId = CustomGroup::create()
121 ->setCheckPermissions(FALSE)
122 ->addValue('name', $group)
123 ->addValue('extends', 'Contact')
124 ->execute()
125 ->first()['id'];
126
127 CustomField::create()
128 ->setCheckPermissions(FALSE)
129 ->addValue('label', $colorField)
130 ->addValue('name', $colorField)
131 ->addValue('option_values', ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'])
132 ->addValue('custom_group_id', $customGroupId)
133 ->addValue('html_type', 'Select')
134 ->addValue('data_type', 'String')
135 ->execute();
136
137 CustomField::create()
138 ->setCheckPermissions(FALSE)
139 ->addValue('label', $foodField)
140 ->addValue('name', $foodField)
141 ->addValue('option_values', ['1' => 'Corn', '2' => 'Potatoes', '3' => 'Cheese'])
142 ->addValue('custom_group_id', $customGroupId)
143 ->addValue('html_type', 'Select')
144 ->addValue('data_type', 'String')
145 ->execute();
146
147 $customGroupId = CustomGroup::create()
148 ->setCheckPermissions(FALSE)
149 ->addValue('name', 'FinancialStuff')
150 ->addValue('extends', 'Contact')
151 ->execute()
152 ->first()['id'];
153
154 CustomField::create()
155 ->setCheckPermissions(FALSE)
156 ->addValue('label', 'Salary')
157 ->addValue('custom_group_id', $customGroupId)
158 ->addValue('html_type', 'Number')
159 ->addValue('data_type', 'Money')
160 ->execute();
161
162 Contact::create()
163 ->setCheckPermissions(FALSE)
164 ->addValue('first_name', 'Red')
165 ->addValue('last_name', 'Corn')
166 ->addValue('contact_type', 'Individual')
167 ->addValue("$group.$colorField", 'r')
168 ->addValue("$group.$foodField", '1')
169 ->addValue('FinancialStuff.Salary', 10000)
170 ->execute();
171
172 Contact::create()
173 ->setCheckPermissions(FALSE)
174 ->addValue('first_name', 'Blue')
175 ->addValue('last_name', 'Cheese')
176 ->addValue('contact_type', 'Individual')
177 ->addValue("$group.$colorField", 'b')
178 ->addValue("$group.$foodField", '3')
179 ->addValue('FinancialStuff.Salary', 500000)
180 ->execute();
181
182 $result = Contact::get()
183 ->setCheckPermissions(FALSE)
184 ->addSelect('first_name')
185 ->addSelect('last_name')
186 ->addSelect("$group.$colorField:label")
187 ->addSelect("$group.$foodField:label")
188 ->addSelect('FinancialStuff.Salary')
189 ->addWhere("$group.$foodField:label", 'IN', ['Corn', 'Cheese'])
190 ->execute();
191
192 $blueCheese = NULL;
193 foreach ($result as $contact) {
194 if ($contact['first_name'] === 'Blue') {
195 $blueCheese = $contact;
196 }
197 }
198
199 $this->assertEquals('Blue', $blueCheese["$group.$colorField:label"]);
200 $this->assertEquals('Cheese', $blueCheese["$group.$foodField:label"]);
201 $this->assertEquals(500000, $blueCheese['FinancialStuff.Salary']);
202 }
203
204 }