From b39946ff5e262723d11fc79e8eba69d083b32fa8 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Fri, 21 Jan 2022 06:05:47 +0000 Subject: [PATCH] CIVICRM-1773 Add Event timezone tests. --- .../Event/Form/ManageEvent/EventInfoTest.php | 59 +++++++++++++++++++ .../Form/ManageEvent/RegistrationTest.php | 56 ++++++++++++++++++ tests/phpunit/CRM/Utils/DateTest.php | 22 +++++++ 3 files changed, 137 insertions(+) create mode 100644 tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php create mode 100644 tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php new file mode 100644 index 0000000000..942a6abb50 --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/ManageEvent/EventInfoTest.php @@ -0,0 +1,59 @@ +form, 'CRM_Event_Form_ManageEvent_EventInfo')) { + // EventInfo form redirects to the location form if the action is ADD + $event->form->setAction(CRM_Core_Action::NONE); + } + } + + public function testTimeZone() { + $formValues = [ + 'start_date' => '2022-06-22 12:00:00', + 'end_date' => '2022-06-22 20:00:00', + 'event_tz' => 'Australia/Sydney', + ]; + + Civi::dispatcher()->addListener('hook_civicrm_postProcess', [$this, 'preventCiviExit']); + + $event_id = $this->submitForm($formValues); + + $this->assertIsInt($event_id, 'Event creation success'); + + Civi::dispatcher()->removeListener('hook_civicrm_postProcess', [$this, 'preventCiviExit']); + + $event = Event::get(FALSE) + ->addWhere('id', '=', $event_id) + ->addSelect('id', 'start_date', 'end_date', 'event_tz') + ->execute()[0]; + + $this->assertEquals('2022-06-22 02:00:00', $event['start_date'], 'Event start date resolved by timezone.'); + $this->assertEquals('2022-06-22 10:00:00', $event['end_date'], 'Event end date resolved by timezone.'); + } + + public function getFormValues() { + if (empty(Civi::$statics[__CLASS__])) { + Civi::$statics[__CLASS__] = $this->eventCreate(); + Civi::$statics[__CLASS__]['id'] = NULL; + } + + return Civi::$statics[__CLASS__]; + } + + public function submitForm(array $formValues, ?int $eventID = NULL): int { + $form = $this->getFormObject(CRM_Event_Form_ManageEvent_EventInfo::class, array_merge($this->getFormValues(), $formValues)); + if ($eventID) { + $form->set('id', $eventID); + } + + $form->preProcess(); + $form->buildQuickForm(); + $form->postProcess(); + return $form->get('id'); + } + +} diff --git a/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php b/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php new file mode 100644 index 0000000000..44a3f26787 --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/ManageEvent/RegistrationTest.php @@ -0,0 +1,56 @@ +form, 'CRM_Event_Form_ManageEvent_Registration')) { + // EventInfo form redirects to the location form if the action is ADD + $event->form->setAction(CRM_Core_Action::NONE); + } + } + + public function testTimeZone() { + $event_id = $this->eventCreate([ + 'event_tz' => 'Australia/Sydney', + 'start_date' => '2022-06-22 12:00:00', + 'end_date' => '2022-06-22 20:00:00', + ])['id']; + + $formValues = [ + 'registration_start_date' => '2022-05-23 09:00:00', + 'registration_end_date' => '2022-06-20 17:00:00', + ]; + + Civi::dispatcher()->addListener('hook_civicrm_postProcess', [$this, 'preventCiviExit']); + + $this->submitForm($formValues, $event_id); + + $this->assertIsInt($event_id, 'Event creation success'); + + Civi::dispatcher()->removeListener('hook_civicrm_postProcess', [$this, 'preventCiviExit']); + + $event = Event::get(FALSE) + ->addWhere('id', '=', $event_id) + ->addSelect('id', 'registration_start_date', 'registration_end_date', 'event_tz') + ->execute()[0]; + + $this->assertEquals('2022-05-22 23:00:00', $event['registration_start_date'], 'Registration start date resolved by timezone.'); + $this->assertEquals('2022-06-20 07:00:00', $event['registration_end_date'], 'Registration end date resolved by timezone.'); + + } + + public function submitForm(array $formValues, int $eventID): int { + $form = $this->getFormObject(CRM_Event_Form_ManageEvent_Registration::class, $formValues); + if ($eventID) { + $form->set('id', $eventID); + } + + $form->preProcess(); + $form->buildQuickForm(); + $form->postProcess(); + return $form->get('id'); + } + +} diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php index b0917741ef..9f17b77322 100644 --- a/tests/phpunit/CRM/Utils/DateTest.php +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -324,4 +324,26 @@ class CRM_Utils_DateTest extends CiviUnitTestCase { } } + /** + * Test timezone conversion function + */ + public function testConvertTimeZone() { + $tests = [ + [['1970-01-01 00:00:00', 'Atlantic/Azores', 'UTC'], '1969-12-31 23:00:00'], + [['1970-01-01 00:00:00', 'Europe/Berlin'], '1970-01-01 01:00:00'], + [['2022-06-22 12:00:00', 'Atlantic/Azores'], '2022-06-22 12:00:00'], + [['2022-06-22 12:00:00', 'Europe/Berlin', 'UTC'], '2022-06-22 14:00:00'], + [['2022-06-22 12:00:00', 'Europe/Berlin', 'Australia/Sydney'], '2022-06-22 04:00:00'], + ]; + + foreach ($tests as $i => [$params, $result]) { + $this->assertEquals($result, CRM_Utils_Date::convertTimeZone(...$params), "convertTimeZone $i"); + } + + //0000-00-00 00:00:00 is not an actual time, so we should expect an exception. + $this->expectException(CRM_Core_Exception::class); + $this->expectExceptionMessageMatches('{^Failed to parse date}'); + CRM_Utils_Date::convertTimeZone('0000-00-00 00:00:00', 'Europe/Berlin'); + } + } -- 2.25.1