Skip to content

Commit

Permalink
Merge pull request #618 from TheRestartProject/RES-1856_cant_create_c…
Browse files Browse the repository at this point in the history
…anberra_group

RES-1856 Allow "outdated" timezones
  • Loading branch information
edwh authored Feb 13, 2023
2 parents 863cfc4 + 1cb986a commit 84c70ee
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]);
}

Expand Down
11 changes: 10 additions & 1 deletion app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 2 additions & 3 deletions app/Http/Controllers/PartyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,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'];
}
Expand All @@ -204,7 +204,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'));
}
},
Expand Down Expand Up @@ -404,7 +404,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,
Expand Down
2 changes: 1 addition & 1 deletion app/Rules/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/Groups/APIv2GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions tests/Feature/Groups/GroupEditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 84c70ee

Please sign in to comment.