From 1cb986a4b8c7200e2b0b13a4c3ed5fbc1720b64c Mon Sep 17 00:00:00 2001 From: Edward Hibbert Date: Mon, 13 Feb 2023 10:47:56 +0000 Subject: [PATCH] Use timezones consistently and allow "outdated" timezones. --- app/Http/Controllers/API/GroupController.php | 2 +- app/Http/Controllers/ApiController.php | 11 +++++++- app/Http/Controllers/PartyController.php | 5 ++-- app/Rules/Timezone.php | 2 +- tests/Feature/Groups/APIv2GroupTest.php | 29 ++++++++++++++++++++ tests/Feature/Groups/GroupEditTest.php | 10 +++++++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/API/GroupController.php b/app/Http/Controllers/API/GroupController.php index 37cd126c5e..48d836fe49 100644 --- a/app/Http/Controllers/API/GroupController.php +++ b/app/Http/Controllers/API/GroupController.php @@ -720,7 +720,7 @@ private function validateGroupParams(Request $request, $create): array { $longitude = null; $country = null; - if ($timezone && !in_array($timezone, \DateTimeZone::listIdentifiers())) { + if ($timezone && !in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) { throw ValidationException::withMessages(['location ' => __('partials.validate_timezone')]); } diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index e2f0883f8d..a06415c853 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -270,6 +270,15 @@ public static function getDevices(Request $request, $page, $size) } public function timezones() { - return response()->json(\DB::select("SELECT name FROM mysql.time_zone_name WHERE name NOT LIKE 'posix%' AND name NOT LIKE 'right%';")); + $zones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC); + $ret = []; + + foreach ($zones as $zone) { + $ret[] = [ + 'name' => $zone + ]; + } + + return response()->json($ret); } } diff --git a/app/Http/Controllers/PartyController.php b/app/Http/Controllers/PartyController.php index e572c79373..bfa70da8c1 100644 --- a/app/Http/Controllers/PartyController.php +++ b/app/Http/Controllers/PartyController.php @@ -184,7 +184,7 @@ public function create(Request $request, $group_id = null) // We might be passed a timezone; if not then use the timezone of the group. $timezone = $request->input('timezone', $groupobj->timezone); - if ($timezone && !in_array($timezone, \DateTimeZone::listIdentifiers())) { + if ($timezone && !in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) { $error['timezone'] = 'Please select a valid timezone.'; $response['warning'] = $error['timezone']; } @@ -199,7 +199,7 @@ function ($attribute, $value, $fail) use ($request) { ], 'timezone' => [ function ($attribute, $value, $fail) use ($request) { - if ($request->filled('timezone') && !in_array($request->timezone, \DateTimeZone::listIdentifiers())) { + if ($request->filled('timezone') && !in_array($request->timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) { $fail(__('partials.validate_timezone')); } }, @@ -399,7 +399,6 @@ public function edit($id, Request $request) 'allGroups' => $allGroups, 'formdata' => PartyController::expandEvent($party, NULL), 'remotePost' => null, - 'grouplist' => $Groups->findList(), 'user' => Auth::user(), 'user_groups' => $groupsUserIsInChargeOf, 'userInChargeOfMultipleGroups' => $userInChargeOfMultipleGroups, diff --git a/app/Rules/Timezone.php b/app/Rules/Timezone.php index 15eef70c06..0d0569ecd2 100644 --- a/app/Rules/Timezone.php +++ b/app/Rules/Timezone.php @@ -25,7 +25,7 @@ public function __construct() */ public function passes($attribute, $value) { - return in_array($value, \DateTimeZone::listIdentifiers()); + return in_array($value, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC)); } /** diff --git a/tests/Feature/Groups/APIv2GroupTest.php b/tests/Feature/Groups/APIv2GroupTest.php index 6b88595c6c..a044df4673 100644 --- a/tests/Feature/Groups/APIv2GroupTest.php +++ b/tests/Feature/Groups/APIv2GroupTest.php @@ -258,4 +258,33 @@ public function testTags() { $json = json_decode($response->getContent(), true); self::assertEquals($tag->id, $json['data']['tags'][0]['id']); } + + public function testOutdated() { + // Check we can create a group with an outdated timezone. + $user = User::factory()->administrator()->create([ + 'api_token' => '1234', + ]); + // Set a network on the user. + $network = Network::factory()->create([ + 'shortname' => 'network', + ]); + $user->repair_network = $network->id; + $user->save(); + + $response = $this->post( + '/api/v2/groups?api_token=1234', + [ + 'name' => 'Test Group', + 'location' => 'London', + 'description' => 'Some text.', + 'timezone' => 'Australia/Canberra' + ] + ); + + $response->assertSuccessful(); + $json = json_decode($response->getContent(), true); + $this->assertTrue(array_key_exists('id', $json)); + $idgroups = $json['id']; + $this->assertGreaterThan(0, $idgroups); + } } diff --git a/tests/Feature/Groups/GroupEditTest.php b/tests/Feature/Groups/GroupEditTest.php index df59744d0a..0dd623c704 100644 --- a/tests/Feature/Groups/GroupEditTest.php +++ b/tests/Feature/Groups/GroupEditTest.php @@ -123,5 +123,15 @@ public function can_edit_timezone() { $timezones = json_decode($response->getContent(), TRUE); self::assertGreaterThan(0, count($timezones)); self::assertTrue(array_key_exists('name', $timezones[0])); + + // Australia/Canberra is an outdated timezone; check it appears. + $found = false; + foreach ($timezones as $timezone) { + if ($timezone['name'] == 'Australia/Canberra') { + $found = true; + } + } + + self::assertTrue($found); } }