From ebdf25e1a84acfbed9f63a1e3b59c41c8f647f9c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 29 Mar 2023 18:14:28 +1100 Subject: [PATCH] Use travel to move timezones --- app/Models/RecurringInvoice.php | 2 - tests/Unit/RecurringDatesTest.php | 183 +++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 5 deletions(-) diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index bb5092d610..83eed4296d 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -418,8 +418,6 @@ class RecurringInvoice extends BaseModel $offset = $this->client->timezone_offset(); - nlog("offset = {$offset}"); - /* If this setting is enabled, the recurring invoice may be set in the past */ if ($this->company->stop_on_unpaid_recurring) { diff --git a/tests/Unit/RecurringDatesTest.php b/tests/Unit/RecurringDatesTest.php index 47e734757f..e4c89c4b04 100644 --- a/tests/Unit/RecurringDatesTest.php +++ b/tests/Unit/RecurringDatesTest.php @@ -45,6 +45,186 @@ class RecurringDatesTest extends TestCase } + public function testDailyFrequencyCalc6() + { + $this->travelTo(now()->subHours(8)); + + $account = Account::factory()->create(); + + $settings = CompanySettings::defaults(); + $settings->entity_send_time = '1'; + $settings->timezone_id = '113'; + + $company = Company::factory()->create([ + 'account_id' => $account->id, + 'settings' => $settings, + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::factory()->create([ + 'account_id' => $account->id, + 'confirmation_code' => $this->createDbHash(config('database.default')), + 'email' => 'whiz@gmail.com', + ]); + + $userPermissions = collect([ + 'view_invoice', + 'view_client', + 'edit_client', + 'edit_invoice', + 'create_invoice', + 'create_client', + ]); + + $userSettings = DefaultSettings::userSettings(); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'notifications' => CompanySettings::notificationDefaults(), + 'permissions' => $userPermissions->toJson(), + 'settings' => json_encode($userSettings), + 'is_locked' => 0, + ]); + + $client = Client::factory()->create(['user_id' => $user->id, 'company_id' => $company->id]); + + ClientContact::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'is_primary' => 1, + ]); + + $recurring_invoice = RecurringInvoice::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'frequency_id' => RecurringInvoice::FREQUENCY_DAILY, + 'next_send_date' => now()->format('Y-m-d'), + 'next_send_date_client' => now()->format('Y-m-d'), + 'date' => now()->format('Y-m-d'), + 'remaining_cycles' => -1, + 'status_id' => 1, + ]); + + $recurring_invoice->service()->start()->save(); + + $this->assertEquals('1', $client->getSetting('entity_send_time')); + $this->assertEquals('113', $client->getSetting('timezone_id')); + + $this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date)->format('Y-m-d')); + $this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d')); + + $recurring_invoice->next_send_date = $recurring_invoice->nextSendDate(); + $recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient(); + $recurring_invoice->save(); + + $this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date)); + $this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d')); + + $recurring_invoice->next_send_date = $recurring_invoice->nextSendDate(); + $recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient(); + $recurring_invoice->save(); + + $this->assertEquals(now()->startOfDay()->addDays(2)->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date)); + $this->assertEquals(now()->addDays(2)->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d')); + + $this->travelBack(); + + } + + + + + public function testDailyFrequencyCalc5() + { + + $account = Account::factory()->create(); + + $settings = CompanySettings::defaults(); + $settings->entity_send_time = '23'; + $settings->timezone_id = '113'; + + $company = Company::factory()->create([ + 'account_id' => $account->id, + 'settings' => $settings, + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::factory()->create([ + 'account_id' => $account->id, + 'confirmation_code' => $this->createDbHash(config('database.default')), + 'email' => 'whiz@gmail.com', + ]); + + $userPermissions = collect([ + 'view_invoice', + 'view_client', + 'edit_client', + 'edit_invoice', + 'create_invoice', + 'create_client', + ]); + + $userSettings = DefaultSettings::userSettings(); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'notifications' => CompanySettings::notificationDefaults(), + 'permissions' => $userPermissions->toJson(), + 'settings' => json_encode($userSettings), + 'is_locked' => 0, + ]); + + $client = Client::factory()->create(['user_id' => $user->id, 'company_id' => $company->id]); + + ClientContact::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'is_primary' => 1, + ]); + + + + $recurring_invoice = RecurringInvoice::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'client_id' => $client->id, + 'frequency_id' => RecurringInvoice::FREQUENCY_DAILY, + 'next_send_date' => now()->format('Y-m-d'), + 'next_send_date_client' => now()->format('Y-m-d'), + 'date' => now()->format('Y-m-d'), + 'remaining_cycles' => -1, + 'status_id' => 1, + ]); + + $recurring_invoice->service()->start()->save(); + + $this->assertEquals('23', $client->getSetting('entity_send_time')); + $this->assertEquals('113', $client->getSetting('timezone_id')); + + $this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date)->format('Y-m-d')); + $this->assertEquals(now()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d')); + + $recurring_invoice->next_send_date = $recurring_invoice->nextSendDate(); + $recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient(); + $recurring_invoice->save(); + + $this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date)); + $this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d')); + + } + + public function testDailyFrequencyCalc4() { @@ -369,9 +549,6 @@ class RecurringDatesTest extends TestCase $recurring_invoice->next_send_date_client = $recurring_invoice->nextSendDateClient(); $recurring_invoice->save(); - nlog($recurring_invoice->next_send_date); - nlog($recurring_invoice->next_send_date_client); - $this->assertEquals(now()->startOfDay()->addDay()->addSeconds($client->timezone_offset()), Carbon::parse($recurring_invoice->next_send_date)); $this->assertEquals(now()->addDay()->format('Y-m-d'), Carbon::parse($recurring_invoice->next_send_date_client)->format('Y-m-d'));