Merge pull request #15184 from eileenmcnaughton/dedupe9
[civicrm-core.git] / tests / phpunit / api / v4 / Action / BasicCustomFieldTest.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\Contact;
41 use Civi\Api4\CustomField;
42 use Civi\Api4\CustomGroup;
43
44 /**
45 * @group headless
46 */
47 class BasicCustomFieldTest extends BaseCustomValueTest {
48
49 public function testWithSingleField() {
50
51 $customGroup = CustomGroup::create()
52 ->setCheckPermissions(FALSE)
53 ->addValue('name', 'MyContactFields')
54 ->addValue('extends', 'Contact')
55 ->execute()
56 ->first();
57
58 CustomField::create()
59 ->setCheckPermissions(FALSE)
60 ->addValue('label', 'FavColor')
61 ->addValue('custom_group_id', $customGroup['id'])
62 ->addValue('html_type', 'Text')
63 ->addValue('data_type', 'String')
64 ->execute();
65
66 $contactId = Contact::create()
67 ->setCheckPermissions(FALSE)
68 ->addValue('first_name', 'Johann')
69 ->addValue('last_name', 'Tester')
70 ->addValue('contact_type', 'Individual')
71 ->addValue('MyContactFields.FavColor', 'Red')
72 ->execute()
73 ->first()['id'];
74
75 $contact = Contact::get()
76 ->setCheckPermissions(FALSE)
77 ->addSelect('first_name')
78 ->addSelect('MyContactFields.FavColor')
79 ->addWhere('id', '=', $contactId)
80 ->addWhere('MyContactFields.FavColor', '=', 'Red')
81 ->execute()
82 ->first();
83
84 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
85
86 Contact::update()
87 ->addWhere('id', '=', $contactId)
88 ->addValue('MyContactFields.FavColor', 'Blue')
89 ->execute();
90
91 $contact = Contact::get()
92 ->setCheckPermissions(FALSE)
93 ->addSelect('MyContactFields.FavColor')
94 ->addWhere('id', '=', $contactId)
95 ->execute()
96 ->first();
97
98 $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
99 }
100
101 public function testWithTwoFields() {
102
103 $customGroup = CustomGroup::create()
104 ->setCheckPermissions(FALSE)
105 ->addValue('name', 'MyContactFields')
106 ->addValue('extends', 'Contact')
107 ->execute()
108 ->first();
109
110 CustomField::create()
111 ->setCheckPermissions(FALSE)
112 ->addValue('label', 'FavColor')
113 ->addValue('custom_group_id', $customGroup['id'])
114 ->addValue('html_type', 'Text')
115 ->addValue('data_type', 'String')
116 ->execute();
117
118 CustomField::create()
119 ->setCheckPermissions(FALSE)
120 ->addValue('label', 'FavFood')
121 ->addValue('custom_group_id', $customGroup['id'])
122 ->addValue('html_type', 'Text')
123 ->addValue('data_type', 'String')
124 ->execute();
125
126 $contactId1 = Contact::create()
127 ->setCheckPermissions(FALSE)
128 ->addValue('first_name', 'Johann')
129 ->addValue('last_name', 'Tester')
130 ->addValue('MyContactFields.FavColor', 'Red')
131 ->addValue('MyContactFields.FavFood', 'Cherry')
132 ->execute()
133 ->first()['id'];
134
135 $contactId2 = Contact::create()
136 ->setCheckPermissions(FALSE)
137 ->addValue('first_name', 'MaryLou')
138 ->addValue('last_name', 'Tester')
139 ->addValue('MyContactFields.FavColor', 'Purple')
140 ->addValue('MyContactFields.FavFood', 'Grapes')
141 ->execute()
142 ->first()['id'];
143
144 $contact = Contact::get()
145 ->setCheckPermissions(FALSE)
146 ->addSelect('first_name')
147 ->addSelect('MyContactFields.FavColor')
148 ->addSelect('MyContactFields.FavFood')
149 ->addWhere('id', '=', $contactId1)
150 ->addWhere('MyContactFields.FavColor', '=', 'Red')
151 ->addWhere('MyContactFields.FavFood', '=', 'Cherry')
152 ->execute()
153 ->first();
154
155 $this->assertArrayHasKey('MyContactFields.FavColor', $contact);
156 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
157
158 Contact::update()
159 ->addWhere('id', '=', $contactId1)
160 ->addValue('MyContactFields.FavColor', 'Blue')
161 ->execute();
162
163 $contact = Contact::get()
164 ->setCheckPermissions(FALSE)
165 ->addSelect('MyContactFields.FavColor')
166 ->addWhere('id', '=', $contactId1)
167 ->execute()
168 ->first();
169
170 $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
171
172 $search = Contact::get()
173 ->setCheckPermissions(FALSE)
174 ->addClause('OR', ['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes'])
175 ->addSelect('id')
176 ->addOrderBy('id')
177 ->execute()
178 ->indexBy('id');
179
180 $this->assertEquals([$contactId1, $contactId2], array_keys((array) $search));
181
182 $search = Contact::get()
183 ->setCheckPermissions(FALSE)
184 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
185 ->addSelect('id')
186 ->addOrderBy('id')
187 ->execute()
188 ->indexBy('id');
189
190 $this->assertNotContains($contactId2, array_keys((array) $search));
191
192 $search = Contact::get()
193 ->setCheckPermissions(FALSE)
194 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
195 ->addSelect('id')
196 ->addOrderBy('id')
197 ->execute()
198 ->indexBy('id');
199
200 $this->assertContains($contactId1, array_keys((array) $search));
201 $this->assertNotContains($contactId2, array_keys((array) $search));
202
203 $search = Contact::get()
204 ->setCheckPermissions(FALSE)
205 ->setWhere([['NOT', ['OR', [['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes']]]]])
206 ->addSelect('id')
207 ->addOrderBy('id')
208 ->execute()
209 ->indexBy('id');
210
211 $this->assertNotContains($contactId1, array_keys((array) $search));
212 $this->assertNotContains($contactId2, array_keys((array) $search));
213 }
214
215 }