Merge pull request #18506 from totten/master-bundle-array
[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 $form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', $this->getFormValues());
24 $form->set('id', $eventID);
25 $form->preProcess();
26 $form->buildQuickForm();
27 $form->postProcess();
28 $this->assertCorrectEmails($eventID);
29
30 // Now do it again to see if it gets messed with.
31 $form = $this->getFormObject('CRM_Event_Form_ManageEvent_Location', array_merge($this->getFormValues(), ['loc_event_id' => $this->ids['LocBlock'][0]]));
32 $form->set('id', $eventID);
33 $form->preProcess();
34 $form->buildQuickForm();
35 $form->postProcess();
36 $this->assertCorrectEmails($eventID);
37 }
38
39 /**
40 * Get the values to submit for the form.
41 *
42 * @return array
43 */
44 protected function getFormValues() {
45 return [
46 'address' =>
47 [
48 1 =>
49 [
50 'master_id' => '',
51 'street_address' => '581O Lincoln Dr SW',
52 'supplemental_address_1' => '',
53 'supplemental_address_2' => '',
54 'supplemental_address_3' => '',
55 'city' => 'Santa Fe',
56 'postal_code' => '87594',
57 'country_id' => '1228',
58 'state_province_id' => '1030',
59 'county_id' => '',
60 'geo_code_1' => '35.5212',
61 'geo_code_2' => '-105.982',
62 ],
63 ],
64 'email' =>
65 [
66 1 =>
67 [
68 'email' => 'celebration@example.org',
69 ],
70 2 =>
71 [
72 'email' => 'bigger_party@example.org',
73 ],
74 ],
75 'phone' =>
76 [
77 1 =>
78 [
79 'phone_type_id' => '1',
80 'phone' => '303 323-1000',
81 'phone_ext' => '',
82 ],
83 2 =>
84 [
85 'phone_type_id' => '1',
86 'phone' => '44',
87 'phone_ext' => '',
88 ],
89 ],
90 'location_option' => '2',
91 'loc_event_id' => '3',
92 'is_show_location' => '1',
93 'is_template' => '0',
94 ];
95 }
96
97 /**
98 * @param int $eventID
99 *
100 * @return \Civi\Api4\Generic\Result
101 * @throws \API_Exception
102 * @throws \Civi\API\Exception\UnauthorizedException
103 */
104 protected function assertCorrectEmails($eventID) {
105 $emails = Email::get()
106 ->addWhere('email', 'IN', ['bigger_party@example.org', 'celebration@example.org'])
107 ->addOrderBy('email', 'DESC')
108 ->execute();
109
110 $this->assertCount(2, $emails);
111 $firstEmail = $emails->first();
112 $locationBlock = Event::get()
113 ->addWhere('id', '=', $eventID)
114 ->setSelect(['loc_block.*', 'loc_block_id'])
115 ->execute()->first();
116 $this->ids['LocBlock'][0] = $locationBlock['loc_block_id'];
117 $this->assertEquals($firstEmail['id'], $locationBlock['loc_block.email_id']);
118 $secondEmail = $emails->last();
119 $this->assertEquals($secondEmail['id'], $locationBlock['loc_block.email_2_id']);
120 return $emails;
121 }
122
123 }