Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CustomValueTest.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
f299f7db 7 | Copyright CiviCRM LLC (c) 2004-2020 |
380f3545
TO
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
f299f7db 32 * @copyright CiviCRM LLC (c) 2004-2020
380f3545
TO
33 * $Id$
34 *
35 */
36
37
19b53e5b
C
38namespace api\v4\Action;
39
40use Civi\Api4\CustomField;
41use Civi\Api4\CustomGroup;
42use Civi\Api4\CustomValue;
43use Civi\Api4\Contact;
44
45/**
46 * @group headless
47 */
48class CustomValueTest extends BaseCustomValueTest {
49
50 protected $contactID;
51
52 /**
53 * Test CustomValue::GetFields/Get/Create/Update/Replace/Delete
54 */
55 public function testCRUD() {
56 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
57
58 $group = uniqid('groupc');
59 $colorField = uniqid('colorc');
60 $textField = uniqid('txt');
61
62 $customGroup = CustomGroup::create()
63 ->setCheckPermissions(FALSE)
64 ->addValue('name', $group)
65 ->addValue('extends', 'Contact')
66 ->addValue('is_multiple', TRUE)
67 ->execute()
68 ->first();
69
70 CustomField::create()
71 ->setCheckPermissions(FALSE)
72 ->addValue('label', $colorField)
73 ->addValue('options', $optionValues)
74 ->addValue('custom_group_id', $customGroup['id'])
75 ->addValue('html_type', 'Select')
76 ->addValue('data_type', 'String')
77 ->execute();
78
79 CustomField::create()
80 ->setCheckPermissions(FALSE)
81 ->addValue('label', $textField)
82 ->addValue('custom_group_id', $customGroup['id'])
83 ->addValue('html_type', 'Text')
84 ->addValue('data_type', 'String')
85 ->execute();
86
87 $this->contactID = Contact::create()
88 ->setCheckPermissions(FALSE)
89 ->addValue('first_name', 'Johann')
90 ->addValue('last_name', 'Tester')
91 ->addValue('contact_type', 'Individual')
92 ->execute()
93 ->first()['id'];
94
95 // Retrieve and check the fields of CustomValue = Custom_$group
96 $fields = CustomValue::getFields($group)->execute();
97 $expectedResult = [
98 [
99 'custom_field_id' => 1,
100 'custom_group' => $group,
101 'name' => $colorField,
102 'title' => $colorField,
103 'entity' => "Custom_$group",
104 'data_type' => 'String',
105 'fk_entity' => NULL,
106 ],
107 [
108 'custom_field_id' => 2,
109 'custom_group' => $group,
110 'name' => $textField,
111 'title' => $textField,
112 'entity' => "Custom_$group",
113 'data_type' => 'String',
114 'fk_entity' => NULL,
115 ],
116 [
117 'name' => 'id',
118 'title' => ts('Custom Value ID'),
119 'entity' => "Custom_$group",
120 'data_type' => 'Integer',
121 'fk_entity' => NULL,
122 ],
123 [
124 'name' => 'entity_id',
125 'title' => ts('Entity ID'),
126 'entity' => "Custom_$group",
127 'data_type' => 'Integer',
128 'fk_entity' => 'Contact',
129 ],
130 ];
131
132 foreach ($expectedResult as $key => $field) {
133 foreach ($field as $attr => $value) {
134 $this->assertEquals($expectedResult[$key][$attr], $fields[$key][$attr]);
135 }
136 }
137
138 // CASE 1: Test CustomValue::create
139 // Create two records for a single contact and using CustomValue::get ensure that two records are created
140 CustomValue::create($group)
141 ->addValue($colorField, 'Green')
142 ->addValue("entity_id", $this->contactID)
143 ->execute();
144 CustomValue::create($group)
145 ->addValue($colorField, 'Red')
146 ->addValue("entity_id", $this->contactID)
147 ->execute();
148 // fetch custom values using API4 CustomValue::get
149 $result = CustomValue::get($group)->execute();
150
151 // check if two custom values are created
152 $this->assertEquals(2, count($result));
153 $expectedResult = [
154 [
155 'id' => 1,
156 $colorField => 'Green',
157 'entity_id' => $this->contactID,
158 ],
159 [
160 'id' => 2,
161 $colorField => 'Red',
162 'entity_id' => $this->contactID,
163 ],
164 ];
165 // match the data
166 foreach ($expectedResult as $key => $field) {
167 foreach ($field as $attr => $value) {
168 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
169 }
170 }
171
172 // CASE 2: Test CustomValue::update
173 // Update a records whose id is 1 and change the custom field (name = Color) value to 'White' from 'Green'
174 CustomValue::update($group)
175 ->addWhere("id", "=", 1)
176 ->addValue($colorField, 'White')
177 ->execute();
178
179 // ensure that the value is changed for id = 1
180 $color = CustomValue::get($group)
181 ->addWhere("id", "=", 1)
182 ->execute()
183 ->first()[$colorField];
184 $this->assertEquals('White', $color);
185
186 // CASE 3: Test CustomValue::replace
187 // create a second contact which will be used to replace the custom values, created earlier
188 $secondContactID = Contact::create()
189 ->setCheckPermissions(FALSE)
190 ->addValue('first_name', 'Adam')
191 ->addValue('last_name', 'Tester')
192 ->addValue('contact_type', 'Individual')
193 ->execute()
194 ->first()['id'];
195 // Replace all the records which was created earlier with entity_id = first contact
196 // with custom record [$colorField => 'Rainbow', 'entity_id' => $secondContactID]
197 CustomValue::replace($group)
198 ->setRecords([[$colorField => 'Rainbow', 'entity_id' => $secondContactID]])
199 ->addWhere('entity_id', '=', $this->contactID)
200 ->execute();
201
202 // Check the two records created earlier is replaced by new contact
203 $result = CustomValue::get($group)->execute();
204 $this->assertEquals(1, count($result));
205
206 $expectedResult = [
207 [
208 'id' => 3,
209 $colorField => 'Rainbow',
210 'entity_id' => $secondContactID,
211 ],
212 ];
213 foreach ($expectedResult as $key => $field) {
214 foreach ($field as $attr => $value) {
215 $this->assertEquals($expectedResult[$key][$attr], $result[$key][$attr]);
216 }
217 }
218
219 // CASE 4: Test CustomValue::delete
220 // There is only record left whose id = 3, delete that record on basis of criteria id = 3
221 CustomValue::delete($group)->addWhere("id", "=", 3)->execute();
222 $result = CustomValue::get($group)->execute();
223 // check that there are no custom values present
224 $this->assertEquals(0, count($result));
225 }
226
227}