New_Micro_Learning/backend/tests/Feature/Subscriptions/PlatformPlanTest.php

41 خطوط
2.2 KiB
PHP

<?php
namespace Tests\Feature\Subscriptions;
use App\Models\User;
use App\Modules\Identity\Domain\Enums\UserRole;
use App\Modules\Organizations\Domain\Organization;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class PlatformPlanTest extends TestCase
{
use RefreshDatabase;
public function test_super_admin_can_create_and_list_a_plan_with_limits(): void
{
Sanctum::actingAs(User::factory()->create(['role' => UserRole::SuperAdmin]));
$plan = $this->postJson('/api/v1/platform/plans', ['key' => 'growth', 'name' => 'Growth', 'description' => 'برای سازمان‌های در حال رشد', 'seatLimit' => 100, 'storageQuotaBytes' => 500000, 'aiCreditQuota' => 2000, 'enabledFeatures' => ['reports']])->assertCreated()->assertJsonPath('data.key', 'growth')->assertJsonPath('data.seatLimit', 100)->json('data.id');
$this->getJson('/api/v1/platform/plans')->assertOk()->assertJsonPath('data.0.id', $plan)->assertJsonPath('data.0.enabledFeatures.0', 'reports');
$this->assertDatabaseHas('audit_logs', ['action' => 'plan.created', 'entity_id' => $plan]);
}
public function test_tenant_user_cannot_manage_plans(): void
{
Sanctum::actingAs(User::factory()->create(['role' => UserRole::CourseDesigner]));
$this->getJson('/api/v1/platform/plans')->assertForbidden();
$this->postJson('/api/v1/platform/plans', ['key' => 'starter', 'name' => 'Starter'])->assertForbidden();
}
public function test_super_admin_can_assign_plan_to_organization_and_audit_it(): void
{
Sanctum::actingAs(User::factory()->create(['role' => UserRole::SuperAdmin]));
$organization = Organization::factory()->create();
$planId = $this->postJson('/api/v1/platform/plans', ['key' => 'pro', 'name' => 'Pro'])->json('data.id');
$this->patchJson('/api/v1/platform/organizations/'.$organization->id.'/plan', ['planId' => $planId])->assertOk()->assertJsonPath('data.planKey', 'pro');
$this->assertDatabaseHas('subscriptions', ['organization_id' => $organization->id, 'plan_key' => 'pro']);
$this->assertDatabaseHas('audit_logs', ['action' => 'organization.plan_assigned', 'organization_id' => $organization->id]);
}
}