Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions ProcessMaker/Console/Commands/UpdateAnonymousUserTimezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ProcessMaker\Console\Commands;

use Illuminate\Console\Command;
use ProcessMaker\Models\AnonymousUser;
use ProcessMaker\Models\User;

class UpdateAnonymousUserTimezone extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processmaker:update-anonymous-user-timezone
{--timezone= : Timezone to assign. Defaults to config app.anonymous_user_timezone}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the anonymous user timezone from config (safe to run multiple times)';

/**
* Execute the console command.
*/
public function handle(): int
{
$timezone = $this->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;
}
}
3 changes: 3 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

Expand Down
82 changes: 82 additions & 0 deletions tests/Feature/Console/UpdateAnonymousUserTimezoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Tests\Feature\Console;

use ProcessMaker\Models\AnonymousUser;
use ProcessMaker\Models\User;
use Tests\TestCase;

class UpdateAnonymousUserTimezoneTest extends TestCase
{
public function testUpdatesTimezoneFromConfig(): void
{
$user = User::where('username', AnonymousUser::ANONYMOUS_USERNAME)->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);
}
}
Loading