Skip to content

Commit 656192c

Browse files
committed
feat: FOUR-30102 Command to update the timezone for the Anonymous user
1 parent a969c75 commit 656192c

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace ProcessMaker\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use ProcessMaker\Models\AnonymousUser;
7+
use ProcessMaker\Models\User;
8+
9+
class UpdateAnonymousUserTimezone extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'processmaker:update-anonymous-user-timezone
17+
{--timezone= : Timezone to assign. Defaults to config app.anonymous_user_timezone}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Update the anonymous user timezone from config (safe to run multiple times)';
25+
26+
/**
27+
* Execute the console command.
28+
*/
29+
public function handle(): int
30+
{
31+
$timezone = $this->option('timezone') ?: config('app.anonymous_user_timezone', 'UTC');
32+
33+
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->first();
34+
35+
if (!$user) {
36+
$this->error('Anonymous user not found.');
37+
38+
return self::FAILURE;
39+
}
40+
41+
if ($user->timezone === $timezone) {
42+
$this->info("Anonymous user timezone is already set to [{$timezone}].");
43+
44+
return self::SUCCESS;
45+
}
46+
47+
$previousTimezone = $user->timezone;
48+
$user->timezone = $timezone;
49+
$user->save();
50+
51+
$this->info("Anonymous user timezone updated from [{$previousTimezone}] to [{$timezone}].");
52+
53+
return self::SUCCESS;
54+
}
55+
}

‎config/app.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
// The timezone for the application
3333
'timezone' => env('APP_TIMEZONE', 'America/Los_Angeles'),
3434

35+
// The timezone for the anonymous user
36+
'anonymous_user_timezone' => env('ANONYMOUS_USER_TIMEZONE', 'UTC'),
37+
3538
// The time format for the application
3639
'dateformat' => env('DATE_FORMAT', 'm/d/Y h:i A'),
3740

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Tests\Feature\Console;
4+
5+
use ProcessMaker\Models\AnonymousUser;
6+
use ProcessMaker\Models\User;
7+
use Tests\TestCase;
8+
9+
class UpdateAnonymousUserTimezoneTest extends TestCase
10+
{
11+
public function testUpdatesTimezoneFromConfig(): void
12+
{
13+
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail();
14+
$user->timezone = 'America/Chicago';
15+
$user->save();
16+
17+
config(['app.anonymous_user_timezone' => 'America/New_York']);
18+
19+
$this->artisan('processmaker:update-anonymous-user-timezone')
20+
->expectsOutput('Anonymous user timezone updated from [America/Chicago] to [America/New_York].')
21+
->assertExitCode(0);
22+
23+
$this->assertEquals('America/New_York', $user->fresh()->timezone);
24+
}
25+
26+
public function testDoesNothingWhenTimezoneAlreadyMatches(): void
27+
{
28+
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail();
29+
$user->timezone = 'UTC';
30+
$user->save();
31+
32+
config(['app.anonymous_user_timezone' => 'UTC']);
33+
34+
$this->artisan('processmaker:update-anonymous-user-timezone')
35+
->expectsOutput('Anonymous user timezone is already set to [UTC].')
36+
->assertExitCode(0);
37+
38+
$this->assertEquals('UTC', $user->fresh()->timezone);
39+
}
40+
41+
public function testUpdatesTimezoneFromOption(): void
42+
{
43+
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail();
44+
$user->timezone = 'UTC';
45+
$user->save();
46+
47+
config(['app.anonymous_user_timezone' => 'UTC']);
48+
49+
$this->artisan('processmaker:update-anonymous-user-timezone', [
50+
'--timezone' => 'Europe/Madrid',
51+
])
52+
->expectsOutput('Anonymous user timezone updated from [UTC] to [Europe/Madrid].')
53+
->assertExitCode(0);
54+
55+
$this->assertEquals('Europe/Madrid', $user->fresh()->timezone);
56+
}
57+
58+
public function testCanBeRunMultipleTimes(): void
59+
{
60+
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail();
61+
$user->timezone = 'America/Chicago';
62+
$user->save();
63+
64+
config(['app.anonymous_user_timezone' => 'America/Los_Angeles']);
65+
66+
$this->artisan('processmaker:update-anonymous-user-timezone')->assertExitCode(0);
67+
$this->artisan('processmaker:update-anonymous-user-timezone')
68+
->expectsOutput('Anonymous user timezone is already set to [America/Los_Angeles].')
69+
->assertExitCode(0);
70+
71+
$this->assertEquals('America/Los_Angeles', $user->fresh()->timezone);
72+
}
73+
74+
public function testFailsWhenAnonymousUserDoesNotExist(): void
75+
{
76+
User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->forceDelete();
77+
78+
$this->artisan('processmaker:update-anonymous-user-timezone')
79+
->expectsOutput('Anonymous user not found.')
80+
->assertExitCode(1);
81+
}
82+
}

0 commit comments

Comments
 (0)