Merge pull request #15293 from ixiam/dev#issue-826
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CreateWithOptionGroupTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Action;
39
40 use Civi\Api4\CustomField;
41 use Civi\Api4\CustomGroup;
42 use Civi\Api4\Contact;
43
44 /**
45 * @group headless
46 */
47 class CreateWithOptionGroupTest extends BaseCustomValueTest {
48
49 /**
50 * Remove the custom tables
51 */
52 public function setUp() {
53 $this->dropByPrefix('civicrm_value_financial');
54 $this->dropByPrefix('civicrm_value_favorite');
55 parent::setUp();
56 }
57
58 public function testGetWithCustomData() {
59 $group = uniqid('fava');
60 $colorField = uniqid('colora');
61 $foodField = uniqid('fooda');
62
63 $customGroupId = CustomGroup::create()
64 ->setCheckPermissions(FALSE)
65 ->addValue('name', $group)
66 ->addValue('extends', 'Contact')
67 ->execute()
68 ->first()['id'];
69
70 CustomField::create()
71 ->setCheckPermissions(FALSE)
72 ->addValue('label', $colorField)
73 ->addValue('name', $colorField)
74 ->addValue('option_values', ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'])
75 ->addValue('custom_group_id', $customGroupId)
76 ->addValue('html_type', 'Select')
77 ->addValue('data_type', 'String')
78 ->execute();
79
80 CustomField::create()
81 ->setCheckPermissions(FALSE)
82 ->addValue('label', $foodField)
83 ->addValue('name', $foodField)
84 ->addValue('option_values', ['1' => 'Corn', '2' => 'Potatoes', '3' => 'Cheese'])
85 ->addValue('custom_group_id', $customGroupId)
86 ->addValue('html_type', 'Select')
87 ->addValue('data_type', 'String')
88 ->execute();
89
90 $customGroupId = CustomGroup::create()
91 ->setCheckPermissions(FALSE)
92 ->addValue('name', 'FinancialStuff')
93 ->addValue('extends', 'Contact')
94 ->execute()
95 ->first()['id'];
96
97 CustomField::create()
98 ->setCheckPermissions(FALSE)
99 ->addValue('label', 'Salary')
100 ->addValue('custom_group_id', $customGroupId)
101 ->addValue('html_type', 'Number')
102 ->addValue('data_type', 'Money')
103 ->execute();
104
105 Contact::create()
106 ->setCheckPermissions(FALSE)
107 ->addValue('first_name', 'Jerome')
108 ->addValue('last_name', 'Tester')
109 ->addValue('contact_type', 'Individual')
110 ->addValue("$group.$colorField", 'r')
111 ->addValue("$group.$foodField", '1')
112 ->addValue('FinancialStuff.Salary', 50000)
113 ->execute();
114
115 $result = Contact::get()
116 ->setCheckPermissions(FALSE)
117 ->addSelect('first_name')
118 ->addSelect("$group.$colorField.label")
119 ->addSelect("$group.$foodField.label")
120 ->addSelect('FinancialStuff.Salary')
121 ->addWhere("$group.$foodField.label", 'IN', ['Corn', 'Potatoes'])
122 ->addWhere('FinancialStuff.Salary', '>', '10000')
123 ->execute()
124 ->first();
125
126 $this->assertEquals('Red', $result["$group.$colorField.label"]);
127 $this->assertEquals('Corn', $result["$group.$foodField.label"]);
128 $this->assertEquals(50000, $result['FinancialStuff.Salary']);
129 }
130
131 public function testWithCustomDataForMultipleContacts() {
132 $group = uniqid('favb');
133 $colorField = uniqid('colorb');
134 $foodField = uniqid('foodb');
135
136 $customGroupId = CustomGroup::create()
137 ->setCheckPermissions(FALSE)
138 ->addValue('name', $group)
139 ->addValue('extends', 'Contact')
140 ->execute()
141 ->first()['id'];
142
143 CustomField::create()
144 ->setCheckPermissions(FALSE)
145 ->addValue('label', $colorField)
146 ->addValue('name', $colorField)
147 ->addValue('option_values', ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'])
148 ->addValue('custom_group_id', $customGroupId)
149 ->addValue('html_type', 'Select')
150 ->addValue('data_type', 'String')
151 ->execute();
152
153 CustomField::create()
154 ->setCheckPermissions(FALSE)
155 ->addValue('label', $foodField)
156 ->addValue('name', $foodField)
157 ->addValue('option_values', ['1' => 'Corn', '2' => 'Potatoes', '3' => 'Cheese'])
158 ->addValue('custom_group_id', $customGroupId)
159 ->addValue('html_type', 'Select')
160 ->addValue('data_type', 'String')
161 ->execute();
162
163 $customGroupId = CustomGroup::create()
164 ->setCheckPermissions(FALSE)
165 ->addValue('name', 'FinancialStuff')
166 ->addValue('extends', 'Contact')
167 ->execute()
168 ->first()['id'];
169
170 CustomField::create()
171 ->setCheckPermissions(FALSE)
172 ->addValue('label', 'Salary')
173 ->addValue('custom_group_id', $customGroupId)
174 ->addValue('html_type', 'Number')
175 ->addValue('data_type', 'Money')
176 ->execute();
177
178 Contact::create()
179 ->setCheckPermissions(FALSE)
180 ->addValue('first_name', 'Red')
181 ->addValue('last_name', 'Corn')
182 ->addValue('contact_type', 'Individual')
183 ->addValue("$group.$colorField", 'r')
184 ->addValue("$group.$foodField", '1')
185 ->addValue('FinancialStuff.Salary', 10000)
186 ->execute();
187
188 Contact::create()
189 ->setCheckPermissions(FALSE)
190 ->addValue('first_name', 'Blue')
191 ->addValue('last_name', 'Cheese')
192 ->addValue('contact_type', 'Individual')
193 ->addValue("$group.$colorField", 'b')
194 ->addValue("$group.$foodField", '3')
195 ->addValue('FinancialStuff.Salary', 500000)
196 ->execute();
197
198 $result = Contact::get()
199 ->setCheckPermissions(FALSE)
200 ->addSelect('first_name')
201 ->addSelect('last_name')
202 ->addSelect("$group.$colorField.label")
203 ->addSelect("$group.$foodField.label")
204 ->addSelect('FinancialStuff.Salary')
205 ->addWhere("$group.$foodField.label", 'IN', ['Corn', 'Cheese'])
206 ->execute();
207
208 $blueCheese = NULL;
209 foreach ($result as $contact) {
210 if ($contact['first_name'] === 'Blue') {
211 $blueCheese = $contact;
212 }
213 }
214
215 $this->assertEquals('Blue', $blueCheese["$group.$colorField.label"]);
216 $this->assertEquals('Cheese', $blueCheese["$group.$foodField.label"]);
217 $this->assertEquals(500000, $blueCheese['FinancialStuff.Salary']);
218 }
219
220 }