Skip to content
Merged
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
2 changes: 1 addition & 1 deletion app/Filament/Pages/SavingsOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion app/Support/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/ParticipantArchivingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{string}>
*/
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
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/SavingsOverviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<string, array{string}>
*/
public static function outOfRangeMonths(): array
{
return [
'before lower bound' => ['1999-12'],
'after upper bound' => ['2101-01'],
'far future' => ['9998-12'],
];
}
}
44 changes: 44 additions & 0 deletions tests/Feature/SupportUtilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Feature;

use App\Support\Gravatar;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/** Regression coverage for small settings-backed application helpers. */
class SupportUtilitiesTest extends TestCase
{
use RefreshDatabase;

/** Verify Gravatar URLs honor enablement, normalized e-mail addresses, size, and fallback style. */
public function test_gravatar_url_handles_disabled_blank_and_normalized_addresses(): void
{
$this->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());
}
}
3 changes: 3 additions & 0 deletions tests/Unit/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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));
}
}