111 lines
3.7 KiB
PHP
111 lines
3.7 KiB
PHP
<?php
|
|
|
|
use App\Models\Organization;
|
|
use App\Models\User;
|
|
use App\OrganizationRole;
|
|
use Laravel\Cashier\Subscription;
|
|
|
|
test('every new account is given a personal organization that is current', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
$organization = $user->currentOrganization()->first();
|
|
|
|
expect($organization)->not->toBeNull()
|
|
->and($organization->personal_workspace)->toBeTrue()
|
|
->and($organization->lifetime_access)->toBeFalse()
|
|
->and($organization->userHasRole($user, OrganizationRole::Owner))->toBeTrue();
|
|
});
|
|
|
|
test('an active subscription entitles the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
expect($user->hasActiveSubscription())->toBeFalse();
|
|
|
|
Subscription::factory()->active()->create([
|
|
'organization_id' => $user->current_organization_id,
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeTrue();
|
|
});
|
|
|
|
test('a trialing subscription entitles the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
Subscription::factory()->trialing(now()->addDays(7))->create([
|
|
'organization_id' => $user->current_organization_id,
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeTrue();
|
|
});
|
|
|
|
test('a canceled subscription that has not lapsed yet still entitles the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
Subscription::factory()->create([
|
|
'organization_id' => $user->current_organization_id,
|
|
'stripe_status' => 'canceled',
|
|
'ends_at' => now()->addDays(14),
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeTrue();
|
|
});
|
|
|
|
test('a canceled subscription that has lapsed does not entitle the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
Subscription::factory()->create([
|
|
'organization_id' => $user->current_organization_id,
|
|
'stripe_status' => 'canceled',
|
|
'ends_at' => now()->subDay(),
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeFalse();
|
|
});
|
|
|
|
test('a past due subscription does not entitle the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
Subscription::factory()->pastDue()->create([
|
|
'organization_id' => $user->current_organization_id,
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeFalse();
|
|
});
|
|
|
|
test('a lifetime organization is entitled without any subscription', function () {
|
|
$organization = Organization::factory()->withLifetimeAccess()->create();
|
|
|
|
expect($organization->isEntitled())->toBeTrue()
|
|
->and($organization->hasLifetimeAccess())->toBeTrue();
|
|
});
|
|
|
|
test('an organization without a subscription is not entitled', function () {
|
|
$organization = Organization::factory()->create();
|
|
|
|
expect($organization->isEntitled())->toBeFalse();
|
|
});
|
|
|
|
test('a user without a current organization is never entitled', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
|
|
$user->forceFill(['current_organization_id' => null])->save();
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeFalse();
|
|
});
|
|
|
|
test('a subscription on another organization does not entitle the user', function () {
|
|
$user = User::factory()->unsubscribed()->create();
|
|
$other = Organization::factory()->create();
|
|
|
|
Subscription::factory()->active()->create([
|
|
'organization_id' => $other->id,
|
|
]);
|
|
|
|
expect($user->refresh()->hasActiveSubscription())->toBeFalse();
|
|
});
|
|
|
|
test('the factory user is entitled by default and unsubscribed opts out', function () {
|
|
expect(User::factory()->create()->hasActiveSubscription())->toBeTrue()
|
|
->and(User::factory()->unsubscribed()->create()->hasActiveSubscription())->toBeFalse();
|
|
});
|