[REF] Fix Event location to create it's locations directly rather than via shared...
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / LocationTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
4cbe18b8
EM
18/**
19 * Class CRM_Core_BAO_LocationTest
acb109b7 20 * @group headless
4cbe18b8 21 */
6a488035 22class CRM_Core_BAO_LocationTest extends CiviUnitTestCase {
39b959db 23
00be9182 24 public function setUp() {
6a488035
TO
25 parent::setUp();
26
9099cab3 27 $this->quickCleanup([
39b959db
SL
28 'civicrm_contact',
29 'civicrm_address',
30 'civicrm_loc_block',
31 'civicrm_email',
32 'civicrm_phone',
33 'civicrm_im',
9099cab3 34 ]);
6a488035
TO
35 }
36
37 /**
38 * Tears down the fixture, for example, closes a network connection.
39 * This method is called after a test is executed.
6a488035 40 */
00be9182 41 public function tearDown() {
9099cab3 42 $tablesToTruncate = [
6a488035
TO
43 'civicrm_contact',
44 'civicrm_openid',
45 'civicrm_loc_block',
9099cab3 46 ];
6a488035
TO
47 $this->quickCleanup($tablesToTruncate);
48 }
49
00be9182 50 public function testCreateWithMissingParams() {
f2040bc6 51 $contactId = $this->individualCreate();
9099cab3 52 $params = [
6a488035
TO
53 'contact_id' => $contactId,
54 'street_address' => 'Saint Helier St',
9099cab3 55 ];
6a488035
TO
56
57 CRM_Core_BAO_Location::create($params);
58
59 //Now check DB for Address
60 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
61 'Database check, Address created successfully.'
62 );
63
93ac19cd 64 $this->contactDelete($contactId);
6a488035
TO
65 }
66
67 /**
100fef9d 68 * Create() method
6a488035
TO
69 * create various elements of location block
70 * without civicrm_loc_block entry
71 */
00be9182 72 public function testCreateWithoutLocBlock() {
f2040bc6 73 $contactId = $this->individualCreate();
6a488035
TO
74
75 //create various element of location block
76 //like address, phone, email, openid, im.
9099cab3
CW
77 $params = [
78 'address' => [
79 '1' => [
6a488035
TO
80 'street_address' => 'Saint Helier St',
81 'supplemental_address_1' => 'Hallmark Ct',
82 'supplemental_address_2' => 'Jersey Village',
207f62c6 83 'supplemental_address_3' => 'My Town',
6a488035
TO
84 'city' => 'Newark',
85 'postal_code' => '01903',
86 'country_id' => 1228,
87 'state_province_id' => 1029,
88 'geo_code_1' => '18.219023',
89 'geo_code_2' => '-105.00973',
90 'is_primary' => 1,
91 'location_type_id' => 1,
9099cab3
CW
92 ],
93 ],
94 'email' => [
95 '1' => [
6a488035
TO
96 'email' => 'john.smith@example.org',
97 'is_primary' => 1,
98 'location_type_id' => 1,
9099cab3
CW
99 ],
100 ],
101 'phone' => [
102 '1' => [
6a488035
TO
103 'phone_type_id' => 1,
104 'phone' => '303443689',
105 'is_primary' => 1,
106 'location_type_id' => 1,
9099cab3
CW
107 ],
108 '2' => [
6a488035
TO
109 'phone_type_id' => 2,
110 'phone' => '9833910234',
111 'location_type_id' => 1,
9099cab3
CW
112 ],
113 ],
114 'openid' => [
115 '1' => [
6a488035
TO
116 'openid' => 'http://civicrm.org/',
117 'location_type_id' => 1,
118 'is_primary' => 1,
9099cab3
CW
119 ],
120 ],
121 'im' => [
122 '1' => [
6a488035
TO
123 'name' => 'jane.doe',
124 'provider_id' => 1,
125 'location_type_id' => 1,
126 'is_primary' => 1,
9099cab3
CW
127 ],
128 ],
129 ];
6a488035
TO
130
131 $params['contact_id'] = $contactId;
132
9a350644 133 $locBlockId = CRM_Core_BAO_Location::create($params);
6a488035
TO
134
135 //Now check DB for contact
9099cab3 136 $searchParams = [
6a488035
TO
137 'contact_id' => $contactId,
138 'location_type_id' => 1,
139 'is_primary' => 1,
9099cab3
CW
140 ];
141 $compareParams = [
6a488035
TO
142 'street_address' => 'Saint Helier St',
143 'supplemental_address_1' => 'Hallmark Ct',
144 'supplemental_address_2' => 'Jersey Village',
207f62c6 145 'supplemental_address_3' => 'My Town',
6a488035
TO
146 'city' => 'Newark',
147 'postal_code' => '01903',
148 'country_id' => 1228,
149 'state_province_id' => 1029,
150 'geo_code_1' => '18.219023',
151 'geo_code_2' => '-105.00973',
9099cab3 152 ];
6a488035
TO
153 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
154
9099cab3 155 $compareParams = ['email' => 'john.smith@example.org'];
6a488035
TO
156 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
157
9099cab3 158 $compareParams = ['openid' => 'http://civicrm.org/'];
6a488035
TO
159 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
160
9099cab3 161 $compareParams = [
6a488035
TO
162 'name' => 'jane.doe',
163 'provider_id' => 1,
9099cab3 164 ];
6a488035
TO
165 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
166
9099cab3 167 $searchParams = [
6a488035
TO
168 'contact_id' => $contactId,
169 'location_type_id' => 1,
170 'is_primary' => 1,
171 'phone_type_id' => 1,
9099cab3
CW
172 ];
173 $compareParams = ['phone' => '303443689'];
6a488035
TO
174 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
175
9099cab3 176 $searchParams = [
6a488035
TO
177 'contact_id' => $contactId,
178 'location_type_id' => 1,
179 'phone_type_id' => 2,
9099cab3
CW
180 ];
181 $compareParams = ['phone' => '9833910234'];
6a488035
TO
182 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
183
93ac19cd 184 $this->contactDelete($contactId);
6a488035
TO
185 }
186
6a488035 187 /**
100fef9d 188 * GetValues() method
6a488035
TO
189 * get the values of various location elements
190 */
00be9182 191 public function testLocBlockgetValues() {
6ae19242 192 $contactId = $this->individualCreate();
6a488035
TO
193
194 //create various element of location block
195 //like address, phone, email, openid, im.
9099cab3
CW
196 $params = [
197 'address' => [
198 '1' => [
6a488035
TO
199 'street_address' => 'Saint Helier St',
200 'supplemental_address_1' => 'Hallmark Ct',
201 'supplemental_address_2' => 'Jersey Village',
207f62c6 202 'supplemental_address_3' => 'My Town',
6a488035
TO
203 'city' => 'Newark',
204 'postal_code' => '01903',
205 'country_id' => 1228,
206 'state_province_id' => 1029,
207 'geo_code_1' => '18.219023',
208 'geo_code_2' => '-105.00973',
209 'is_primary' => 1,
210 'location_type_id' => 1,
9099cab3
CW
211 ],
212 ],
213 'email' => [
214 '1' => [
6a488035
TO
215 'email' => 'john.smith@example.org',
216 'is_primary' => 1,
217 'location_type_id' => 1,
9099cab3
CW
218 ],
219 ],
220 'phone' => [
221 '1' => [
6a488035
TO
222 'phone_type_id' => 1,
223 'phone' => '303443689',
224 'is_primary' => 1,
225 'location_type_id' => 1,
9099cab3
CW
226 ],
227 '2' => [
6a488035
TO
228 'phone_type_id' => 2,
229 'phone' => '9833910234',
230 'location_type_id' => 1,
9099cab3
CW
231 ],
232 ],
233 'openid' => [
234 '1' => [
6a488035
TO
235 'openid' => 'http://civicrm.org/',
236 'location_type_id' => 1,
237 'is_primary' => 1,
9099cab3
CW
238 ],
239 ],
240 'im' => [
241 '1' => [
6a488035
TO
242 'name' => 'jane.doe',
243 'provider_id' => 1,
244 'location_type_id' => 1,
245 'is_primary' => 1,
9099cab3
CW
246 ],
247 ],
248 ];
6a488035
TO
249
250 $params['contact_id'] = $contactId;
251
252 //create location elements.
253 CRM_Core_BAO_Location::create($params);
254
255 //get the values from DB
256 $values = CRM_Core_BAO_Location::getValues($params);
257
258 //Now check values of address
259 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
260 CRM_Utils_Array::value('1', $values['address'])
261 );
262
263 //Now check values of email
264 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
265 CRM_Utils_Array::value('1', $values['email'])
266 );
267
268 //Now check values of phone
269 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
270 CRM_Utils_Array::value('1', $values['phone'])
271 );
272
273 //Now check values of mobile
274 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
275 CRM_Utils_Array::value('2', $values['phone'])
276 );
277
278 //Now check values of openid
279 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
280 CRM_Utils_Array::value('1', $values['openid'])
281 );
282
283 //Now check values of im
284 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
285 CRM_Utils_Array::value('1', $values['im'])
286 );
93ac19cd 287 $this->contactDelete($contactId);
6a488035 288 }
96025800 289
6a488035 290}