88 lines
3.3 KiB
PHP
88 lines
3.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
use App\Models\Organization;
|
||
|
|
use App\Models\Plan;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\WorkspaceType;
|
||
|
|
use Laravel\Cashier\Subscription;
|
||
|
|
|
||
|
|
test('the default plans are seeded', function () {
|
||
|
|
expect(Plan::query()->orderBy('sort_order')->pluck('key')->all())
|
||
|
|
->toBe(['free', 'personal', 'family', 'organization', 'unlimited']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a plan casts its workspace type and limits', function () {
|
||
|
|
$free = Plan::query()->where('key', 'free')->firstOrFail();
|
||
|
|
|
||
|
|
expect($free->workspace_type)->toBe(WorkspaceType::Personal)
|
||
|
|
->and($free->is_active)->toBeTrue()
|
||
|
|
->and($free->seat_limit)->toBe(1)
|
||
|
|
->and($free->max_folders)->toBe(3)
|
||
|
|
->and($free->max_folder_depth)->toBe(1)
|
||
|
|
->and($free->max_notes_per_folder)->toBe(50)
|
||
|
|
->and($free->note_chars)->toBe(1000);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the unlimited plan has no limits', function () {
|
||
|
|
$unlimited = Plan::query()->where('key', 'unlimited')->firstOrFail();
|
||
|
|
|
||
|
|
expect($unlimited->max_folders)->toBeNull()
|
||
|
|
->and($unlimited->max_folder_depth)->toBeNull()
|
||
|
|
->and($unlimited->max_notes_per_folder)->toBeNull()
|
||
|
|
->and($unlimited->note_chars)->toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('only the personal plan is purchasable by default', function () {
|
||
|
|
expect(Plan::query()->where('key', 'personal')->firstOrFail()->isPurchasable())->toBeTrue()
|
||
|
|
->and(Plan::query()->where('key', 'free')->firstOrFail()->isPurchasable())->toBeFalse()
|
||
|
|
->and(Plan::query()->where('key', 'unlimited')->firstOrFail()->isPurchasable())->toBeFalse();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a plan created through the factory can be given caps and a price', function () {
|
||
|
|
$plan = Plan::factory()->limited(2, 1, 10, 200)->purchasable('price_abc')->create();
|
||
|
|
|
||
|
|
expect($plan->max_folders)->toBe(2)
|
||
|
|
->and($plan->note_chars)->toBe(200)
|
||
|
|
->and($plan->isPurchasable())->toBeTrue()
|
||
|
|
->and($plan->stripe_price)->toBe('price_abc');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('an organization belongs to a plan', function () {
|
||
|
|
$organization = Organization::factory()->withPlan('family')->create();
|
||
|
|
|
||
|
|
expect($organization->plan)->not->toBeNull()
|
||
|
|
->and($organization->plan->key)->toBe('family');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the factory user is given the unlimited plan', function () {
|
||
|
|
$user = User::factory()->create();
|
||
|
|
|
||
|
|
expect($user->currentOrganization()->first()->plan->key)->toBe('unlimited');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the factory user can be given a specific plan', function () {
|
||
|
|
$user = User::factory()->unsubscribed()->withFreePlan()->create();
|
||
|
|
|
||
|
|
expect($user->currentOrganization()->first()->plan->key)->toBe('free');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('the backfill assigns a plan to every existing workspace', function () {
|
||
|
|
$legacy = Organization::factory()->personal()->withLifetimeAccess()->create();
|
||
|
|
$subscriber = Organization::factory()->personal()->create();
|
||
|
|
$plain = Organization::factory()->personal()->create();
|
||
|
|
|
||
|
|
Subscription::factory()->active()
|
||
|
|
->withPrice(config('plans.plans.pro.price'))
|
||
|
|
->create(['organization_id' => $subscriber->id]);
|
||
|
|
|
||
|
|
// Simulate a database that predates the backfill.
|
||
|
|
Organization::query()->update(['plan_id' => null]);
|
||
|
|
|
||
|
|
$migration = require database_path('migrations/2026_10_16_043630_backfill_organization_plans.php');
|
||
|
|
$migration->up();
|
||
|
|
|
||
|
|
expect($legacy->refresh()->plan->key)->toBe('unlimited')
|
||
|
|
->and($subscriber->refresh()->plan->key)->toBe('personal')
|
||
|
|
->and($plain->refresh()->plan->key)->toBe('free');
|
||
|
|
});
|