presseportale/tests/Feature/Billing/AdminPaymentsPageTest.php
2026-06-12 13:54:53 +00:00

150 lines
4.7 KiB
PHP

<?php
use App\Enums\InvoiceStatus;
use App\Models\Invoice;
use App\Models\Plan;
use App\Models\SinglePurchase;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function paymentsPageAdmin(): User
{
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
return $admin;
}
test('the payments page shows subscriptions with plan name and mrr', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['name' => 'Abo Kunde']);
$plan = Plan::factory()->create([
'name' => 'Business',
'monthly_price_cents' => 4900,
'stripe_price_id_monthly' => 'price_test_m_biz',
]);
subscribeUserToPlan($customer, $plan);
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->assertSee('Aktive Abos')
->assertSee('MRR (netto)')
->assertSee('49,00 €')
->assertSee('Abo Kunde')
->assertSee('Business')
->assertSee('monatlich');
});
test('a yearly subscription contributes one twelfth to the mrr', function () {
/** @var TestCase $this */
$customer = User::factory()->create();
$plan = Plan::factory()->create([
'monthly_price_cents' => 4900,
'yearly_price_cents' => 49000,
'stripe_price_id_monthly' => 'price_test_m_y',
'stripe_price_id_yearly' => 'price_test_y_y',
]);
subscribeUserToPlan($customer, $plan, 'yearly');
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->assertSee('40,83 €')
->assertSee('jährlich');
});
test('single purchases appear with type and status', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['name' => 'Einzel Käufer']);
SinglePurchase::factory()->paid()->create(['user_id' => $customer->id]);
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->assertSee('Einzel Käufer')
->assertSee('Einzel-Pressemitteilung')
->assertSee('Bezahlt')
->assertSee('19,00 €');
});
test('local invoices appear with number and circle badge', function () {
/** @var TestCase $this */
$customer = User::factory()->create();
Invoice::factory()->create([
'user_id' => $customer->id,
'number' => 'STR-2026-000042',
'status' => InvoiceStatus::Paid->value,
'paid_at' => now(),
'stripe_invoice_id' => 'in_test_123',
]);
Invoice::factory()->create([
'user_id' => $customer->id,
'number' => 'MAN-2026-000007',
'status' => InvoiceStatus::Open->value,
'stripe_invoice_id' => null,
]);
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->assertSee('STR-2026-000042')
->assertSee('Stripe (STR)')
->assertSee('MAN-2026-000007')
->assertSee('Manuell (MAN)');
});
test('paid invoices of the last 30 days are summed as revenue', function () {
/** @var TestCase $this */
Invoice::factory()->create([
'status' => InvoiceStatus::Paid->value,
'paid_at' => now()->subDays(5),
'total_cents' => 11900,
]);
// Außerhalb des 30-Tage-Fensters: erscheint in der Tabelle,
// zählt aber nicht in die Umsatz-KPI (sonst stünde dort 1.118,00 €).
Invoice::factory()->create([
'status' => InvoiceStatus::Paid->value,
'paid_at' => now()->subDays(60),
'total_cents' => 99900,
]);
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->assertSee('Umsatz 30 Tage')
->assertSee('119,00 €')
->assertDontSee('1.118,00 €');
});
test('the search filters all panels by user name or email', function () {
/** @var TestCase $this */
$match = User::factory()->create(['name' => 'Maria Treffer']);
$other = User::factory()->create(['name' => 'Olaf Anders']);
SinglePurchase::factory()->paid()->create(['user_id' => $match->id]);
SinglePurchase::factory()->paid()->create(['user_id' => $other->id]);
$this->actingAs(paymentsPageAdmin());
LivewireVolt::test('admin.payments.index')
->set('search', 'Maria')
->assertSee('Maria Treffer')
->assertDontSee('Olaf Anders');
});
test('the payments page is not accessible for customers', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$this->actingAs($customer)
->get(route('admin.payments.index'))
->assertForbidden();
});