diff --git a/ProcessMaker/Console/Commands/UpdateAnonymousUserTimezone.php b/ProcessMaker/Console/Commands/UpdateAnonymousUserTimezone.php new file mode 100644 index 0000000000..ae83216480 --- /dev/null +++ b/ProcessMaker/Console/Commands/UpdateAnonymousUserTimezone.php @@ -0,0 +1,55 @@ +option('timezone') ?: config('app.anonymous_user_timezone', 'UTC'); + + $user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->first(); + + if (!$user) { + $this->error('Anonymous user not found.'); + + return self::FAILURE; + } + + if ($user->timezone === $timezone) { + $this->info("Anonymous user timezone is already set to [{$timezone}]."); + + return self::SUCCESS; + } + + $previousTimezone = $user->timezone; + $user->timezone = $timezone; + $user->save(); + + $this->info("Anonymous user timezone updated from [{$previousTimezone}] to [{$timezone}]."); + + return self::SUCCESS; + } +} diff --git a/config/app.php b/config/app.php index 2d3855b971..b3c5891f98 100644 --- a/config/app.php +++ b/config/app.php @@ -32,6 +32,9 @@ // The timezone for the application 'timezone' => env('APP_TIMEZONE', 'America/Los_Angeles'), + // The timezone for the anonymous user + 'anonymous_user_timezone' => env('ANONYMOUS_USER_TIMEZONE', 'UTC'), + // The time format for the application 'dateformat' => env('DATE_FORMAT', 'm/d/Y h:i A'), diff --git a/tests/Feature/Console/UpdateAnonymousUserTimezoneTest.php b/tests/Feature/Console/UpdateAnonymousUserTimezoneTest.php new file mode 100644 index 0000000000..51d7ca39bb --- /dev/null +++ b/tests/Feature/Console/UpdateAnonymousUserTimezoneTest.php @@ -0,0 +1,82 @@ +firstOrFail(); + $user->timezone = 'America/Chicago'; + $user->save(); + + config(['app.anonymous_user_timezone' => 'America/New_York']); + + $this->artisan('processmaker:update-anonymous-user-timezone') + ->expectsOutput('Anonymous user timezone updated from [America/Chicago] to [America/New_York].') + ->assertExitCode(0); + + $this->assertEquals('America/New_York', $user->fresh()->timezone); + } + + public function testDoesNothingWhenTimezoneAlreadyMatches(): void + { + $user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + $user->timezone = 'UTC'; + $user->save(); + + config(['app.anonymous_user_timezone' => 'UTC']); + + $this->artisan('processmaker:update-anonymous-user-timezone') + ->expectsOutput('Anonymous user timezone is already set to [UTC].') + ->assertExitCode(0); + + $this->assertEquals('UTC', $user->fresh()->timezone); + } + + public function testUpdatesTimezoneFromOption(): void + { + $user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + $user->timezone = 'UTC'; + $user->save(); + + config(['app.anonymous_user_timezone' => 'UTC']); + + $this->artisan('processmaker:update-anonymous-user-timezone', [ + '--timezone' => 'Europe/Madrid', + ]) + ->expectsOutput('Anonymous user timezone updated from [UTC] to [Europe/Madrid].') + ->assertExitCode(0); + + $this->assertEquals('Europe/Madrid', $user->fresh()->timezone); + } + + public function testCanBeRunMultipleTimes(): void + { + $user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->firstOrFail(); + $user->timezone = 'America/Chicago'; + $user->save(); + + config(['app.anonymous_user_timezone' => 'America/Los_Angeles']); + + $this->artisan('processmaker:update-anonymous-user-timezone')->assertExitCode(0); + $this->artisan('processmaker:update-anonymous-user-timezone') + ->expectsOutput('Anonymous user timezone is already set to [America/Los_Angeles].') + ->assertExitCode(0); + + $this->assertEquals('America/Los_Angeles', $user->fresh()->timezone); + } + + public function testFailsWhenAnonymousUserDoesNotExist(): void + { + User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->forceDelete(); + + $this->artisan('processmaker:update-anonymous-user-timezone') + ->expectsOutput('Anonymous user not found.') + ->assertExitCode(1); + } +}