239 lines
7.8 KiB
PHP
239 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Sponsor;
|
|
use App\Models\EmployerProfile;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AdminEmployerCharityTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected $admin;
|
|
protected $employer;
|
|
protected $charity;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// 1. Create an Admin user
|
|
$this->admin = User::create([
|
|
'name' => 'System Admin',
|
|
'email' => 'admin@marketplace.com',
|
|
'password' => bcrypt('password'),
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
// 2. Create an Employer User
|
|
$this->employer = User::create([
|
|
'name' => 'John Employer',
|
|
'email' => 'john@employer.com',
|
|
'password' => bcrypt('password'),
|
|
'role' => 'employer',
|
|
]);
|
|
|
|
EmployerProfile::create([
|
|
'user_id' => $this->employer->id,
|
|
'company_name' => 'John Enterprises',
|
|
'phone' => '+971501112222',
|
|
'country' => 'Dubai',
|
|
'verification_status' => 'pending',
|
|
]);
|
|
|
|
// Employers have a matching Sponsor record in our schema
|
|
Sponsor::create([
|
|
'full_name' => 'John Employer',
|
|
'email' => 'john@employer.com',
|
|
'mobile' => '+971501112222',
|
|
'password' => bcrypt('password'),
|
|
'organization_name' => 'John Enterprises',
|
|
'city' => 'Dubai',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
// 3. Create a Charity Organization (Sponsor)
|
|
$this->charity = Sponsor::create([
|
|
'full_name' => 'Charity Agent',
|
|
'organization_name' => 'Red Crescent',
|
|
'email' => 'contact@redcrescent.org',
|
|
'mobile' => '+971503334444',
|
|
'password' => bcrypt('password'),
|
|
'city' => 'Abu Dhabi',
|
|
'status' => 'active',
|
|
'subscription_status' => 'active',
|
|
'subscription_plan' => 'premium',
|
|
'is_verified' => true,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test admin can access employers listing page.
|
|
*/
|
|
public function test_admin_can_list_employers()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->get('/admin/employers');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee('John Employer');
|
|
}
|
|
|
|
/**
|
|
* Test admin can update an employer profile.
|
|
*/
|
|
public function test_admin_can_update_employer()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/employers/{$this->employer->id}/update", [
|
|
'name' => 'John Employer Updated',
|
|
'email' => 'john.updated@employer.com',
|
|
'company_name' => 'John MegaCorp',
|
|
'phone' => '+971509999999',
|
|
'country' => 'Abu Dhabi',
|
|
'subscription_status' => 'active',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'id' => $this->employer->id,
|
|
'name' => 'John Employer Updated',
|
|
'email' => 'john.updated@employer.com',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('employer_profiles', [
|
|
'user_id' => $this->employer->id,
|
|
'company_name' => 'John MegaCorp',
|
|
'phone' => '+971509999999',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test admin can verify an employer.
|
|
*/
|
|
public function test_admin_can_verify_employer()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/employers/{$this->employer->id}/verify");
|
|
|
|
$response->assertStatus(302);
|
|
$this->assertEquals('approved', $this->employer->fresh()->employerProfile->verification_status);
|
|
|
|
$sponsor = Sponsor::where('email', $this->employer->email)->first();
|
|
$this->assertEquals('active', $sponsor->status);
|
|
$this->assertTrue((bool)$sponsor->is_verified);
|
|
}
|
|
|
|
/**
|
|
* Test admin can suspend and activate an employer.
|
|
*/
|
|
public function test_admin_can_suspend_and_activate_employer()
|
|
{
|
|
// Suspend
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/employers/{$this->employer->id}/suspend");
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$sponsor = Sponsor::where('email', $this->employer->email)->first();
|
|
$this->assertEquals('suspended', $sponsor->status);
|
|
|
|
// Activate
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/employers/{$this->employer->id}/activate");
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$sponsor = Sponsor::where('email', $this->employer->email)->first();
|
|
$this->assertEquals('active', $sponsor->status);
|
|
}
|
|
|
|
/**
|
|
* Test admin can delete an employer.
|
|
*/
|
|
public function test_admin_can_delete_employer()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->delete("/admin/employers/{$this->employer->id}");
|
|
|
|
$response->assertStatus(302);
|
|
$this->assertDatabaseMissing('users', ['id' => $this->employer->id]);
|
|
$this->assertDatabaseMissing('employer_profiles', ['user_id' => $this->employer->id]);
|
|
$this->assertDatabaseMissing('sponsors', ['email' => $this->employer->email]);
|
|
}
|
|
|
|
/**
|
|
* Test admin can access charity organizations listing page.
|
|
*/
|
|
public function test_admin_can_list_charities()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->get('/admin/charity-organizations');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee('Red Crescent');
|
|
}
|
|
|
|
/**
|
|
* Test admin can update a charity organization.
|
|
*/
|
|
public function test_admin_can_update_charity()
|
|
{
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/charity-organizations/{$this->charity->id}/update", [
|
|
'full_name' => 'Charity Agent Updated',
|
|
'organization_name' => 'Red Crescent UAE',
|
|
'email' => 'contact.ae@redcrescent.org',
|
|
'mobile' => '+971505555555',
|
|
'city' => 'Sharjah',
|
|
'nationality' => 'Emirati',
|
|
'address' => 'Corniche Road',
|
|
'subscription_plan' => 'vip',
|
|
'subscription_status' => 'active',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
$this->assertDatabaseHas('sponsors', [
|
|
'id' => $this->charity->id,
|
|
'full_name' => 'Charity Agent Updated',
|
|
'organization_name' => 'Red Crescent UAE',
|
|
'email' => 'contact.ae@redcrescent.org',
|
|
'mobile' => '+971505555555',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test admin can suspend and activate a charity organization.
|
|
*/
|
|
public function test_admin_can_suspend_and_activate_charity()
|
|
{
|
|
// Suspend
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/charity-organizations/{$this->charity->id}/suspend");
|
|
|
|
$response->assertStatus(302);
|
|
$this->assertEquals('suspended', $this->charity->fresh()->status);
|
|
|
|
// Activate
|
|
$response = $this->actingAs($this->admin)
|
|
->withSession(['user' => $this->admin])
|
|
->post("/admin/charity-organizations/{$this->charity->id}/activate");
|
|
|
|
$response->assertStatus(302);
|
|
$this->assertEquals('active', $this->charity->fresh()->status);
|
|
}
|
|
}
|