4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2020
38 namespace api\v
4\Action
;
40 use Civi\Api4\Contact
;
41 use Civi\Api4\CustomField
;
42 use Civi\Api4\CustomGroup
;
47 class BasicCustomFieldTest
extends BaseCustomValueTest
{
49 public function testWithSingleField() {
51 $customGroup = CustomGroup
::create()
52 ->setCheckPermissions(FALSE)
53 ->addValue('name', 'MyContactFields')
54 ->addValue('extends', 'Contact')
59 ->setCheckPermissions(FALSE)
60 ->addValue('label', 'FavColor')
61 ->addValue('custom_group_id', $customGroup['id'])
62 ->addValue('html_type', 'Text')
63 ->addValue('data_type', 'String')
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')
75 $contact = Contact
::get()
76 ->setCheckPermissions(FALSE)
77 ->addSelect('first_name')
78 ->addSelect('MyContactFields.FavColor')
79 ->addWhere('id', '=', $contactId)
80 ->addWhere('MyContactFields.FavColor', '=', 'Red')
84 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
87 ->addWhere('id', '=', $contactId)
88 ->addValue('MyContactFields.FavColor', 'Blue')
91 $contact = Contact
::get()
92 ->setCheckPermissions(FALSE)
93 ->addSelect('MyContactFields.FavColor')
94 ->addWhere('id', '=', $contactId)
98 $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
101 public function testWithTwoFields() {
103 $customGroup = CustomGroup
::create()
104 ->setCheckPermissions(FALSE)
105 ->addValue('name', 'MyContactFields')
106 ->addValue('extends', 'Contact')
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')
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')
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')
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')
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')
155 $this->assertArrayHasKey('MyContactFields.FavColor', $contact);
156 $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
159 ->addWhere('id', '=', $contactId1)
160 ->addValue('MyContactFields.FavColor', 'Blue')
163 $contact = Contact
::get()
164 ->setCheckPermissions(FALSE)
165 ->addSelect('MyContactFields.FavColor')
166 ->addWhere('id', '=', $contactId1)
170 $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
172 $search = Contact
::get()
173 ->setCheckPermissions(FALSE)
174 ->addClause('OR', ['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes'])
180 $this->assertEquals([$contactId1, $contactId2], array_keys((array) $search));
182 $search = Contact
::get()
183 ->setCheckPermissions(FALSE)
184 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
190 $this->assertNotContains($contactId2, array_keys((array) $search));
192 $search = Contact
::get()
193 ->setCheckPermissions(FALSE)
194 ->addClause('NOT', ['MyContactFields.FavColor', '=', 'Purple'], ['MyContactFields.FavFood', '=', 'Grapes'])
200 $this->assertContains($contactId1, array_keys((array) $search));
201 $this->assertNotContains($contactId2, array_keys((array) $search));
203 $search = Contact
::get()
204 ->setCheckPermissions(FALSE)
205 ->setWhere([['NOT', ['OR', [['MyContactFields.FavColor', '=', 'Blue'], ['MyContactFields.FavFood', '=', 'Grapes']]]]])
211 $this->assertNotContains($contactId1, array_keys((array) $search));
212 $this->assertNotContains($contactId2, array_keys((array) $search));