Merge pull request #18544 from magnolia61/participant_status_notification_default
[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 * Get the values to submit for the form.
115 *
116 * @return array
117 */
118 protected function getFormValues() {
119 return [
120 'address' =>
121 [
122 1 =>
123 [
124 'master_id' => '',
125 'street_address' => '581O Lincoln Dr SW',
126 'supplemental_address_1' => '',
127 'supplemental_address_2' => '',
128 'supplemental_address_3' => '',
129 'city' => 'Santa Fe',
130 'postal_code' => '87594',
131 'country_id' => '1228',
132 'state_province_id' => '1030',
133 'county_id' => '',
134 'geo_code_1' => '35.5212',
135 'geo_code_2' => '-105.982',
136 ],
137 ],
138 'email' =>
139 [
140 1 =>
141 [
142 'email' => 'celebration@example.org',
143 ],
144 2 =>
145 [
146 'email' => 'bigger_party@example.org',
147 ],
148 ],
149 'phone' =>
150 [
151 1 =>
152 [
153 'phone_type_id' => '1',
154 'phone' => '303 323-1000',
155 'phone_ext' => '',
156 ],
157 2 =>
158 [
159 'phone_type_id' => '1',
160 'phone' => '44',
161 'phone_ext' => '',
162 ],
163 ],
164 'location_option' => '2',
165 'loc_event_id' => '3',
166 'is_show_location' => '1',
167 'is_template' => '0',
168 ];
169 }
170
171 /**
172 * @param int $eventID
173 *
174 * @return \Civi\Api4\Generic\Result
175 * @throws \API_Exception
176 * @throws \Civi\API\Exception\UnauthorizedException
177 */
178 protected function assertCorrectEmails($eventID) {
179 $emails = Email::get()
180 ->addWhere('email', 'IN', ['bigger_party@example.org', 'celebration@example.org'])
181 ->addOrderBy('email', 'DESC')
182 ->execute();
183
184 $this->assertCount(2, $emails);
185 $firstEmail = $emails->first();
186 $locationBlock = Event::get()
187 ->addWhere('id', '=', $eventID)
188 ->setSelect(['loc_block.*', 'loc_block_id'])
189 ->execute()->first();
190 $this->ids['LocBlock'][0] = $locationBlock['loc_block_id'];
191 $this->assertEquals($firstEmail['id'], $locationBlock['loc_block.email_id']);
192 $secondEmail = $emails->last();
193 $this->assertEquals($secondEmail['id'], $locationBlock['loc_block.email_2_id']);
194 return $emails;
195 }
196
197 /**
198 * @param array $formValues
199 * @param int $eventID
200 *
201 * @throws \CRM_Core_Exception
202 */
203 protected function submitForm(array $formValues, int $eventID): void {
204 $form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', array_merge($this->getFormValues(), $formValues));
205 $form->set('id', $eventID);
206 $form->preProcess();
207 $form->buildQuickForm();
208 $form->postProcess();
209 }
210
211 }