migrant-web/tests/Feature/AdminSubscriptionPlansTest.php

143 lines
4.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Plan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminSubscriptionPlansTest extends TestCase
{
use RefreshDatabase;
protected $admin;
protected function setUp(): void
{
parent::setUp();
$this->admin = User::create([
'name' => 'Admin User',
'email' => 'admin@example.com',
'password' => bcrypt('password'),
'role' => 'admin',
]);
}
public function test_admin_can_view_subscription_plans()
{
// Seed default plans are loaded from the migration, let's verify
$this->assertDatabaseHas('plans', ['id' => 'basic']);
$response = $this->actingAs($this->admin)->get('/admin/subscriptions');
$response->assertStatus(200);
}
public function test_admin_can_create_new_plan()
{
$response = $this->actingAs($this->admin)->post('/admin/subscriptions', [
'id' => 'super-vip',
'name' => 'Super VIP Plan',
'price' => 999.00,
'duration' => 'Monthly',
'features' => ['Super feature 1', 'Super feature 2'],
'max_contact_people' => null,
'unlimited_contacts' => true,
'enable_job_apply' => true,
'status' => 'Active',
]);
$response->assertStatus(302);
$this->assertDatabaseHas('plans', [
'id' => 'super-vip',
'name' => 'Super VIP Plan',
'price' => 999.00,
'duration' => 'Monthly',
'unlimited_contacts' => true,
'enable_job_apply' => true,
'status' => 'Active',
]);
}
public function test_admin_can_update_existing_plan()
{
$response = $this->actingAs($this->admin)->post('/admin/subscriptions/basic/update', [
'name' => 'Basic Search Updated',
'price' => 120.00,
'duration' => 'Monthly',
'features' => ['Browse 500+ workers', '10 Shortlists', 'New feature'],
'max_contact_people' => 15,
'unlimited_contacts' => false,
'enable_job_apply' => false,
'status' => 'Active',
]);
$response->assertStatus(302);
$this->assertDatabaseHas('plans', [
'id' => 'basic',
'name' => 'Basic Search Updated',
'price' => 120.00,
'max_contact_people' => 15,
'unlimited_contacts' => false,
'status' => 'Active',
]);
}
public function test_admin_can_delete_plan()
{
$response = $this->actingAs($this->admin)->delete('/admin/subscriptions/basic');
$response->assertStatus(302);
$this->assertDatabaseMissing('plans', [
'id' => 'basic',
]);
}
public function test_admin_cannot_disable_plan_if_in_use()
{
// Associate a sponsor with 'basic' plan
\App\Models\Sponsor::create([
'full_name' => 'Test Sponsor',
'email' => 'sponsor@example.com',
'mobile' => '+971501112233',
'password' => bcrypt('password'),
'subscription_status' => 'active',
'subscription_plan' => 'basic',
]);
$response = $this->actingAs($this->admin)->post('/admin/subscriptions/basic/update', [
'name' => 'Basic Search',
'price' => 99.00,
'duration' => 'Monthly',
'features' => ['Browse 500+ workers'],
'max_contact_people' => 10,
'unlimited_contacts' => false,
'enable_job_apply' => false,
'status' => 'Disabled',
]);
$response->assertSessionHasErrors(['status']);
$this->assertEquals('Active', Plan::find('basic')->status);
}
public function test_admin_cannot_delete_plan_if_in_use()
{
// Associate a sponsor with 'basic' plan
\App\Models\Sponsor::create([
'full_name' => 'Test Sponsor',
'email' => 'sponsor@example.com',
'mobile' => '+971501112233',
'password' => bcrypt('password'),
'subscription_status' => 'active',
'subscription_plan' => 'basic',
]);
$response = $this->actingAs($this->admin)->delete('/admin/subscriptions/basic');
$response->assertSessionHasErrors(['status']);
$this->assertDatabaseHas('plans', ['id' => 'basic']);
}
}