mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-13 02:32:15 +01:00
Fixed test case and travelTo function in test cases
This commit is contained in:
committed by
Constantin Graf
parent
86555664c5
commit
b0cdeb3e33
@@ -5,8 +5,10 @@ declare(strict_types=1);
|
||||
namespace Tests;
|
||||
|
||||
use App\Service\PermissionStore;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use TiMacDonald\Log\LogFake;
|
||||
|
||||
@@ -32,4 +34,17 @@ abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
$this->assertEqualsCanonicalizing($ids, $models->pluck('id')->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current time to the given time.
|
||||
* This method fixes a bug, that setting the test now with Carbon::setTestNow() with a Carbon instance that has a timezone set, will not work as expected.
|
||||
* IT will also set the timezone for model casts with type "datetime" to the timezone and not use the timezone configured in the configuration "app.timezone".
|
||||
*
|
||||
* @param Carbon|CarbonImmutable $date
|
||||
* @param callable|null $callback
|
||||
*/
|
||||
public function travelTo($date, $callback = null): void
|
||||
{
|
||||
parent::travelTo($date->utc());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,39 +240,36 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_more_time_entries_than_limit_with_a_timezone_edge_case(): void
|
||||
{
|
||||
// Arrange
|
||||
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||
$this->freezeTime($now);
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$data->user->timezone = 'America/New_York';
|
||||
$data->user->save();
|
||||
/**
|
||||
* We create in the eyes of the users timezone 2 time entries yesterday, 5 time entries two days ago, and 3 time entries three days ago
|
||||
* We create in the eyes of the users timezone 2 time entries yesterday, 2 time entries two days ago, and 3 time entries three days ago
|
||||
* The time entries are created in a way that they jump to the next day if the endpoint ignores the users timezone and just uses UTC
|
||||
*/
|
||||
|
||||
// Note: This entry is yesterday in user timezone and yesterday in UTC
|
||||
$timeEntriesDay1InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->state([
|
||||
'start' => Carbon::now($data->user->timezone)->subDay()->startOfDay()->utc(),
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(),
|
||||
])
|
||||
->createMany(2);
|
||||
//dump($timeEntriesDay1InUserTimeZone->first()->refresh()->start->toImmutable()->timezone('UTC')->toDateString());
|
||||
//dump($timeEntriesDay1InUserTimeZone->first()->refresh()->start->toImmutable()->timezone($data->user->timezone)->toDateString());
|
||||
// Note: This entry is yesterday in UTC timezone, but two days ago in user timezone
|
||||
$timeEntriesDay1InUTC = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->state([
|
||||
'start' => Carbon::now('UTC')->subDay()->startOfDay()->utc(),
|
||||
'start' => Carbon::now()->utc()->subDay()->startOfDay()->utc(),
|
||||
])
|
||||
->createMany(2);
|
||||
//dump($timeEntriesDay1InUTC->first()->refresh()->start->toImmutable()->timezone('UTC')->toDateString());
|
||||
//dump($timeEntriesDay1InUTC->first()->refresh()->start->toImmutable()->timezone($data->user->timezone)->toDateString());
|
||||
// Note: This entry is two days ago in user timezone
|
||||
$timeEntriesDay2InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->state([
|
||||
'start' => Carbon::now($data->user->timezone)->subDays(2)->startOfDay()->utc(),
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->startOfDay()->utc(),
|
||||
])
|
||||
->createMany(3);
|
||||
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
@@ -291,12 +288,21 @@ class TimeEntryEndpointTest extends ApiEndpointTestAbstract
|
||||
public function test_index_endpoint_filter_only_full_dates_returns_time_entries_for_the_whole_day_case_more_time_entries_in_latest_day_than_limit(): void
|
||||
{
|
||||
// Arrange
|
||||
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||
$this->freezeTime($now);
|
||||
$data = $this->createUserWithPermission([
|
||||
'time-entries:view:own',
|
||||
]);
|
||||
$timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->startBetween(Carbon::now()->subDay()->startOfDay(), Carbon::now()->subDay()->endOfDay(), true)
|
||||
->state([
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(),
|
||||
])
|
||||
->createMany(7);
|
||||
$timeEntriesDay2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||
->state([
|
||||
'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->endOfDay()->utc(),
|
||||
])
|
||||
->createMany(3);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -13,7 +13,6 @@ use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use App\Service\DashboardService;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
@@ -261,7 +260,7 @@ class DashboardServiceTest extends TestCase
|
||||
{
|
||||
// Arrange
|
||||
// Note: Is a Monday
|
||||
$now = CarbonImmutable::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna')->toImmutable();
|
||||
$this->travelTo($now);
|
||||
$user = User::factory()->create([
|
||||
'timezone' => 'Europe/Vienna',
|
||||
@@ -337,7 +336,7 @@ class DashboardServiceTest extends TestCase
|
||||
{
|
||||
// Arrange
|
||||
// Note: Is a Monday
|
||||
$now = CarbonImmutable::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna')->toImmutable();
|
||||
$this->travelTo($now);
|
||||
$organization = Organization::factory()->create();
|
||||
$user = User::factory()->create([
|
||||
|
||||
Reference in New Issue
Block a user