From e3decdc30a8c33aeb3e4b6febc5ba486fedfd957 Mon Sep 17 00:00:00 2001 From: Triopsi Date: Wed, 25 Nov 2026 13:30:21 +0000 Subject: [PATCH] fix: bound savings replay and format negative amounts Reject savings overview months outside 2000-01 to 2100-12, format negative monetary values correctly, and add regression tests. --- app/Filament/Pages/SavingsOverview.php | 2 +- app/Support/Money.php | 5 ++- tests/Feature/ParticipantArchivingTest.php | 42 +++++++++++++++++++++ tests/Feature/SavingsOverviewTest.php | 33 ++++++++++++++++ tests/Feature/SupportUtilitiesTest.php | 44 ++++++++++++++++++++++ tests/Unit/MoneyTest.php | 3 ++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/SupportUtilitiesTest.php diff --git a/app/Filament/Pages/SavingsOverview.php b/app/Filament/Pages/SavingsOverview.php index 94768ed..d9791c9 100644 --- a/app/Filament/Pages/SavingsOverview.php +++ b/app/Filament/Pages/SavingsOverview.php @@ -56,7 +56,7 @@ public static function getNavigationGroup(): string protected function getViewData(): array { $data = ['budgets' => [], 'names' => [], 'total' => 0, 'closed' => false, 'problem' => null]; - if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D', $this->month) !== 1) { + if (preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D', $this->month) !== 1 || $this->month < '2000-01' || $this->month > '2100-12') { return [...$data, 'problem' => __('funding.savings.invalid_month')]; } $closing = MonthlyClosing::where('month', $this->month)->whereNotNull('closed_at')->first(); diff --git a/app/Support/Money.php b/app/Support/Money.php index 2eda815..b398c8e 100644 --- a/app/Support/Money.php +++ b/app/Support/Money.php @@ -34,7 +34,10 @@ public static function cents(mixed $amount, string $field = 'amount'): int */ public static function decimal(int $cents): string { - return sprintf('%d.%02d', intdiv($cents, 100), $cents % 100); + $euros = intdiv($cents, 100); + $remainder = abs($cents % 100); + + return ($cents < 0 ? '-' : '').sprintf('%d.%02d', abs($euros), $remainder); } /** diff --git a/tests/Feature/ParticipantArchivingTest.php b/tests/Feature/ParticipantArchivingTest.php index 4cacb75..93701c9 100644 --- a/tests/Feature/ParticipantArchivingTest.php +++ b/tests/Feature/ParticipantArchivingTest.php @@ -45,6 +45,48 @@ public function test_archiving_preserves_previous_months_and_closed_reports(): v $this->actingAs($admin)->get(route('closings.pdf', $closing))->assertOk(); } + /** An invalid archive month is rejected before it can be stored on the participant. */ + #[DataProvider('invalidArchiveMonths')] + public function test_invalid_archive_months_are_rejected(string $month): void + { + $person = Participant::create(['name' => 'Participant']); + + try { + app(ArchiveParticipant::class)->archive($person, $month); + $this->fail('Invalid archive month was accepted'); + } catch (ValidationException $exception) { + $this->assertArrayHasKey('archived_from_month', $exception->errors()); + } + + $this->assertNull($person->fresh()->archived_from_month); + } + + /** Supply malformed month values that must fail the archive boundary validator. + * @return array + */ + public static function invalidArchiveMonths(): array + { + return [ + 'empty' => [''], + 'wrong order' => ['02-2027'], + 'invalid month' => ['2027-13'], + ]; + } + + /** A completed one-off assignment before the archive boundary does not block retirement. */ + public function test_completed_assignments_before_the_archive_month_are_allowed(): void + { + $person = Participant::create(['name' => 'Participant']); + Income::create([ + 'name' => 'One-off receipt', 'participant_id' => $person->id, 'amount_cents' => 100, + 'start_month' => '2027-01', 'interval_months' => 0, + ]); + + app(ArchiveParticipant::class)->archive($person, '2027-02'); + + $this->assertSame('2027-02', $person->fresh()->archived_from_month); + } + /** Active income recipients, senders and expense shares require explicit reassignment first. */ #[DataProvider('blockingAssignments')] public function test_archiving_with_future_assignments_is_rejected(string $assignment, string $message): void diff --git a/tests/Feature/SavingsOverviewTest.php b/tests/Feature/SavingsOverviewTest.php index b882e5a..83065b3 100644 --- a/tests/Feature/SavingsOverviewTest.php +++ b/tests/Feature/SavingsOverviewTest.php @@ -10,7 +10,9 @@ use App\Services\CloseMonth; use Filament\Facades\Filament; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\DB; use Livewire\Livewire; +use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; /** Verify accumulated reserves, historical snapshots and explanatory navigation. */ @@ -67,4 +69,35 @@ public function test_empty_overview_and_invalid_month_are_handled(): void Livewire::test(SavingsOverview::class)->assertViewHas('total', 0)->assertSee(__('funding.no_budgets')) ->set('month', '2027-13')->assertSee(__('funding.savings.invalid_month')); } + + /** + * Reject out-of-range months before they can trigger a long historical budget replay. + */ + #[DataProvider('outOfRangeMonths')] + public function test_out_of_range_months_do_not_start_budget_calculation(string $month): void + { + DB::enableQueryLog(); + try { + Livewire::test(SavingsOverview::class, ['month' => $month]) + ->assertSee(__('funding.savings.invalid_month')); + $queries = DB::getQueryLog(); + } finally { + DB::disableQueryLog(); + DB::flushQueryLog(); + } + + $this->assertFalse(collect($queries)->contains(fn (array $query): bool => str_contains($query['query'], 'budgets'))); + } + + /** Months before or after the supported replay window must be rejected. + * @return array + */ + public static function outOfRangeMonths(): array + { + return [ + 'before lower bound' => ['1999-12'], + 'after upper bound' => ['2101-01'], + 'far future' => ['9998-12'], + ]; + } } diff --git a/tests/Feature/SupportUtilitiesTest.php b/tests/Feature/SupportUtilitiesTest.php new file mode 100644 index 0000000..d55c961 --- /dev/null +++ b/tests/Feature/SupportUtilitiesTest.php @@ -0,0 +1,44 @@ +assertNull(Gravatar::url('person@example.test')); + + Settings::set('gravatar_enabled', '1'); + Settings::set('gravatar_style', 'monsterid'); + + $this->assertNull(Gravatar::url(' ')); + $this->assertSame( + 'https://www.gravatar.com/avatar/'.md5('person@example.test').'?s=128&d=monsterid', + Gravatar::url(' Person@Example.Test ', 128), + ); + } + + /** Verify setting reads return defaults until a value is saved and mail templates remain configurable. */ + public function test_settings_return_defaults_and_persist_custom_values(): void + { + $this->assertSame('fallback', Settings::get('missing_setting', 'fallback')); + $this->assertFalse(Settings::gravatarEnabled()); + $this->assertSame('initials', Settings::gravatarStyle()); + $this->assertSame(__('mail.default_body'), Settings::mailBody()); + + Settings::set('mail_body', 'Hello {name}'); + Settings::set('gravatar_enabled', '1'); + + $this->assertSame('Hello {name}', Settings::mailBody()); + $this->assertTrue(Settings::gravatarEnabled()); + } +} diff --git a/tests/Unit/MoneyTest.php b/tests/Unit/MoneyTest.php index 1fba4b0..140f095 100644 --- a/tests/Unit/MoneyTest.php +++ b/tests/Unit/MoneyTest.php @@ -93,6 +93,8 @@ public function test_decimal_formats_cents_as_plain_decimal(): void $this->assertSame('1701.25', Money::decimal(170125)); $this->assertSame('0.00', Money::decimal(0)); $this->assertSame('0.05', Money::decimal(5)); + $this->assertSame('-0.05', Money::decimal(-5)); + $this->assertSame('-10.50', Money::decimal(-1050)); } /** @@ -105,5 +107,6 @@ public function test_format_outputs_german_currency(): void $this->assertSame('1.701,25 €', Money::format(170125)); $this->assertSame('0,00 €', Money::format(0)); $this->assertSame('0,05 €', Money::format(5)); + $this->assertSame('-0,05 €', Money::format(-5)); } }