dev/core#2102 Fix hang on event edit
[civicrm-core.git] / tests / phpunit / CRM / Event / Form / ManageEvent / LocationTest.php
1 <?php
2
3 use Civi\Api4\Event;
4 use Civi\Api4\Email;
5
6 /**
7 * Test CRM_Event_Form_Registration functions.
8 *
9 * @package CiviCRM
10 * @group headless
11 */
12 class CRM_Event_Form_ManageEvent_LocationTest extends CiviUnitTestCase {
13
14 /**
15 * Test the right emails exist after submitting the location form twice.
16 *
17 * @throws \API_Exception
18 * @throws \CRM_Core_Exception
19 * @throws \Civi\API\Exception\UnauthorizedException
20 */
21 public function testSubmit() {
22 $eventID = (int) $this->eventCreate()['id'];
23 $this->submitForm([], $eventID);
24 $this->assertCorrectEmails($eventID);
25
26 // Now do it again to see if it gets messed with.
27 $this->submitForm(['loc_event_id' => $this->ids['LocBlock'][0]], $eventID);
28 $this->assertCorrectEmails($eventID);
29 }
30
31 /**
32 * Create() method
33 * create various elements of location block
34 * with civicrm_loc_block
35 */
36 public function testCreateWithLocBlock() {
37 $eventID = (int) $this->eventCreate()['id'];
38 $this->submitForm([
39 'address' => [
40 '1' => [
41 'street_address' => 'Saint Helier St',
42 'supplemental_address_1' => 'Hallmark Ct',
43 'supplemental_address_2' => 'Jersey Village',
44 'supplemental_address_3' => 'My Town',
45 'city' => 'Newark',
46 'postal_code' => '01903',
47 'country_id' => 1228,
48 'state_province_id' => 1029,
49 'geo_code_1' => '18.219023',
50 'geo_code_2' => '-105.00973',
51 'is_primary' => 1,
52 'location_type_id' => 1,
53 ],
54 ],
55 'email' => [
56 '1' => [
57 'email' => 'john.smith@example.org',
58 'is_primary' => 1,
59 'location_type_id' => 1,
60 ],
61 ],
62 'phone' => [
63 '1' => [
64 'phone_type_id' => 1,
65 'phone' => '303443689',
66 'is_primary' => 1,
67 'location_type_id' => 1,
68 ],
69 '2' => [
70 'phone_type_id' => 2,
71 'phone' => '9833910234',
72 'location_type_id' => 1,
73 ],
74 ],
75 ], $eventID);
76
77 //Now check DB for location block
78
79 $locationBlock = Event::get()
80 ->addWhere('id', '=', $eventID)
81 ->setSelect(['loc_block.*', 'loc_block_id'])
82 ->execute()->first();
83
84 $address = $this->callAPISuccessGetSingle('Address', ['id' => $locationBlock['loc_block.address_id']]);
85
86 $this->assertEquals([
87 'id' => $address['id'],
88 'location_type_id' => '1',
89 'is_primary' => '1',
90 'is_billing' => '0',
91 'street_address' => 'Saint Helier St',
92 'supplemental_address_1' => 'Hallmark Ct',
93 'supplemental_address_2' => 'Jersey Village',
94 'supplemental_address_3' => 'My Town',
95 'city' => 'Newark',
96 'postal_code' => '01903',
97 'country_id' => 1228,
98 'state_province_id' => 1029,
99 'geo_code_1' => '18.219023',
100 'geo_code_2' => '-105.00973',
101 'manual_geo_code' => '0',
102 ], $address);
103
104 $this->callAPISuccessGetSingle('Email', ['id' => $locationBlock['loc_block.email_id'], 'email' => 'john.smith@example.org']);
105 $this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_id'], 'phone' => '303443689']);
106 $this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_2_id'], 'phone' => '9833910234']);
107
108 // Cleanup.
109 CRM_Core_BAO_Location::deleteLocBlock($locationBlock['loc_block_id']);
110 $this->eventDelete($eventID);
111 }
112
113 /**
114 * Test updating a location block.
115 *
116 * @throws \API_Exception
117 * @throws \CRM_Core_Exception
118 * @throws \Civi\API\Exception\UnauthorizedException
119 */
120 public function testUpdateLocationBlock() {
121 $eventID = (int) $this->eventCreate()['id'];
122 $this->submitForm([
123 'address' => [
124 '1' => [
125 'street_address' => 'Old address',
126 'supplemental_address_1' => 'Hallmark Ct',
127 'supplemental_address_2' => 'Jersey Village',
128 'supplemental_address_3' => 'My Town',
129 'city' => 'Newark',
130 'postal_code' => '01903',
131 'country_id' => 1228,
132 'state_province_id' => 1029,
133 'geo_code_1' => '18.219023',
134 'geo_code_2' => '-105.00973',
135 'is_primary' => 1,
136 'location_type_id' => 1,
137 ],
138 ],
139 ], $eventID);
140
141 $this->submitForm([
142 'location_option' => 1,
143 'loc_event_id' => Event::get()->addWhere('id', '=', $eventID)->addSelect('loc_block_id')->execute()->first()['loc_block_id'],
144 'address' => [
145 '1' => [
146 'street_address' => 'New address',
147 'supplemental_address_1' => 'Hallmark Ct',
148 'supplemental_address_2' => 'Jersey Village',
149 'supplemental_address_3' => 'My Town',
150 'city' => 'Newark',
151 'postal_code' => '01903',
152 'country_id' => 1228,
153 'state_province_id' => 1029,
154 'geo_code_1' => '18.219023',
155 'geo_code_2' => '-105.00973',
156 ],
157 ],
158 'email' => [
159 '1' => [
160 'email' => '',
161 ],
162 '2' => [
163 'email' => '',
164 ],
165 ],
166 'phone' => [
167 '1' => [
168 'phone_type_id' => 1,
169 'phone' => '',
170 'phone_ext' => '',
171 ],
172 '2' => [
173 'phone_type_id' => 1,
174 'phone' => '',
175 'phone_ext' => '',
176 ],
177 ],
178 ], $eventID);
179 // Cleanup.
180 $this->eventDelete($eventID);
181 }
182
183 /**
184 * Get the values to submit for the form.
185 *
186 * @return array
187 */
188 protected function getFormValues() {
189 return [
190 'address' =>
191 [
192 1 =>
193 [
194 'master_id' => '',
195 'street_address' => '581O Lincoln Dr SW',
196 'supplemental_address_1' => '',
197 'supplemental_address_2' => '',
198 'supplemental_address_3' => '',
199 'city' => 'Santa Fe',
200 'postal_code' => '87594',
201 'country_id' => '1228',
202 'state_province_id' => '1030',
203 'county_id' => '',
204 'geo_code_1' => '35.5212',
205 'geo_code_2' => '-105.982',
206 ],
207 ],
208 'email' =>
209 [
210 1 =>
211 [
212 'email' => 'celebration@example.org',
213 ],
214 2 =>
215 [
216 'email' => 'bigger_party@example.org',
217 ],
218 ],
219 'phone' =>
220 [
221 1 =>
222 [
223 'phone_type_id' => '1',
224 'phone' => '303 323-1000',
225 'phone_ext' => '',
226 ],
227 2 =>
228 [
229 'phone_type_id' => '1',
230 'phone' => '44',
231 'phone_ext' => '',
232 ],
233 ],
234 'location_option' => '2',
235 'loc_event_id' => '3',
236 'is_show_location' => '1',
237 'is_template' => '0',
238 ];
239 }
240
241 /**
242 * @param int $eventID
243 *
244 * @return \Civi\Api4\Generic\Result
245 * @throws \API_Exception
246 * @throws \Civi\API\Exception\UnauthorizedException
247 */
248 protected function assertCorrectEmails($eventID) {
249 $emails = Email::get()
250 ->addWhere('email', 'IN', ['bigger_party@example.org', 'celebration@example.org'])
251 ->addOrderBy('email', 'DESC')
252 ->execute();
253
254 $this->assertCount(2, $emails);
255 $firstEmail = $emails->first();
256 $locationBlock = Event::get()
257 ->addWhere('id', '=', $eventID)
258 ->setSelect(['loc_block.*', 'loc_block_id'])
259 ->execute()->first();
260 $this->ids['LocBlock'][0] = $locationBlock['loc_block_id'];
261 $this->assertEquals($firstEmail['id'], $locationBlock['loc_block.email_id']);
262 $secondEmail = $emails->last();
263 $this->assertEquals($secondEmail['id'], $locationBlock['loc_block.email_2_id']);
264 return $emails;
265 }
266
267 /**
268 * @param array $formValues
269 * @param int $eventID
270 *
271 * @throws \CRM_Core_Exception
272 */
273 protected function submitForm(array $formValues, int $eventID): void {
274 $form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', array_merge($this->getFormValues(), $formValues));
275 $form->set('id', $eventID);
276 $form->preProcess();
277 $form->buildQuickForm();
278 $form->postProcess();
279 }
280
281 }