mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 20:22: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
@@ -9,7 +9,7 @@ use App\Service\Import\Importers\ImporterContract;
|
|||||||
use App\Service\Import\Importers\ImporterProvider;
|
use App\Service\Import\Importers\ImporterProvider;
|
||||||
use App\Service\Import\Importers\ImportException;
|
use App\Service\Import\Importers\ImportException;
|
||||||
use App\Service\Import\Importers\ReportDto;
|
use App\Service\Import\Importers\ReportDto;
|
||||||
use Carbon\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
'personal_team' => false,
|
'personal_team' => false,
|
||||||
'currency' => 'EUR',
|
'currency' => 'EUR',
|
||||||
]);
|
]);
|
||||||
$userRivalManager = User::factory()->withPersonalOrganization()->create([
|
$userAcmeManager = User::factory()->withPersonalOrganization()->create([
|
||||||
'name' => 'Acme Manager',
|
'name' => 'Acme Manager',
|
||||||
'email' => 'test@example.com',
|
'email' => 'test@example.com',
|
||||||
]);
|
]);
|
||||||
@@ -57,7 +57,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
'password' => null,
|
'password' => null,
|
||||||
]);
|
]);
|
||||||
$userAcmeOwnerMember = Member::factory()->forUser($userAcmeOwner)->forOrganization($organizationAcme)->role(Role::Owner)->create();
|
$userAcmeOwnerMember = Member::factory()->forUser($userAcmeOwner)->forOrganization($organizationAcme)->role(Role::Owner)->create();
|
||||||
$userAcmeManagerMember = Member::factory()->forUser($userRivalManager)->forOrganization($organizationAcme)->role(Role::Manager)->create();
|
$userAcmeManagerMember = Member::factory()->forUser($userAcmeManager)->forOrganization($organizationAcme)->role(Role::Manager)->create();
|
||||||
$userAcmeAdminMember = Member::factory()->forUser($userAcmeAdmin)->forOrganization($organizationAcme)->role(Role::Admin)->create();
|
$userAcmeAdminMember = Member::factory()->forUser($userAcmeAdmin)->forOrganization($organizationAcme)->role(Role::Admin)->create();
|
||||||
$userAcmeEmployeeMember = Member::factory()->forUser($userAcmeEmployee)->forOrganization($organizationAcme)->role(Role::Employee)->create();
|
$userAcmeEmployeeMember = Member::factory()->forUser($userAcmeEmployee)->forOrganization($organizationAcme)->role(Role::Employee)->create();
|
||||||
$userAcmePlaceholderMember = Member::factory()->forUser($userAcmePlaceholder)->forOrganization($organizationAcme)->role(Role::Placeholder)->create();
|
$userAcmePlaceholderMember = Member::factory()->forUser($userAcmePlaceholder)->forOrganization($organizationAcme)->role(Role::Placeholder)->create();
|
||||||
@@ -67,6 +67,10 @@ class DatabaseSeeder extends Seeder
|
|||||||
->count(10)
|
->count(10)
|
||||||
->forMember($userAcmeAdminMember)
|
->forMember($userAcmeAdminMember)
|
||||||
->create();
|
->create();
|
||||||
|
TimeEntry::factory()
|
||||||
|
->count(10)
|
||||||
|
->forMember($userAcmeManagerMember)
|
||||||
|
->create();
|
||||||
TimeEntry::factory()
|
TimeEntry::factory()
|
||||||
->count(10)
|
->count(10)
|
||||||
->forMember($userAcmePlaceholderMember)
|
->forMember($userAcmePlaceholderMember)
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ declare(strict_types=1);
|
|||||||
namespace Tests;
|
namespace Tests;
|
||||||
|
|
||||||
use App\Service\PermissionStore;
|
use App\Service\PermissionStore;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Mail;
|
use Illuminate\Support\Facades\Mail;
|
||||||
use TiMacDonald\Log\LogFake;
|
use TiMacDonald\Log\LogFake;
|
||||||
|
|
||||||
@@ -32,4 +34,17 @@ abstract class TestCase extends BaseTestCase
|
|||||||
{
|
{
|
||||||
$this->assertEqualsCanonicalizing($ids, $models->pluck('id')->toArray());
|
$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
|
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
|
// Arrange
|
||||||
|
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||||
|
$this->freezeTime($now);
|
||||||
$data = $this->createUserWithPermission([
|
$data = $this->createUserWithPermission([
|
||||||
'time-entries:view:own',
|
'time-entries:view:own',
|
||||||
]);
|
]);
|
||||||
$data->user->timezone = 'America/New_York';
|
$data->user->timezone = 'America/New_York';
|
||||||
$data->user->save();
|
$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
|
* 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
|
// Note: This entry is yesterday in user timezone and yesterday in UTC
|
||||||
$timeEntriesDay1InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
$timeEntriesDay1InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||||
->state([
|
->state([
|
||||||
'start' => Carbon::now($data->user->timezone)->subDay()->startOfDay()->utc(),
|
'start' => Carbon::now()->timezone($data->user->timezone)->subDay()->startOfDay()->utc(),
|
||||||
])
|
])
|
||||||
->createMany(2);
|
->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
|
// Note: This entry is yesterday in UTC timezone, but two days ago in user timezone
|
||||||
$timeEntriesDay1InUTC = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
$timeEntriesDay1InUTC = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||||
->state([
|
->state([
|
||||||
'start' => Carbon::now('UTC')->subDay()->startOfDay()->utc(),
|
'start' => Carbon::now()->utc()->subDay()->startOfDay()->utc(),
|
||||||
])
|
])
|
||||||
->createMany(2);
|
->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
|
// Note: This entry is two days ago in user timezone
|
||||||
$timeEntriesDay2InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
$timeEntriesDay2InUserTimeZone = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
||||||
->state([
|
->state([
|
||||||
'start' => Carbon::now($data->user->timezone)->subDays(2)->startOfDay()->utc(),
|
'start' => Carbon::now()->timezone($data->user->timezone)->subDays(2)->startOfDay()->utc(),
|
||||||
])
|
])
|
||||||
->createMany(3);
|
->createMany(3);
|
||||||
|
|
||||||
Passport::actingAs($data->user);
|
Passport::actingAs($data->user);
|
||||||
|
|
||||||
// Act
|
// 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
|
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
|
// Arrange
|
||||||
|
$now = Carbon::create(2024, 1, 1, 12, 0, 0, 'Europe/Vienna');
|
||||||
|
$this->freezeTime($now);
|
||||||
$data = $this->createUserWithPermission([
|
$data = $this->createUserWithPermission([
|
||||||
'time-entries:view:own',
|
'time-entries:view:own',
|
||||||
]);
|
]);
|
||||||
$timeEntriesDay1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)
|
$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);
|
->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);
|
Passport::actingAs($data->user);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ use App\Models\Task;
|
|||||||
use App\Models\TimeEntry;
|
use App\Models\TimeEntry;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Service\DashboardService;
|
use App\Service\DashboardService;
|
||||||
use Carbon\CarbonImmutable;
|
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
@@ -261,7 +260,7 @@ class DashboardServiceTest extends TestCase
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
// Note: Is a Monday
|
// 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);
|
$this->travelTo($now);
|
||||||
$user = User::factory()->create([
|
$user = User::factory()->create([
|
||||||
'timezone' => 'Europe/Vienna',
|
'timezone' => 'Europe/Vienna',
|
||||||
@@ -337,7 +336,7 @@ class DashboardServiceTest extends TestCase
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
// Note: Is a Monday
|
// 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);
|
$this->travelTo($now);
|
||||||
$organization = Organization::factory()->create();
|
$organization = Organization::factory()->create();
|
||||||
$user = User::factory()->create([
|
$user = User::factory()->create([
|
||||||
|
|||||||
Reference in New Issue
Block a user