added pricing, orgs
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-04 20:34:19 -07:00
parent 999d5ad28d
commit 2ca3860557
52 changed files with 3467 additions and 47 deletions
+218
View File
@@ -0,0 +1,218 @@
<?php
use App\Models\Organization;
use App\Models\Setting;
use App\Models\User;
use Livewire\Livewire;
test('guests are redirected from the plan pricing page', function () {
$this->get(route('admin.plan-pricing'))
->assertRedirect(route('login'));
});
test('non-admins cannot access the plan pricing page', function () {
$this->actingAs(User::factory()->create());
$this->get(route('admin.plan-pricing'))->assertForbidden();
});
test('non-admins do not see the plan pricing navigation item', function () {
$this->actingAs(User::factory()->create());
$this->get(route('dashboard'))
->assertOk()
->assertDontSee('Plan pricing');
});
test('admins see the plan pricing navigation item', function () {
$this->actingAs(User::factory()->asAdmin()->create());
$this->get(route('dashboard'))
->assertOk()
->assertSee('Plan pricing');
});
test('admins can view the plan pricing page', function () {
$this->actingAs(User::factory()->asAdmin()->create());
$this->get(route('admin.plan-pricing'))->assertOk();
});
test('the plan pricing page shows the current price for each configured plan', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
'team' => ['name' => 'Team', 'price' => 'price_team', 'interval' => 'year'],
]);
Setting::put(Setting::planAmountKey('pro'), '1250');
Setting::put(Setting::planAmountKey('team'), '9900');
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->assertOk()
->assertSet('amounts.pro', '12.50')
->assertSet('amounts.team', '99.00');
});
test('the plan price field is pre-filled with the stored price', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
Setting::put(Setting::planAmountKey('pro'), '1250');
$this->actingAs(User::factory()->asAdmin()->create());
// A pre-filled field is what stops an administrator saving a blank price
// over a real one, so the value has to reach the rendered input.
expect(preg_replace('/\s+/', ' ', Livewire::test('pages::admin.plan-pricing')->html()))
->toContain('value="12.50"');
});
test('a plan without a Stripe price is still listed but marked as not on sale', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => null, 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->assertOk()
->assertSee('This plan is not on sale yet');
});
test('a deployment with no plans configured says so', function () {
config()->set('plans.plans', []);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->assertOk()
->assertSee('No plans are configured');
});
test('an admin can change the advertised price of a plan', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '12.50')
->call('saveAmounts')
->assertHasNoErrors();
expect(Setting::get(Setting::planAmountKey('pro')))->toBe('1250')
->and(Organization::planAmount('pro'))->toBe(1250);
});
test('an admin can give a plan away for free', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '0')
->call('saveAmounts')
->assertHasNoErrors();
expect(Organization::planAmount('pro'))->toBe(0);
});
test('a price with cents is stored without floating point drift', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '0.07')
->call('saveAmounts')
->assertHasNoErrors();
expect(Organization::planAmount('pro'))->toBe(7);
});
test('a negative price is rejected and the stored price is left alone', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '-5')
->call('saveAmounts')
->assertHasErrors(['amounts.pro']);
expect(Organization::planAmount('pro'))->toBe(1000);
});
test('a price with more than two decimal places is rejected', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '1.234')
->call('saveAmounts')
->assertHasErrors(['amounts.pro']);
});
test('an emptied price is rejected rather than stored as a free plan', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
$this->actingAs(User::factory()->asAdmin()->create());
Livewire::test('pages::admin.plan-pricing')
->set('amounts.pro', '')
->call('saveAmounts')
->assertHasErrors(['amounts.pro']);
expect(Organization::planAmount('pro'))->toBe(1000);
});
test('a stored price that is not a number falls back to the default', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
]);
Setting::put(Setting::planAmountKey('pro'), 'free');
expect(Organization::planAmount('pro'))->toBe(1000);
});
test('a plan with no stored price falls back to the default', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => 'price_pro', 'interval' => 'month'],
'team' => ['name' => 'Team', 'price' => 'price_team', 'interval' => 'year'],
]);
Setting::put(Setting::planAmountKey('pro'), '2500');
expect(Organization::planAmount('pro'))->toBe(2500)
->and(Organization::planAmount('team'))->toBe(1000);
});
test('the amount is formatted in the configured currency', function () {
expect(Organization::formatAmount(1250))->toBe('$12.50');
config()->set('cashier.currency', 'eur');
expect(Organization::formatAmount(1250))->toContain('12.50');
});
test('an unrecognised currency falls back to a plain number rather than a blank price', function () {
config()->set('cashier.currency', 'x');
expect(Organization::formatAmount(1250))->toBe('12.50');
});